-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
base.py
463 lines (373 loc) · 13.7 KB
/
base.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
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""Base classses for file formats."""
import os
import sys
import tempfile
from copy import deepcopy
import six
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _
from weblate.utils.hash import calculate_hash
def move_atomic(source, target):
"""Tries to perform atomic move.
This is tricky on Windows as until Python 3.3 there is no function for that. And
even on Python 3.3 the MoveFileEx is not guarateed to be atomic, so it might fail in
some cases. Anyway we try to choose best available method.
"""
# Use os.replace if available
if sys.version_info >= (3, 3):
os.replace(source, target)
else:
# Remove target on Windows if exists
if sys.platform == 'win32' and os.path.exists(target):
os.unlink(target)
# Use os.rename
os.rename(source, target)
class UnitNotFound(Exception):
pass
class TranslationUnit(object):
"""Wrapper for translate-toolkit unit.
It handles ID/template based translations and other API differences.
"""
def __init__(self, parent, unit, template=None):
"""Create wrapper object."""
self.unit = unit
self.template = template
self.parent = parent
if template is not None:
self.mainunit = template
else:
self.mainunit = unit
def _invalidate_target(self):
"""Invalidate target cache."""
if 'target' in self.__dict__:
del self.__dict__['target']
@cached_property
def locations(self):
"""Return comma separated list of locations."""
return ''
@cached_property
def flags(self):
"""Return flags or typecomments from units."""
return ''
@cached_property
def notes(self):
"""Return notes from units."""
return ''
@cached_property
def source(self):
"""Return source string from a ttkit unit."""
raise NotImplementedError()
@cached_property
def target(self):
"""Return target string from a ttkit unit."""
raise NotImplementedError()
@cached_property
def context(self):
"""Return context of message.
In some cases we have to use ID here to make all backends consistent.
"""
raise NotImplementedError()
@cached_property
def previous_source(self):
"""Return previous message source if there was any."""
return ''
@cached_property
def id_hash(self):
"""Return hash of source string, used for quick lookup.
We use siphash as it is fast and works well for our purpose.
"""
if self.template is None:
return calculate_hash(self.source, self.context)
return calculate_hash(None, self.context)
@cached_property
def content_hash(self):
"""Return hash of source string and context, used for quick lookup."""
if self.template is None:
return self.id_hash
return calculate_hash(self.source, self.context)
def is_translated(self):
"""Check whether unit is translated."""
return bool(self.target)
def is_approved(self, fallback=False):
"""Check whether unit is appoved."""
return fallback
def is_fuzzy(self, fallback=False):
"""Check whether unit needs edit."""
return fallback
def has_content(self):
"""Check whether unit has content."""
return True
def is_readonly(self):
"""Check whether unit is read only."""
return False
def set_target(self, target):
"""Set translation unit target."""
raise NotImplementedError()
def mark_fuzzy(self, fuzzy):
"""Set fuzzy flag on translated unit."""
raise NotImplementedError()
def mark_approved(self, value):
"""Set approved flag on translated unit."""
raise NotImplementedError()
class TranslationFormat(object):
"""Generic object defining file format loader."""
name = ''
format_id = ''
monolingual = None
check_flags = ()
unit_class = TranslationUnit
autoload = ()
can_add_unit = True
language_format = 'posix'
simple_filename = True
new_translation = None
@classmethod
def get_identifier(cls):
return cls.format_id
@classmethod
def parse(
cls, storefile, template_store=None, language_code=None, is_template=False
):
"""Parse store and returns TranslationFormat instance.
This wrapper is needed for AutodetectFormat to be able to return instance of
different class.
"""
return cls(storefile, template_store, language_code, is_template)
def __init__(
self, storefile, template_store=None, language_code=None, is_template=False
):
"""Create file format object, wrapping up translate-toolkit's store."""
if not isinstance(storefile, six.string_types) and not hasattr(
storefile, 'mode'
):
storefile.mode = 'r'
self.storefile = storefile
# Load store
self.store = self.load(storefile)
# Remember template
self.template_store = template_store
self.is_template = is_template
def check_valid(self):
"""Check store validity."""
if not self.is_valid():
raise ValueError(
_('Failed to load strings from the file, try choosing other format.')
)
def get_filenames(self):
if isinstance(self.storefile, six.string_types):
return [self.storefile]
return [self.storefile.name]
@classmethod
def load(cls, storefile):
raise NotImplementedError()
def get_plural(self, language):
"""Return matching plural object."""
return language.plural
@cached_property
def has_template(self):
"""Check whether class is using template."""
return (
self.monolingual or self.monolingual is None
) and self.template_store is not None
@cached_property
def _context_index(self):
"""ID based index for units."""
return {unit.context: unit for unit in self.mono_units}
def find_unit_mono(self, context):
try:
# The mono units always have only template set
return self._context_index[context].template
except KeyError:
return None
def _find_unit_template(self, context):
# Need to create new unit based on template
template_ttkit_unit = self.template_store.find_unit_mono(context)
# We search by ID when using template
ttkit_unit = self.find_unit_mono(context)
# We always need new unit to translate
if ttkit_unit is None:
if template_ttkit_unit is None:
raise UnitNotFound('Unit not found: {}'.format(context))
ttkit_unit = deepcopy(template_ttkit_unit)
add = True
else:
add = False
return (self.unit_class(self, ttkit_unit, template_ttkit_unit), add)
@cached_property
def _source_index(self):
"""Context and source based index for units."""
return {(unit.context, unit.source): unit for unit in self.all_units}
def _find_unit_bilingual(self, context, source):
try:
return (self._source_index[context, source], False)
except KeyError:
raise UnitNotFound('Unit not found: {}, {}'.format(context, source))
def find_unit(self, context, source):
"""Find unit by context and source.
Returns tuple (ttkit_unit, created) indicating whether returned unit is new one.
"""
if self.has_template:
return self._find_unit_template(context)
return self._find_unit_bilingual(context, source)
def add_unit(self, ttkit_unit):
"""Add new unit to underlaying store."""
raise NotImplementedError()
def update_header(self, **kwargs):
"""Update store header if available."""
return
def save_atomic(self, filename, callback):
dirname, basename = os.path.split(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
temp = tempfile.NamedTemporaryFile(prefix=basename, dir=dirname, delete=False)
try:
callback(temp)
temp.close()
move_atomic(temp.name, filename)
finally:
if os.path.exists(temp.name):
os.unlink(temp.name)
def save(self):
"""Save underlaying store to disk."""
raise NotImplementedError()
@property
def all_store_units(self):
"""Wrapper for all store units for possible filtering."""
return self.store.units
@cached_property
def mono_units(self):
return [self.unit_class(self, None, unit) for unit in self.all_store_units]
@cached_property
def all_units(self):
"""List of all units."""
if not self.has_template:
return [self.unit_class(self, unit) for unit in self.all_store_units]
return [
self.unit_class(self, self.find_unit_mono(unit.context), unit.template)
for unit in self.template_store.mono_units
]
@property
def content_units(self):
yield from (unit for unit in self.all_units if unit.has_content())
@staticmethod
def mimetype():
"""Return most common mime type for format."""
return 'text/plain'
@staticmethod
def extension():
"""Return most common file extension for format."""
return 'txt'
def is_valid(self):
"""Check whether store seems to be valid."""
return True
@classmethod
def is_valid_base_for_new(cls, base, monolingual):
"""Check whether base is valid."""
raise NotImplementedError()
@classmethod
def get_language_code(cls, code, language_format=None):
"""Do any possible formatting needed for language code."""
if not language_format:
language_format = cls.language_format
return getattr(cls, 'get_language_{}'.format(language_format))(code)
@staticmethod
def get_language_posix(code):
return code
@staticmethod
def get_language_bcp(code):
return code.replace('_', '-')
@staticmethod
def get_language_android(code):
# Android doesn't use Hans/Hant, but rather TW/CN variants
if code == 'zh_Hans':
return 'zh-rCN'
if code == 'zh_Hant':
return 'zh-rTW'
sanitized = code.replace('-', '_')
if '_' in sanitized and len(sanitized.split('_')[1]) > 2:
return 'b+{}'.format(sanitized.replace('_', '+'))
return sanitized.replace('_', '-r')
@classmethod
def get_language_java(cls, code):
# Java doesn't use Hans/Hant, but rather TW/CN variants
if code == 'zh_Hans':
return 'zh-CN'
if code == 'zh_Hant':
return 'zh-TW'
if code == 'zh_Hans_SG':
return 'zh-SG'
if code == 'zh_Hant_HK':
return 'zh-HK'
return cls.get_language_bcp(code)
@classmethod
def get_language_filename(cls, mask, code):
"""Return full filename of a language file.
Calculated forfor given path, filemask and language code.
"""
return mask.replace('*', cls.get_language_code(code))
@classmethod
def add_language(cls, filename, language, base):
"""Add new language file."""
# Create directory for a translation
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
cls.create_new_file(filename, language, base)
@classmethod
def create_new_file(cls, filename, language, base):
"""Handle creation of new translation file."""
raise NotImplementedError()
def iterate_merge(self, fuzzy):
"""Iterate over units for merging.
Note: This can change fuzzy state of units!
"""
for unit in self.content_units:
# Skip fuzzy (if asked for that)
if unit.is_fuzzy():
if not fuzzy:
continue
elif not unit.is_translated():
continue
# Unmark unit as fuzzy (to allow merge)
set_fuzzy = False
if fuzzy and unit.is_fuzzy():
unit.mark_fuzzy(False)
if fuzzy != 'approve':
set_fuzzy = True
yield set_fuzzy, unit
def create_unit(self, key, source):
raise NotImplementedError()
def new_unit(self, key, source):
"""Add new unit to monolingual store."""
unit = self.create_unit(key, source)
self.add_unit(unit)
self.save()
@classmethod
def get_class(cls):
raise NotImplementedError()
class EmptyFormat(TranslationFormat):
"""For testing purposes."""
@classmethod
def load(cls, storefile):
return type(str(''), (object,), {"units": []})()
def save(self):
return