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: Adressing comment from Sebastian Created Aug. 18, 2017, 7:32 a.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 import_locales(params, files):
194 import localeTools 194 import localeTools
195 195
196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as 196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as
197 # separator instead. 197 # separator instead.
198 convert_locale_code = lambda code: code.replace('-', '_') 198 convert_locale_code = lambda code: code.replace('-', '_')
199 199
200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome 200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome
201 # locales to themselves, merely with the dash as separator. 201 # locales to themselves, merely with the dash as separator.
202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} 202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es}
203 203
(...skipping 16 matching lines...) Expand all
220 fileName, keys = item 220 fileName, keys = item
221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) 221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') )
222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 222 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') 223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete')
224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): 224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ):
225 continue 225 continue
226 226
227 data = json.loads(files[targetFile].decode('utf-8')) 227 data = json.loads(files[targetFile].decode('utf-8'))
228 228
229 try: 229 try:
230 # .json and other formats provide translations differently and
231 # / or provide additional information like e.g. "placeholders".
232 # We want to adhere to that / preserve the addtional info
230 if sourceFile.endswith('.json'): 233 if sourceFile.endswith('.json'):
231 with io.open(sourceFile, 'r', encoding='utf-8') as handle: 234 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
232 sourceData = {k: v['message'] for k, v in json.load(hand le).iteritems()} 235 sourceData = json.load(handle)
236
237 def get_value(data, stringID):
238 return data[stringID]['message']
239
240 def set_translation(data, key, value, source, stringID):
241 data[key] = {'messages': value}
242 for k, v in source[stringID].items():
243 data[key].setdefault(k, v)
233 else: 244 else:
234 sourceData = localeTools.readFile(sourceFile) 245 sourceData = localeTools.readFile(sourceFile)
235 246
247 def get_value(data, stringID):
248 return data[stringID]
249
250 def set_translation(data, key, value, *args):
251 data[key] = {'message': value}
252
236 # Resolve wildcard imports 253 # Resolve wildcard imports
237 if keys == '*' or keys == '=*': 254 if keys == '*' or keys == '=*':
238 importList = sourceData.keys() 255 importList = sourceData.keys()
239 importList = filter(lambda k: not k.startswith('_'), importL ist) 256 importList = filter(lambda k: not k.startswith('_'), importL ist)
240 if keys == '=*': 257 if keys == '=*':
241 importList = map(lambda k: '=' + k, importList) 258 importList = map(lambda k: '=' + k, importList)
242 keys = ' '.join(importList) 259 keys = ' '.join(importList)
243 260
244 for stringID in keys.split(): 261 for stringID in keys.split():
245 noMangling = False 262 noMangling = False
246 if stringID.startswith('='): 263 if stringID.startswith('='):
247 stringID = stringID[1:] 264 stringID = stringID[1:]
248 noMangling = True 265 noMangling = True
249 266
250 if stringID in sourceData: 267 if stringID in sourceData:
251 if noMangling: 268 if noMangling:
252 key = re.sub(r'\W', '_', stringID) 269 key = re.sub(r'\W', '_', stringID)
253 else: 270 else:
254 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 271 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
255 if key in data: 272 if key in data:
256 print 'Warning: locale string %s defined multiple ti mes' % key 273 print 'Warning: locale string %s defined multiple ti mes' % key
257 274
258 # Remove access keys 275 # Remove access keys
259 value = sourceData[stringID] 276 value = get_value(sourceData, stringID)
260 match = re.search(r'^(.*?)\s*\(&.\)$', value) 277 match = re.search(r'^(.*?)\s*\(&.\)$', value)
Sebastian Noack 2017/08/18 07:44:28 The logic to strip the access keys here is actuall
tlucas 2017/08/18 12:59:37 Ok - i will move this to the specific "get_value"
261 if match: 278 if match:
262 value = match.group(1) 279 value = match.group(1)
263 else: 280 else:
264 index = value.find('&') 281 index = value.find('&')
265 if index >= 0: 282 if index >= 0:
266 value = value[0:index] + value[index + 1:] 283 value = value[0:index] + value[index + 1:]
267 data[key] = {'message': value} 284
285 set_translation(data, key, value, sourceData, stringID)
268 except Exception as e: 286 except Exception as e:
269 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) 287 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e)
270 288
271 files[targetFile] = toJson(data) 289 files[targetFile] = toJson(data)
272 290
273 291
274 def truncate(text, length_limit): 292 def truncate(text, length_limit):
275 if len(text) <= length_limit: 293 if len(text) <= length_limit:
276 return text 294 return text
277 return text[:length_limit - 1].rstrip() + u'\u2026' 295 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'): 391 if metadata.has_section('convert_js'):
374 convertJS(params, files) 392 convertJS(params, files)
375 393
376 if metadata.has_section('preprocess'): 394 if metadata.has_section('preprocess'):
377 files.preprocess( 395 files.preprocess(
378 [f for f, _ in metadata.items('preprocess')], 396 [f for f, _ in metadata.items('preprocess')],
379 {'needsExt': True} 397 {'needsExt': True}
380 ) 398 )
381 399
382 if metadata.has_section('import_locales'): 400 if metadata.has_section('import_locales'):
383 importGeckoLocales(params, files) 401 import_locales(params, files)
384 402
385 files['manifest.json'] = createManifest(params, files) 403 files['manifest.json'] = createManifest(params, files)
386 if type == 'chrome': 404 if type == 'chrome':
387 fixTranslationsForCWS(files) 405 fixTranslationsForCWS(files)
388 406
389 if devenv: 407 if devenv:
390 import buildtools 408 import buildtools
391 import random 409 import random
392 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') 410 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js')
393 files['devenvVersion__'] = str(random.random()) 411 files['devenvVersion__'] = str(random.random())
394 412
395 if metadata.has_option('general', 'testScripts'): 413 if metadata.has_option('general', 'testScripts'):
396 files['qunit/index.html'] = createScriptPage( 414 files['qunit/index.html'] = createScriptPage(
397 params, 'testIndex.html.tmpl', ('general', 'testScripts') 415 params, 'testIndex.html.tmpl', ('general', 'testScripts')
398 ) 416 )
399 417
400 zipdata = files.zipToString() 418 zipdata = files.zipToString()
401 signature = None 419 signature = None
402 pubkey = None 420 pubkey = None
403 if keyFile != None: 421 if keyFile != None:
404 signature = signBinary(zipdata, keyFile) 422 signature = signBinary(zipdata, keyFile)
405 pubkey = getPublicKey(keyFile) 423 pubkey = getPublicKey(keyFile)
406 writePackage(outFile, pubkey, signature, zipdata) 424 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