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

Delta Between Two Patch Sets: packagerChrome.py

Issue 29517660: Issue 5477 - Import everything from imported locales (Closed)
Left Patch Set: Created Aug. 16, 2017, 6:28 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | packagerEdge.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 set_full_dict(data, key, value, source, stringID): 193 def import_string_webext(data, key, source):
194 """Sets data[key] to the desired {'message':value}, adds all additional 194 """Import a single translation from the source dictionary into data"""
195 info from the source dict""" 195 data[key] = source
196 data[key] = {'messages': value} 196
197 for k, v in source[stringID].items(): 197
198 data[key].setdefault(k, v) 198 def import_string_gecko(data, key, value):
199 199 """Import Gecko-style locales into data.
200 200
201 def set_message_only(data, key, value, *args): 201 Only sets {'message': value} in the data-dictionary, after stripping
202 """only set {'message': value} in data-dictionary, API-consitent with 202 undesired Gecko-style access keys.
203 set_full_dict()""" 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
204 data[key] = {'message': value} 212 data[key] = {'message': value}
205 213
206 214
207 def import_locales(params, files): 215 def import_locales(params, files):
208 import localeTools 216 import localeTools
209 217
210 # 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
211 # separator instead. 219 # separator instead.
212 convert_locale_code = lambda code: code.replace('-', '_') 220 convert_locale_code = lambda code: code.replace('-', '_')
213 221
(...skipping 20 matching lines...) Expand all
234 fileName, keys = item 242 fileName, keys = item
235 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) 243 parts = map(lambda n: source if n == '*' else n, fileName.split('/') )
236 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 244 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
237 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') 245 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete')
238 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): 246 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ):
239 continue 247 continue
240 248
241 data = json.loads(files[targetFile].decode('utf-8')) 249 data = json.loads(files[targetFile].decode('utf-8'))
242 250
243 try: 251 try:
244 # .json and other formats provide translations differently and 252 # The WebExtensions (.json) and Gecko format provide
245 # / or provide additional information like e.g. "placeholders". 253 # translations differently and/or provide additional
246 # We want to adhere to that / preserve the addtional info 254 # information like e.g. "placeholders". We want to adhere to
255 # that and preserve the addtional info.
247 if sourceFile.endswith('.json'): 256 if sourceFile.endswith('.json'):
248 with io.open(sourceFile, 'r', encoding='utf-8') as handle: 257 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
249 sourceData = json.load(handle) 258 sourceData = json.load(handle)
250 259 import_string = import_string_webext
251 get_value = lambda x: x[stringID]['message']
Sebastian Noack 2017/08/17 16:32:34 PEP-8 discourages assignment of lambda functions t
tlucas 2017/08/18 07:34:15 Done.
252 set_translation = set_full_dict
253 else: 260 else:
254 sourceData = localeTools.readFile(sourceFile) 261 sourceData = localeTools.readFile(sourceFile)
255 get_value = lambda x: x[stringID] 262 import_string = import_string_gecko
256 set_translation = set_message_only
257 263
258 # Resolve wildcard imports 264 # Resolve wildcard imports
259 if keys == '*' or keys == '=*': 265 if keys == '*' or keys == '=*':
260 importList = sourceData.keys() 266 importList = sourceData.keys()
261 importList = filter(lambda k: not k.startswith('_'), importL ist) 267 importList = filter(lambda k: not k.startswith('_'), importL ist)
262 if keys == '=*': 268 if keys == '=*':
263 importList = map(lambda k: '=' + k, importList) 269 importList = map(lambda k: '=' + k, importList)
264 keys = ' '.join(importList) 270 keys = ' '.join(importList)
265 271
266 for stringID in keys.split(): 272 for stringID in keys.split():
267 noMangling = False 273 noMangling = False
268 if stringID.startswith('='): 274 if stringID.startswith('='):
269 stringID = stringID[1:] 275 stringID = stringID[1:]
270 noMangling = True 276 noMangling = True
271 277
272 if stringID in sourceData: 278 if stringID in sourceData:
273 if noMangling: 279 if noMangling:
274 key = re.sub(r'\W', '_', stringID) 280 key = re.sub(r'\W', '_', stringID)
275 else: 281 else:
276 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
277 if key in data: 283 if key in data:
278 print 'Warning: locale string %s defined multiple ti mes' % key 284 print 'Warning: locale string %s defined multiple ti mes' % key
279 285
280 # Remove access keys 286 import_string(data, key, sourceData[stringID])
281 value = get_value(sourceData)
282 match = re.search(r'^(.*?)\s*\(&.\)$', value)
283 if match:
284 value = match.group(1)
285 else:
286 index = value.find('&')
287 if index >= 0:
288 value = value[0:index] + value[index + 1:]
289
290 set_translation(data, key, value, sourceData, stringID)
291 except Exception as e: 287 except Exception as e:
292 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)
293 289
294 files[targetFile] = toJson(data) 290 files[targetFile] = toJson(data)
295 291
296 292
297 def truncate(text, length_limit): 293 def truncate(text, length_limit):
298 if len(text) <= length_limit: 294 if len(text) <= length_limit:
299 return text 295 return text
300 return text[:length_limit - 1].rstrip() + u'\u2026' 296 return text[:length_limit - 1].rstrip() + u'\u2026'
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 params, 'testIndex.html.tmpl', ('general', 'testScripts') 416 params, 'testIndex.html.tmpl', ('general', 'testScripts')
421 ) 417 )
422 418
423 zipdata = files.zipToString() 419 zipdata = files.zipToString()
424 signature = None 420 signature = None
425 pubkey = None 421 pubkey = None
426 if keyFile != None: 422 if keyFile != None:
427 signature = signBinary(zipdata, keyFile) 423 signature = signBinary(zipdata, keyFile)
428 pubkey = getPublicKey(keyFile) 424 pubkey = getPublicKey(keyFile)
429 writePackage(outFile, pubkey, signature, zipdata) 425 writePackage(outFile, pubkey, signature, zipdata)
LEFTRIGHT
« no previous file | packagerEdge.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld