/
utils.py
583 lines (485 loc) · 19.8 KB
/
utils.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import codecs
import json
import logging
import os
import pytz
import re
import requests
import StringIO
import tempfile
import time
import zipfile
from datetime import datetime, timedelta
from django.db.models import Prefetch
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.translation import trans_real
from translate.filters import checks
from translate.storage import base as storage_base
from translate.storage.placeables import base, general, parse
from translate.storage.placeables.interfaces import BasePlaceable
from translate.lang import data as lang_data
log = logging.getLogger('pontoon')
def get_project_locale_from_request(request, locales):
"""Get Pontoon locale from Accept-language request header."""
header = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
accept = trans_real.parse_accept_lang_header(header)
for a in accept:
try:
return locales.get(code__iexact=a[0]).code
except:
continue
class NewlineEscapePlaceable(base.Ph):
"""Placeable handling newline escapes."""
istranslatable = False
regex = re.compile(r'\\n')
parse = classmethod(general.regex_parse)
class TabEscapePlaceable(base.Ph):
"""Placeable handling tab escapes."""
istranslatable = False
regex = re.compile(r'\t')
parse = classmethod(general.regex_parse)
class EscapePlaceable(base.Ph):
"""Placeable handling escapes."""
istranslatable = False
regex = re.compile(r'\\')
parse = classmethod(general.regex_parse)
class SpacesPlaceable(base.Ph):
"""Placeable handling spaces."""
istranslatable = False
regex = re.compile('^ +| +$|[\r\n\t] +| {2,}')
parse = classmethod(general.regex_parse)
class PythonFormatNamedPlaceable(base.Ph):
"""Placeable handling named format string in python"""
istranslatable = False
regex = re.compile(r'%\([[\w\d\!\.,\[\]%:$<>\+\-= ]*\)[+|-|0\d+|#]?[\.\d+]?[s|d|e|f|g|o|x|c|%]', re.IGNORECASE)
parse = classmethod(general.regex_parse)
class PythonFormatPlaceable(base.Ph):
"""Placeable handling new format strings in python"""
istranslatable = False
regex = re.compile(r'\{{?[[\w\d\!\.,\[\]%:$<>\+\-= ]*\}?}', )
parse = classmethod(general.regex_parse)
def mark_placeables(text):
"""Wrap placeables to easily distinguish and manipulate them.
Source: http://bit.ly/1yQOC9B
"""
PARSERS = [
NewlineEscapePlaceable.parse,
TabEscapePlaceable.parse,
EscapePlaceable.parse,
# The spaces placeable can match '\n ' and mask the newline,
# so it has to come later.
SpacesPlaceable.parse,
PythonFormatNamedPlaceable.parse,
PythonFormatPlaceable.parse,
general.XMLTagPlaceable.parse,
general.AltAttrPlaceable.parse,
general.XMLEntityPlaceable.parse,
general.PythonFormattingPlaceable.parse,
general.JavaMessageFormatPlaceable.parse,
general.FormattingPlaceable.parse,
# The Qt variables can consume the %1 in %1$s which will mask a printf
# placeable, so it has to come later.
general.QtFormattingPlaceable.parse,
general.UrlPlaceable.parse,
general.FilePlaceable.parse,
general.EmailPlaceable.parse,
general.CapsPlaceable.parse,
general.CamelCasePlaceable.parse,
general.OptionPlaceable.parse,
general.PunctuationPlaceable.parse,
general.NumberPlaceable.parse,
]
TITLES = {
'NewlineEscapePlaceable': "Escaped newline",
'TabEscapePlaceable': "Escaped tab",
'EscapePlaceable': "Escaped sequence",
'SpacesPlaceable': "Unusual space in string",
'AltAttrPlaceable': "'alt' attribute inside XML tag",
'NewlinePlaceable': "New-line",
'NumberPlaceable': "Number",
'QtFormattingPlaceable': "Qt string formatting variable",
'PythonFormattingPlaceable': "Python string formatting variable",
'JavaMessageFormatPlaceable': "Java Message formatting variable",
'FormattingPlaceable': "String formatting variable",
'UrlPlaceable': "URI",
'FilePlaceable': "File location",
'EmailPlaceable': "Email",
'PunctuationPlaceable': "Punctuation",
'XMLEntityPlaceable': "XML entity",
'CapsPlaceable': "Long all-caps string",
'CamelCasePlaceable': "Camel case string",
'XMLTagPlaceable': "XML tag",
'OptionPlaceable': "Command line option",
'PythonFormatNamedPlaceable': "Python format string",
'PythonFormatPlaceable': "Python format string"
}
output = u""
# Get a flat list of placeables and StringElem instances
flat_items = parse(text, PARSERS).flatten()
for item in flat_items:
# Placeable: mark
if isinstance(item, BasePlaceable):
class_name = item.__class__.__name__
placeable = unicode(item)
# CSS class used to mark the placeable
css = {
'TabEscapePlaceable': "escape ",
'EscapePlaceable': "escape ",
'SpacesPlaceable': "space ",
'NewlinePlaceable': "escape ",
}.get(class_name, "")
title = TITLES.get(class_name, "Unknown placeable")
spaces = ' ' * len(placeable)
if not placeable.startswith(' '):
spaces = placeable[0] + ' ' * (len(placeable) - 1)
# Correctly render placeables in translation editor
content = {
'TabEscapePlaceable': u'\\t',
'EscapePlaceable': u'\\',
'SpacesPlaceable': spaces,
'NewlinePlaceable': {
u'\r\n': u'\\r\\n<br/>\n',
u'\r': u'\\r<br/>\n',
u'\n': u'\\n<br/>\n',
}.get(placeable),
'PythonFormatPlaceable':
placeable.replace('&', '&').replace('<', '<').replace('>', '>'),
'PythonFormatNamedPlaceable':
placeable.replace('&', '&').replace('<', '<').replace('>', '>'),
'XMLEntityPlaceable': placeable.replace('&', '&'),
'XMLTagPlaceable':
placeable.replace('<', '<').replace('>', '>'),
}.get(class_name, placeable)
output += ('<mark class="%splaceable" title="%s">%s</mark>') \
% (css, title, content)
# Not a placeable: skip
else:
output += unicode(item).replace('<', '<').replace('>', '>')
return output
def quality_check(original, string, locale, ignore):
"""Check for obvious errors like blanks and missing interpunction."""
if not ignore:
original = lang_data.normalized_unicode(original)
string = lang_data.normalized_unicode(string)
unit = storage_base.TranslationUnit(original)
unit.target = string
checker = checks.StandardChecker(
checkerconfig=checks.CheckerConfig(targetlanguage=locale.code))
warnings = checker.run_filters(unit)
if warnings:
# https://github.com/translate/pootle/
check_names = {
'accelerators': 'Accelerators',
'acronyms': 'Acronyms',
'blank': 'Blank',
'brackets': 'Brackets',
'compendiumconflicts': 'Compendium conflict',
'credits': 'Translator credits',
'doublequoting': 'Double quotes',
'doublespacing': 'Double spaces',
'doublewords': 'Repeated word',
'emails': 'E-mail',
'endpunc': 'Ending punctuation',
'endwhitespace': 'Ending whitespace',
'escapes': 'Escapes',
'filepaths': 'File paths',
'functions': 'Functions',
'gconf': 'GConf values',
'kdecomments': 'Old KDE comment',
'long': 'Long',
'musttranslatewords': 'Must translate words',
'newlines': 'Newlines',
'nplurals': 'Number of plurals',
'notranslatewords': 'Don\'t translate words',
'numbers': 'Numbers',
'options': 'Options',
'printf': 'printf()',
'puncspacing': 'Punctuation spacing',
'purepunc': 'Pure punctuation',
'sentencecount': 'Number of sentences',
'short': 'Short',
'simplecaps': 'Simple capitalization',
'simpleplurals': 'Simple plural(s)',
'singlequoting': 'Single quotes',
'startcaps': 'Starting capitalization',
'startpunc': 'Starting punctuation',
'startwhitespace': 'Starting whitespace',
'tabs': 'Tabs',
'unchanged': 'Unchanged',
'untranslated': 'Untranslated',
'urls': 'URLs',
'validchars': 'Valid characters',
'variables': 'Placeholders',
'xmltags': 'XML tags',
}
warnings_array = []
for key in warnings.keys():
warning = check_names.get(key, key)
warnings_array.append(warning)
return HttpResponse(json.dumps({
'warnings': warnings_array,
}), content_type='application/json')
def first(collection, test, default=None):
"""
Return the first item that, when passed to the given test function,
returns True. If no item passes the test, return the default value.
"""
return next((c for c in collection if test(c)), default)
def match_attr(collection, **attributes):
"""
Return the first item that has matching values for the given
attributes, or None if no item is found to match.
"""
return first(
collection,
lambda i: all(getattr(i, attrib) == value
for attrib, value in attributes.items()),
default=None
)
def aware_datetime(*args, **kwargs):
"""Return an aware datetime using Django's configured timezone."""
return timezone.make_aware(datetime(*args, **kwargs))
def extension_in(filename, extensions):
"""
Check if the extension for the given filename is in the list of
allowed extensions. Uses os.path.splitext rules for getting the
extension.
"""
filename, extension = os.path.splitext(filename)
if extension and extension[1:] in extensions:
return True
else:
return False
def get_object_or_none(model, *args, **kwargs):
"""
Get an instance of the given model, returning None instead of
raising an error if an instance cannot be found.
"""
try:
return model.objects.get(*args, **kwargs)
except model.DoesNotExist:
return None
def require_AJAX(f):
"""
AJAX request required decorator
"""
def wrap(request, *args, **kwargs):
if not request.is_ajax():
return HttpResponseBadRequest('Bad Request: Request must be AJAX')
return f(request, *args, **kwargs)
return wrap
def _download_file(prefixes, dirnames, relative_path):
for prefix in prefixes:
for dirname in dirnames:
url = os.path.join(prefix.format(locale_code=dirname), relative_path)
r = requests.get(url, stream=True)
if not r.ok:
continue
extension = os.path.splitext(relative_path)[1]
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as temp:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
temp.write(chunk)
temp.flush()
return temp.name
def _get_relative_path_from_part(slug, part):
"""Check if part is a Resource path or Subpage name."""
# Avoid circular import; someday we should refactor to avoid.
from pontoon.base.models import Subpage
try:
subpage = Subpage.objects.get(project__slug=slug, name=part)
return subpage.resources.first().path
except Subpage.DoesNotExist:
return part
def get_download_content(slug, code, part):
"""
Get content of the file to be downloaded.
:param str slug: Project slug.
:param str code: Locale code.
:param str part: Resource path or Subpage name.
"""
# Avoid circular import; someday we should refactor to avoid.
from pontoon.sync import formats
from pontoon.sync.vcs.models import VCSProject
from pontoon.base.models import Entity, Locale, Project, Resource
project = get_object_or_404(Project, slug=slug)
locale = get_object_or_404(Locale, code__iexact=code)
# Download a ZIP of all files if project has > 1 and < 10 resources
isZipable = 1 < project.resources.count() < 10
if isZipable:
resources = project.resources.all()
s = StringIO.StringIO()
zf = zipfile.ZipFile(s, "w")
# Download a single file if project has 1 or >= 10 resources
else:
relative_path = _get_relative_path_from_part(slug, part)
resources = [get_object_or_404(Resource, project__slug=slug, path=relative_path)]
for resource in resources:
# Get locale file
locale_prefixes = (
project.repositories.filter(permalink_prefix__contains='{locale_code}')
.values_list('permalink_prefix', flat=True)
.distinct()
)
dirnames = set([locale.code, locale.code.replace('-', '_')])
locale_path = _download_file(locale_prefixes, dirnames, resource.path)
if not locale_path and not resource.is_asymmetric:
return None, None
# Get source file if needed
source_path = None
if resource.is_asymmetric:
source_prefixes = (
project.repositories
.values_list('permalink_prefix', flat=True)
.distinct()
)
dirnames = VCSProject.SOURCE_DIR_NAMES
source_path = _download_file(source_prefixes, dirnames, resource.path)
if not source_path:
return None, None
# If locale file doesn't exist, create it
if not locale_path:
extension = os.path.splitext(resource.path)[1]
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as temp:
temp.flush()
locale_path = temp.name
# Update file from database
resource_file = formats.parse(locale_path, source_path)
entities_dict = {}
entities_qs = Entity.objects.filter(
changedentitylocale__locale=locale,
resource__project=project,
resource__path=resource.path,
obsolete=False
)
for e in entities_qs:
entities_dict[e.key] = e.translation_set.filter(approved=True, locale=locale)
for vcs_translation in resource_file.translations:
key = vcs_translation.key
if key in entities_dict:
entity = entities_dict[key]
vcs_translation.update_from_db(entity)
resource_file.save(locale)
if not locale_path:
return None, None
if isZipable:
zf.write(locale_path, resource.path)
else:
with codecs.open(locale_path, 'r', 'utf-8') as f:
content = f.read()
filename = os.path.basename(resource.path)
# Remove temporary files
os.remove(locale_path)
if source_path:
os.remove(source_path)
if isZipable:
zf.close()
content = s.getvalue()
filename = project.slug + '.zip'
return content, filename
def handle_upload_content(slug, code, part, f, user):
"""
Update translations in the database from uploaded file.
:param str slug: Project slug.
:param str code: Locale code.
:param str part: Resource path or Subpage name.
:param UploadedFile f: UploadedFile instance.
:param User user: User uploading the file.
"""
# Avoid circular import; someday we should refactor to avoid.
from pontoon.sync import formats
from pontoon.sync.changeset import ChangeSet
from pontoon.sync.vcs.models import VCSProject
from pontoon.base.models import (
ChangedEntityLocale,
Entity,
Locale,
Project,
Resource,
TranslatedResource,
Translation,
)
relative_path = _get_relative_path_from_part(slug, part)
project = get_object_or_404(Project, slug=slug)
locale = get_object_or_404(Locale, code__iexact=code)
resource = get_object_or_404(Resource, project__slug=slug, path=relative_path)
# Store uploaded file to a temporary file and parse it
extension = os.path.splitext(f.name)[1]
with tempfile.NamedTemporaryFile(suffix=extension) as temp:
for chunk in f.chunks():
temp.write(chunk)
temp.flush()
resource_file = formats.parse(temp.name)
# Update database objects from file
changeset = ChangeSet(
project,
VCSProject(project, locales=[locale]),
timezone.now()
)
entities_qs = Entity.objects.filter(
resource__project=project,
resource__path=relative_path,
obsolete=False
).prefetch_related(
Prefetch(
'translation_set',
queryset=Translation.objects.filter(locale=locale),
to_attr='db_translations'
)
).prefetch_related(
Prefetch(
'translation_set',
queryset=Translation.objects.filter(locale=locale, approved_date__lte=timezone.now()),
to_attr='old_translations'
)
)
entities_dict = {entity.key: entity for entity in entities_qs}
for vcs_translation in resource_file.translations:
key = vcs_translation.key
if key in entities_dict:
entity = entities_dict[key]
changeset.update_entity_translations_from_vcs(
entity, locale.code, vcs_translation, user,
entity.db_translations, entity.old_translations
)
changeset.bulk_create_translations()
changeset.bulk_update_translations()
TranslatedResource.objects.get(resource=resource, locale=locale).calculate_stats()
# Mark translations as changed
changed_entities = {}
existing = ChangedEntityLocale.objects.values_list('entity', 'locale').distinct()
for t in changeset.translations_to_create + changeset.translations_to_update:
key = (t.entity.pk, t.locale.pk)
# Remove duplicate changes to prevent unique constraint violation
if not key in existing:
changed_entities[key] = ChangedEntityLocale(entity=t.entity, locale=t.locale)
ChangedEntityLocale.objects.bulk_create(changed_entities.values())
def latest_datetime(datetimes):
"""
Return the latest datetime in the given list of datetimes,
gracefully handling `None` values in the list. Returns `None` if all
values in the list are `None`.
"""
if all(map(lambda d: d is None, datetimes)):
return None
min_datetime = timezone.make_aware(datetime.min)
datetimes = map(lambda d: d or min_datetime, datetimes)
return max(datetimes)
def parse_time_interval(interval):
"""
Return start and end time objects from time interval string n the format
%d%m%Y%H%M-%d%m%Y%H%M. Also, increase interval by one minute due to
truncation to a minute in Translation.counts_per_minute QuerySet.
"""
def parse_timestamp(timestamp):
return timezone.make_aware(datetime.strptime(timestamp, '%Y%m%d%H%M'), timezone=pytz.UTC)
start = interval.split('-')[0]
end = interval.split('-')[1]
return parse_timestamp(start), parse_timestamp(end) + timedelta(minutes=1)
def convert_to_unix_time(my_datetime):
"""
Convert datetime object to UNIX time
"""
return int(time.mktime(my_datetime.timetuple()) * 1000)