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: Further simplifying Created Aug. 18, 2017, 2:34 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_translation_and_additional_info(data, key, 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
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'] 196
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 197
198 data[key] = {'messages': value} 198 def import_string_gecko(data, key, value):
199 for k, v in source[stringID].items(): 199 """Import Gecko-style locales into data.
200 data[key].setdefault(k, v) 200
201 201 Only sets {'message': value} in the data-dictionary, after stripping
202 202 undesired Gecko-style access keys.
203 def set_translation_without_access_keys(data, key, source, stringID): 203 """
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) 204 match = re.search(r'^(.*?)\s*\(&.\)$', value)
211 if match: 205 if match:
212 value = match.group(1) 206 value = match.group(1)
213 else: 207 else:
214 index = value.find('&') 208 index = value.find('&')
215 if index >= 0: 209 if index >= 0:
216 value = value[0:index] + value[index + 1:] 210 value = value[0:index] + value[index + 1:]
217 211
218 data[key] = {'message': value} 212 data[key] = {'message': value}
219 213
(...skipping 28 matching lines...) Expand all
248 fileName, keys = item 242 fileName, keys = item
249 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) 243 parts = map(lambda n: source if n == '*' else n, fileName.split('/') )
250 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 244 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
251 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') 245 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete')
252 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): 246 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ):
253 continue 247 continue
254 248
255 data = json.loads(files[targetFile].decode('utf-8')) 249 data = json.loads(files[targetFile].decode('utf-8'))
256 250
257 try: 251 try:
258 # .json and other formats provide translations differently and 252 # The WebExtensions (.json) and Gecko format provide
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". 253 # translations differently and/or provide additional
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 254 # information like e.g. "placeholders". We want to adhere to
255 # that and preserve the addtional info.
261 if sourceFile.endswith('.json'): 256 if sourceFile.endswith('.json'):
262 with io.open(sourceFile, 'r', encoding='utf-8') as handle: 257 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
263 sourceData = json.load(handle) 258 sourceData = json.load(handle)
264 259 import_string = import_string_webext
265 set_translation = set_translation_and_additional_info
266 else: 260 else:
267 sourceData = localeTools.readFile(sourceFile) 261 sourceData = localeTools.readFile(sourceFile)
268 262 import_string = import_string_gecko
269 set_translation = set_translation_without_access_keys
270 263
271 # Resolve wildcard imports 264 # Resolve wildcard imports
272 if keys == '*' or keys == '=*': 265 if keys == '*' or keys == '=*':
273 importList = sourceData.keys() 266 importList = sourceData.keys()
274 importList = filter(lambda k: not k.startswith('_'), importL ist) 267 importList = filter(lambda k: not k.startswith('_'), importL ist)
275 if keys == '=*': 268 if keys == '=*':
276 importList = map(lambda k: '=' + k, importList) 269 importList = map(lambda k: '=' + k, importList)
277 keys = ' '.join(importList) 270 keys = ' '.join(importList)
278 271
279 for stringID in keys.split(): 272 for stringID in keys.split():
280 noMangling = False 273 noMangling = False
281 if stringID.startswith('='): 274 if stringID.startswith('='):
282 stringID = stringID[1:] 275 stringID = stringID[1:]
283 noMangling = True 276 noMangling = True
284 277
285 if stringID in sourceData: 278 if stringID in sourceData:
286 if noMangling: 279 if noMangling:
287 key = re.sub(r'\W', '_', stringID) 280 key = re.sub(r'\W', '_', stringID)
288 else: 281 else:
289 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
290 if key in data: 283 if key in data:
291 print 'Warning: locale string %s defined multiple ti mes' % key 284 print 'Warning: locale string %s defined multiple ti mes' % key
292 285
293 set_translation(data, key, sourceData, stringID) 286 import_string(data, key, sourceData[stringID])
294 except Exception as e: 287 except Exception as e:
295 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)
296 289
297 files[targetFile] = toJson(data) 290 files[targetFile] = toJson(data)
298 291
299 292
300 def truncate(text, length_limit): 293 def truncate(text, length_limit):
301 if len(text) <= length_limit: 294 if len(text) <= length_limit:
302 return text 295 return text
303 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
423 params, 'testIndex.html.tmpl', ('general', 'testScripts') 416 params, 'testIndex.html.tmpl', ('general', 'testScripts')
424 ) 417 )
425 418
426 zipdata = files.zipToString() 419 zipdata = files.zipToString()
427 signature = None 420 signature = None
428 pubkey = None 421 pubkey = None
429 if keyFile != None: 422 if keyFile != None:
430 signature = signBinary(zipdata, keyFile) 423 signature = signBinary(zipdata, keyFile)
431 pubkey = getPublicKey(keyFile) 424 pubkey = getPublicKey(keyFile)
432 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