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