Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: packagerChrome.py

Issue 29517660: Issue 5477 - Import everything from imported locales (Closed)
Patch Set: Further simplifying Created Aug. 18, 2017, 2:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | packagerEdge.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This Source Code Form is subject to the terms of the Mozilla Public 1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 4
5 import errno 5 import errno
6 import io 6 import io
7 import json 7 import json
8 import os 8 import os
9 import re 9 import re
10 from StringIO import StringIO 10 from StringIO import StringIO
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 ).encode('utf-8') 183 ).encode('utf-8')
184 184
185 185
186 def toJson(data): 186 def toJson(data):
187 return json.dumps( 187 return json.dumps(
188 data, ensure_ascii=False, sort_keys=True, 188 data, ensure_ascii=False, sort_keys=True,
189 indent=2, separators=(',', ': ') 189 indent=2, separators=(',', ': ')
190 ).encode('utf-8') + '\n' 190 ).encode('utf-8') + '\n'
191 191
192 192
193 def importGeckoLocales(params, files): 193 def set_translation_and_additional_info(data, key, source, stringID):
194 """Sets data[key] to the desired {'message':value}, adds all additional
195 info from the source dict"""
Sebastian Noack 2017/08/18 14:53:59 As per PEP-257, the trailing """ goes onto a new l
tlucas 2017/08/18 15:10:35 Done.
196 value = source[stringID]['message']
Sebastian Noack 2017/08/18 14:53:59 It seems all usage of "source" and "stringID" here
tlucas 2017/08/18 15:10:34 Done.
197
198 data[key] = {'messages': value}
199 for k, v in source[stringID].items():
200 data[key].setdefault(k, v)
201
202
203 def set_translation_without_access_keys(data, key, source, stringID):
Sebastian Noack 2017/08/18 14:53:59 I think better names for these functions would be
tlucas 2017/08/18 15:10:35 Done.
204 """only set {'message': value} in data-dictionary, after stripping
205 undesired gecko-style access keys"""
206 value = source[stringID]
207
208 # Remove access keys from possible gecko-style
209 # translations
210 match = re.search(r'^(.*?)\s*\(&.\)$', value)
211 if match:
212 value = match.group(1)
213 else:
214 index = value.find('&')
215 if index >= 0:
216 value = value[0:index] + value[index + 1:]
217
218 data[key] = {'message': value}
219
220
221 def import_locales(params, files):
194 import localeTools 222 import localeTools
195 223
196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as 224 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as
197 # separator instead. 225 # separator instead.
198 convert_locale_code = lambda code: code.replace('-', '_') 226 convert_locale_code = lambda code: code.replace('-', '_')
199 227
200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome 228 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome
201 # locales to themselves, merely with the dash as separator. 229 # locales to themselves, merely with the dash as separator.
202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} 230 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es}
203 231
(...skipping 16 matching lines...) Expand all
220 fileName, keys = item 248 fileName, keys = item
221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) 249 parts = map(lambda n: source if n == '*' else n, fileName.split('/') )
222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 250 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') 251 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete')
224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): 252 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ):
225 continue 253 continue
226 254
227 data = json.loads(files[targetFile].decode('utf-8')) 255 data = json.loads(files[targetFile].decode('utf-8'))
228 256
229 try: 257 try:
258 # .json and other formats provide translations differently and
Sebastian Noack 2017/08/18 14:53:59 The other format is the Gecko format. "The WebE
tlucas 2017/08/18 15:10:35 Done.
259 # / or provide additional information like e.g. "placeholders".
Sebastian Noack 2017/08/18 14:53:59 It is supposed to be "and/or" without space.
tlucas 2017/08/18 15:10:34 Done.
260 # We want to adhere to that / preserve the addtional info
230 if sourceFile.endswith('.json'): 261 if sourceFile.endswith('.json'):
231 with io.open(sourceFile, 'r', encoding='utf-8') as handle: 262 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
232 sourceData = {k: v['message'] for k, v in json.load(hand le).iteritems()} 263 sourceData = json.load(handle)
264
265 set_translation = set_translation_and_additional_info
233 else: 266 else:
234 sourceData = localeTools.readFile(sourceFile) 267 sourceData = localeTools.readFile(sourceFile)
235 268
269 set_translation = set_translation_without_access_keys
270
236 # Resolve wildcard imports 271 # Resolve wildcard imports
237 if keys == '*' or keys == '=*': 272 if keys == '*' or keys == '=*':
238 importList = sourceData.keys() 273 importList = sourceData.keys()
239 importList = filter(lambda k: not k.startswith('_'), importL ist) 274 importList = filter(lambda k: not k.startswith('_'), importL ist)
240 if keys == '=*': 275 if keys == '=*':
241 importList = map(lambda k: '=' + k, importList) 276 importList = map(lambda k: '=' + k, importList)
242 keys = ' '.join(importList) 277 keys = ' '.join(importList)
243 278
244 for stringID in keys.split(): 279 for stringID in keys.split():
245 noMangling = False 280 noMangling = False
246 if stringID.startswith('='): 281 if stringID.startswith('='):
247 stringID = stringID[1:] 282 stringID = stringID[1:]
248 noMangling = True 283 noMangling = True
249 284
250 if stringID in sourceData: 285 if stringID in sourceData:
251 if noMangling: 286 if noMangling:
252 key = re.sub(r'\W', '_', stringID) 287 key = re.sub(r'\W', '_', stringID)
253 else: 288 else:
254 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 289 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
255 if key in data: 290 if key in data:
256 print 'Warning: locale string %s defined multiple ti mes' % key 291 print 'Warning: locale string %s defined multiple ti mes' % key
257 292
258 # Remove access keys 293 set_translation(data, key, sourceData, stringID)
259 value = sourceData[stringID]
260 match = re.search(r'^(.*?)\s*\(&.\)$', value)
261 if match:
262 value = match.group(1)
263 else:
264 index = value.find('&')
265 if index >= 0:
266 value = value[0:index] + value[index + 1:]
267 data[key] = {'message': value}
268 except Exception as e: 294 except Exception as e:
269 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) 295 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e)
270 296
271 files[targetFile] = toJson(data) 297 files[targetFile] = toJson(data)
272 298
273 299
274 def truncate(text, length_limit): 300 def truncate(text, length_limit):
275 if len(text) <= length_limit: 301 if len(text) <= length_limit:
276 return text 302 return text
277 return text[:length_limit - 1].rstrip() + u'\u2026' 303 return text[:length_limit - 1].rstrip() + u'\u2026'
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 if metadata.has_section('convert_js'): 399 if metadata.has_section('convert_js'):
374 convertJS(params, files) 400 convertJS(params, files)
375 401
376 if metadata.has_section('preprocess'): 402 if metadata.has_section('preprocess'):
377 files.preprocess( 403 files.preprocess(
378 [f for f, _ in metadata.items('preprocess')], 404 [f for f, _ in metadata.items('preprocess')],
379 {'needsExt': True} 405 {'needsExt': True}
380 ) 406 )
381 407
382 if metadata.has_section('import_locales'): 408 if metadata.has_section('import_locales'):
383 importGeckoLocales(params, files) 409 import_locales(params, files)
384 410
385 files['manifest.json'] = createManifest(params, files) 411 files['manifest.json'] = createManifest(params, files)
386 if type == 'chrome': 412 if type == 'chrome':
387 fixTranslationsForCWS(files) 413 fixTranslationsForCWS(files)
388 414
389 if devenv: 415 if devenv:
390 import buildtools 416 import buildtools
391 import random 417 import random
392 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') 418 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js')
393 files['devenvVersion__'] = str(random.random()) 419 files['devenvVersion__'] = str(random.random())
394 420
395 if metadata.has_option('general', 'testScripts'): 421 if metadata.has_option('general', 'testScripts'):
396 files['qunit/index.html'] = createScriptPage( 422 files['qunit/index.html'] = createScriptPage(
397 params, 'testIndex.html.tmpl', ('general', 'testScripts') 423 params, 'testIndex.html.tmpl', ('general', 'testScripts')
398 ) 424 )
399 425
400 zipdata = files.zipToString() 426 zipdata = files.zipToString()
401 signature = None 427 signature = None
402 pubkey = None 428 pubkey = None
403 if keyFile != None: 429 if keyFile != None:
404 signature = signBinary(zipdata, keyFile) 430 signature = signBinary(zipdata, keyFile)
405 pubkey = getPublicKey(keyFile) 431 pubkey = getPublicKey(keyFile)
406 writePackage(outFile, pubkey, signature, zipdata) 432 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « no previous file | packagerEdge.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld