forked from KA-Advocates/KATranslationCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
executable file
·344 lines (316 loc) · 14.6 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
# coding: utf-8
"""
EXPERIMENTAL
Regular expression rule checker for Khan Academy translations.
Instructions:
- Download https://crowdin.com/download/project/khanacademy.zip
- Unzip the 'de' folder.
- From the directory where the 'de' folder is located, run this script.
"""
import re
import operator
import simplejson as json
import itertools
import os
import os.path
import glob
import urllib
import shutil
import datetime
import functools
import concurrent.futures
import collections
from XLIFFReader import *
from toolz.dicttoolz import valfilter, merge, merge_with, keyfilter, valmap
from toolz.itertoolz import groupby, reduceby
from multiprocessing import Pool
from ansicolor import red, black, blue
from UpdateAllFiles import get_translation_urls
from Rules import Severity, importRulesForLanguage
from LintReport import readAndMapLintEntries, NoResultException
from AutoTranslateCommon import to_crowdin_search_string
XLIFFEntry = collections.namedtuple("XLIFFEntry", ["id", "english", "translated", "is_untranslated", "note"])
def writeToFile(filename, s):
"Utility function to write a string to a file identified by its filename"
with open(filename, "w") as outfile:
outfile.write(s)
def writeJSONToFile(filename, obj):
"Utility function to write a string to a file identified by its filename"
with open(filename, "w") as outfile:
json.dump(obj, outfile)
def findPOFiles(directory):
"""
Get a list of PO files (.po / .pot) which are present in the directory.
"""
if os.path.isfile(directory): #Single file>=
poFilenames = [directory]
else:
poFilenames = []
#Recursively iterate directory, ignore everythin except *.po
for (curdir, _, files) in os.walk(directory):
for f in files:
#Ignore non-PO files
if not f.endswith(".po") and not f.endswith(".pot"): continue
#Add to list of files to process
poFilenames.append(os.path.join(curdir, f))
return poFilenames
_multiSpace = re.compile(r"\s+")
class JSONHitRenderer(object):
"""
A state container for the code which applies rules and generates HTML.
"""
def __init__(self, outdir, lang="de", num_processes=2):
self.lang = lang
# Create output directory
self.outdir = os.path.join(outdir, lang)
os.makedirs(self.outdir, exist_ok=True)
# Async executor
self.executor = concurrent.futures.ThreadPoolExecutor(num_processes)
# Load rules for language
rules, rule_errors = importRulesForLanguage(lang)
self.rules = sorted(rules, reverse=True)
self.rule_errors = rule_errors
# Get timestamp
self.timestamp = datetime.datetime.now().strftime("%y-%m-%d %H:%M:%S")
# Process lastdownload date (copied to the templated)
lastdownloadPath = os.path.join("cache", "lastdownload-{}.txt".format(lang))
if os.path.isfile(lastdownloadPath):
with open(lastdownloadPath) as infile:
self.downloadTimestamp = infile.read().strip()
else:
self.downloadTimestamp = None
# Initialize translation ID/URL map
self.translationURLs = get_translation_urls(lang)
def file_relpath(self, filename):
return os.path.relpath(filename, os.path.join("cache", self.lang))
def computeRuleHits(self, filename):
"""
Compute all rule hits for a single parsed PO file and return a list of hits
"""
# Compute relative path (which is how Crowin refers to the file)
relpath = self.file_relpath(filename)
# Read XLIFF
basename = os.path.basename(filename)
soup = parse_xliff_file(filename)
try:
body = soup.xliff.file.body
except AttributeError:
print(red("File {} is not valid XLIFF - Ignoring.".format(relpath)))
return []
# Iterate over all translatable strings and apply rule
rule_hits = defaultdict(list)
print(filename)
for trans_unit in body.find_all("trans-unit"):
# Extract info
source = trans_unit.source
target = trans_unit.target
# Broken XLIFF entry
if source is None or target is None:
continue
note = "" if trans_unit.note is None else trans_unit.note.texts
# Convert to XLIFF entry
is_untranslated = ("state" in target.attrs and target["state"] == "needs-translation")
entry = XLIFFEntry(trans_unit["id"], source.text,
"" if is_untranslated else target.text,
is_untranslated, note)
# Apply to rules
for rule in self.rules:
rule_hits[rule] += list(rule.apply_to_xliff_entry(entry, relpath))
# Convert to list which is easier to process down the chain
gc.collect()
return [
(relpath, rule, hits)
for rule, hits in rule_hits.items()
]
def computeRuleHitsForFileSet(self, xliffs):
"""
For each file in the given filename -> PO object dictionary,
compute the Rule -> Hits dictonary.
Stores the information in the current instance.
Does not return anything
"""
# Compute dict with sorted & prettified filenames
self.files = sorted(xliffs.keys())
# Add all futures to the executor
futures = [self.executor.submit(self.computeRuleHits, filename)
for filename in xliffs.keys()]
# Process the results in first-received order. Also keep track of rule performance
self.fileRuleHits = collections.defaultdict(dict)
n_finished = 0
# Intermediate result storage
raw_results = collections.defaultdict(dict) # filename -> {rule: result}
for future in concurrent.futures.as_completed(futures):
# Extract result
for filename, rule, result in future.result():
self.fileRuleHits[filename][rule] = result
# Track progress
n_finished += 1
if n_finished % 1000 == 0:
percent_finished = n_finished * 100. / len(futures)
print("Rule computation finished {0:.2f} %".format(percent_finished))
# Compute total stats by file
self.statsByFile = {
filename: merge(self.ruleHitsToSeverityCountMap(ruleHits), {
"translation_url": self.translationURLs[filename]})
for filename, ruleHits in self.fileRuleHits.items()
}
# Compute map filename -> {rule: numHits for rule}
self.statsByFileAndRule = {
filename: valmap(len, ruleHits)
for filename, ruleHits in self.fileRuleHits.items()
}
# Compute map rule -> numHits for rule
self.totalStatsByRule = merge_with(sum, *(self.statsByFileAndRule.values()))
def ruleHitsToSeverityCountMap(self, rule_hit_map):
"""
In a rule -> hitlist mapping, count the total number of hits above or at a given severity
level and return a cumulative dictionary
"""
# Create a map severity -> count
severity_counts = collections.defaultdict(int)
for rule, hits in rule_hit_map.items():
severity_counts[rule.severity] += len(hits)
# Create string severity -> count map
above_severity = lambda sev: sum(keyfilter(lambda k: k >= sev, severity_counts).values())
return {"hits": above_severity(Severity.standard),
"warnings": above_severity(Severity.warning),
"errors": above_severity(Severity.dangerous),
"infos": above_severity(Severity.info),
"notices": above_severity(Severity.notice)}
def countRuleHitsAboveSeverity(self, ruleHits, severity):
return sum((len(hits) for rule, hits in ruleHits.items() if rule.severity >= severity))
def writeStatsJSON(self):
"""
Write a statistics-by-filename JSON to outdir/filestats.sjon
"""
# Write file
stats = {
filename: {"hits": self.countRuleHitsAboveSeverity(ruleHits, Severity.standard),
"warnings": self.countRuleHitsAboveSeverity(ruleHits, Severity.warning),
"errors": self.countRuleHitsAboveSeverity(ruleHits, Severity.dangerous),
"infos": self.countRuleHitsAboveSeverity(ruleHits, Severity.info),
"notices": self.countRuleHitsAboveSeverity(ruleHits, Severity.notice)}
for filename, ruleHits in self.fileRuleHits.items()
}
writeJSONToFile(os.path.join(self.outdir, "filestats.json"), stats)
def _renderDirectory(self, ruleHits, ruleStats, directory, filename):
# Generate output HTML for each rule
for rule, hits in ruleHits.items():
# Render hits for individual rule
outfilePathJSON = os.path.join(directory, rule.machine_name + ".json")
if len(hits) > 0: # Render hits
# Generate JSON API
jsonAPI = {
"timestamp": self.timestamp,
"downloadTimestamp": self.downloadTimestamp,
"rule": rule.meta_dict,
# valfilter: remove empty values for smaller JSON
"hits": [valfilter(bool, {"msgstr": entry.translated,
"msgid": entry.english,
"tcomment": entry.note,
"hit": hit,
"origImages": origImages,
"translatedImages": translatedImages,
"crowdinLink": "{}#{}".format(self.translationURLs[filename], entry.id)
})
for entry, hit, filename, origImages, translatedImages in hits]
}
writeJSONToFile(outfilePathJSON, jsonAPI)
else: # Remove file (redirects to 404 file) if there are no exportHitsAsJSON
if os.path.isfile(outfilePathJSON):
os.remove(outfilePathJSON)
# Render file index page (no filelist)
ruleInfos = [merge(rule.meta_dict, {"num_hits": ruleStats[rule]})
for rule in self.rules if ruleStats[rule] > 0]
ruleInfos.sort(key=lambda o: -o["severity"]) # Invert sort order
js = {
"pageTimestamp": self.timestamp,
"downloadTimestamp": self.downloadTimestamp,
"stats": ruleInfos,
"files": [merge(self.statsByFile[filename], {"filename": filename})
for filename in map(self.file_relpath, self.files)
if self.statsByFile[filename]["notices"] > 0]
}
writeJSONToFile(os.path.join(directory, "index.json"), js)
def exportHitsAsJSON(self):
"""
Apply a rule and write a directory of output HTML files
"""
for filename, ruleHits in self.fileRuleHits.items():
rule_stats = self.statsByFileAndRule[filename]
# Ensure output directory is present
directory = os.path.join(self.outdir, filename)
os.makedirs(directory, exist_ok=True)
# Perform rendering
self._renderDirectory(ruleHits, rule_stats, directory, filename)
#####################
## Render overview ##
#####################
# Compute global hits for every rule
overview_hits = {
rule: list(itertools.chain(*(fileHits[rule] for fileHits in self.fileRuleHits.values())))
for rule in self.rules
}
self._renderDirectory(overview_hits, self.totalStatsByRule, self.outdir, filename="All files")
# Create rule error file
writeJSONToFile(os.path.join(self.outdir, "ruleerrors.json"),
[err.msg for err in self.rule_errors])
# Copy static files
for filename in glob.glob("templates/*"):
shutil.copyfile(filename, os.path.join(self.outdir, os.path.split(filename)[-1]))
def renderLint(outdir, kalangcode):
"Parse & render lint"
# Map from KA code to crowdin code
langMap = {"pt": "pt-BR", "pt-pt": "pt-PT", "ur": "ur-PK", "es": "es-ES", "hy": "hy-AM"}
lang = langMap[kalangcode] if kalangcode in langMap else kalangcode
print(black("Rendering lint for {0} ({1})".format(lang, kalangcode), bold=True))
# Generate lint report
lintFilename = os.path.join("cache", "{0}-lint.csv".format(kalangcode))
if os.path.isfile(lintFilename):
lintEntries = list(readAndMapLintEntries(lintFilename, lang))
# Write JSON
jsonEntries = list(map(operator.methodcaller("_asdict"), lintEntries))
os.makedirs(os.path.join(outdir, lang), exist_ok=True)
writeJSONToFile(os.path.join(outdir, lang, "lint.json"), jsonEntries)
else:
print("Skipping lint ({0} does not exist)".format(lintFilename))
def renderAllLints(outdir):
rgx = re.compile(r"([a-z]{2}(-[a-z]{2})?)-lint.csv")
for f in os.listdir("cache"):
m = rgx.match(f)
if m is None: continue
lang = m.group(1)
renderLint(outdir, lang)
def performRenderLint(args):
renderAllLints("output")
def performRender(args):
# Download / update if requested
if args.download:
download()
# Create directory
if not args.outdir:
args.outdir = "output"
os.makedirs(args.outdir, exist_ok=True)
renderer = JSONHitRenderer(args.outdir, args.language, args.num_processes)
# Import
potDir = os.path.join("cache", args.language)
xliffFiles = findXLIFFFiles(potDir, filt=args.filter)
print(black("Reading {} files from {} folder...".format(len(xliffFiles), potDir), bold=True))
# Compute hits
print(black("Computing rules...", bold=True))
renderer.computeRuleHitsForFileSet(xliffFiles)
# Generate HTML
print(black("Rendering HTML...", bold=True))
renderer.exportHitsAsJSON()
# Generate filestats.json
print (black("Generating JSON API files...", bold=True))
renderer.writeStatsJSON()
# If data is present, generate subtitle information
videosJSONPath = os.path.join("cache", "videos.json")
if os.path.isfile(videosJSONPath):
print (black("Rendering subtitles overview...", bold=True))
with open(videosJSONPath) as infile:
exercises = json.load(infile)
subtitleTemplate = renderer.env.get_template("subtitles.html")
writeToFile(os.path.join(args.outdir, "subtitles.html"), subtitleTemplate.render(exercises=exercises))