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: Created Aug. 22, 2017, 7:59 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_string_webext(data, key, source):
194 """Import a single translation from the source dictionary into data"""
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.
230 if sourceFile.endswith('.json'): 256 if sourceFile.endswith('.json'):
231 with io.open(sourceFile, 'r', encoding='utf-8') as handle: 257 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
232 sourceData = {k: v['message'] for k, v in json.load(hand le).iteritems()} 258 sourceData = json.load(handle)
259 import_string = import_string_webext
233 else: 260 else:
234 sourceData = localeTools.readFile(sourceFile) 261 sourceData = localeTools.readFile(sourceFile)
262 import_string = import_string_gecko
235 263
236 # Resolve wildcard imports 264 # Resolve wildcard imports
237 if keys == '*' or keys == '=*': 265 if keys == '*' or keys == '=*':
238 importList = sourceData.keys() 266 importList = sourceData.keys()
239 importList = filter(lambda k: not k.startswith('_'), importL ist) 267 importList = filter(lambda k: not k.startswith('_'), importL ist)
240 if keys == '=*': 268 if keys == '=*':
241 importList = map(lambda k: '=' + k, importList) 269 importList = map(lambda k: '=' + k, importList)
242 keys = ' '.join(importList) 270 keys = ' '.join(importList)
243 271
244 for stringID in keys.split(): 272 for stringID in keys.split():
245 noMangling = False 273 noMangling = False
246 if stringID.startswith('='): 274 if stringID.startswith('='):
247 stringID = stringID[1:] 275 stringID = stringID[1:]
248 noMangling = True 276 noMangling = True
249 277
250 if stringID in sourceData: 278 if stringID in sourceData:
251 if noMangling: 279 if noMangling:
252 key = re.sub(r'\W', '_', stringID) 280 key = re.sub(r'\W', '_', stringID)
253 else: 281 else:
254 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
255 if key in data: 283 if key in data:
256 print 'Warning: locale string %s defined multiple ti mes' % key 284 print 'Warning: locale string %s defined multiple ti mes' % key
257 285
258 # Remove access keys 286 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: 287 except Exception as e:
269 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) 288 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e)
270 289
271 files[targetFile] = toJson(data) 290 files[targetFile] = toJson(data)
272 291
273 292
274 def truncate(text, length_limit): 293 def truncate(text, length_limit):
275 if len(text) <= length_limit: 294 if len(text) <= length_limit:
276 return text 295 return text
277 return text[:length_limit - 1].rstrip() + u'\u2026' 296 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'): 392 if metadata.has_section('convert_js'):
374 convertJS(params, files) 393 convertJS(params, files)
375 394
376 if metadata.has_section('preprocess'): 395 if metadata.has_section('preprocess'):
377 files.preprocess( 396 files.preprocess(
378 [f for f, _ in metadata.items('preprocess')], 397 [f for f, _ in metadata.items('preprocess')],
379 {'needsExt': True} 398 {'needsExt': True}
380 ) 399 )
381 400
382 if metadata.has_section('import_locales'): 401 if metadata.has_section('import_locales'):
383 importGeckoLocales(params, files) 402 import_locales(params, files)
384 403
385 files['manifest.json'] = createManifest(params, files) 404 files['manifest.json'] = createManifest(params, files)
386 if type == 'chrome': 405 if type == 'chrome':
387 fixTranslationsForCWS(files) 406 fixTranslationsForCWS(files)
388 407
389 if devenv: 408 if devenv:
390 import buildtools 409 import buildtools
391 import random 410 import random
392 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') 411 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js')
393 files['devenvVersion__'] = str(random.random()) 412 files['devenvVersion__'] = str(random.random())
394 413
395 if metadata.has_option('general', 'testScripts'): 414 if metadata.has_option('general', 'testScripts'):
396 files['qunit/index.html'] = createScriptPage( 415 files['qunit/index.html'] = createScriptPage(
397 params, 'testIndex.html.tmpl', ('general', 'testScripts') 416 params, 'testIndex.html.tmpl', ('general', 'testScripts')
398 ) 417 )
399 418
400 zipdata = files.zipToString() 419 zipdata = files.zipToString()
401 signature = None 420 signature = None
402 pubkey = None 421 pubkey = None
403 if keyFile != None: 422 if keyFile != None:
404 signature = signBinary(zipdata, keyFile) 423 signature = signBinary(zipdata, keyFile)
405 pubkey = getPublicKey(keyFile) 424 pubkey = getPublicKey(keyFile)
406 writePackage(outFile, pubkey, signature, zipdata) 425 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