| LEFT | RIGHT |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This Source Code Form is subject to the terms of the Mozilla Public | 3 # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 # License, v. 2.0. If a copy of the MPL was not distributed with this | 4 # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | 6 |
| 7 import re, os, sys, codecs, json, urllib, urllib2 | 7 import re, os, sys, codecs, json, urllib, urllib2 |
| 8 from StringIO import StringIO | 8 from StringIO import StringIO |
| 9 from ConfigParser import SafeConfigParser | 9 from ConfigParser import SafeConfigParser |
| 10 from zipfile import ZipFile | 10 from zipfile import ZipFile |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 265 |
| 266 # Delete description from translations | 266 # Delete description from translations |
| 267 for key, value in parsed.iteritems(): | 267 for key, value in parsed.iteritems(): |
| 268 if "description" in value: | 268 if "description" in value: |
| 269 del value["description"] | 269 del value["description"] |
| 270 | 270 |
| 271 file = codecs.open(path, 'wb', encoding='utf-8') | 271 file = codecs.open(path, 'wb', encoding='utf-8') |
| 272 json.dump(parsed, file, ensure_ascii=False, sort_keys=True, indent=2, separato
rs=(',', ': ')) | 272 json.dump(parsed, file, ensure_ascii=False, sort_keys=True, indent=2, separato
rs=(',', ': ')) |
| 273 file.close() | 273 file.close() |
| 274 | 274 |
| 275 def setupTranslations(projectName, localeConfig, key): | 275 def setupTranslations(localeConfig, projectName, key): |
| 276 # Make a new set from the locales list, mapping to Crowdin friendly format | 276 # Make a new set from the locales list, mapping to Crowdin friendly format |
| 277 locales = {mapLocale(localeConfig['name_format'], locale) | 277 locales = {mapLocale(localeConfig['name_format'], locale) |
| 278 for locale in localeConfig['locales']} | 278 for locale in localeConfig['locales']} |
| 279 | 279 |
| 280 # Fill up with locales that we don't have but the browser supports | 280 # Fill up with locales that we don't have but the browser supports |
| 281 if 'chrome' in localeConfig['target_platforms']: | 281 if 'chrome' in localeConfig['target_platforms']: |
| 282 for locale in chromeLocales: | 282 for locale in chromeLocales: |
| 283 locales.add(mapLocale('ISO-15897', locale)) | 283 locales.add(mapLocale('ISO-15897', locale)) |
| 284 | 284 |
| 285 if 'gecko' in localeConfig['target_platforms']: | 285 if 'gecko' in localeConfig['target_platforms']: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 body += '--%s--\r\n' % boundary | 319 body += '--%s--\r\n' % boundary |
| 320 | 320 |
| 321 body = body.encode('utf-8') | 321 body = body.encode('utf-8') |
| 322 request = urllib2.Request(url, StringIO(body)) | 322 request = urllib2.Request(url, StringIO(body)) |
| 323 request.add_header('Content-Type', 'multipart/form-data; boundary=%s' % bounda
ry) | 323 request.add_header('Content-Type', 'multipart/form-data; boundary=%s' % bounda
ry) |
| 324 request.add_header('Content-Length', len(body)) | 324 request.add_header('Content-Length', len(body)) |
| 325 result = urllib2.urlopen(request).read() | 325 result = urllib2.urlopen(request).read() |
| 326 if result.find('<success') < 0: | 326 if result.find('<success') < 0: |
| 327 raise Exception('Server indicated that the operation was not successful\n' +
result) | 327 raise Exception('Server indicated that the operation was not successful\n' +
result) |
| 328 | 328 |
| 329 def updateTranslationMaster(metadata, dir, projectName, localeConfig, key): | 329 def updateTranslationMaster(localeConfig, metadata, dir, projectName, key): |
| 330 result = json.load(urllib2.urlopen('http://api.crowdin.net/api/project/%s/info
?key=%s&json=1' % (projectName, key))) | 330 result = json.load(urllib2.urlopen('http://api.crowdin.net/api/project/%s/info
?key=%s&json=1' % (projectName, key))) |
| 331 | 331 |
| 332 existing = set(map(lambda f: f['name'], result['files'])) | 332 existing = set(map(lambda f: f['name'], result['files'])) |
| 333 add = [] | 333 add = [] |
| 334 update = [] | 334 update = [] |
| 335 for file in os.listdir(dir): | 335 for file in os.listdir(dir): |
| 336 path = os.path.join(dir, file) | 336 path = os.path.join(dir, file) |
| 337 if os.path.isfile(path): | 337 if os.path.isfile(path): |
| 338 if localeConfig['file_format'] == 'chrome-json' and file.endswith('.json')
: | 338 if localeConfig['file_format'] == 'chrome-json' and file.endswith('.json')
: |
| 339 data = preprocessChromeLocale(path, metadata, True) | 339 data = preprocessChromeLocale(path, metadata, True) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 357 if len(add): | 357 if len(add): |
| 358 titles = urllib.urlencode([('titles[%s]' % name, re.sub(r'\.json', '', name)
) for name, data in add]) | 358 titles = urllib.urlencode([('titles[%s]' % name, re.sub(r'\.json', '', name)
) for name, data in add]) |
| 359 postFiles(add, 'http://api.crowdin.net/api/project/%s/add-file?key=%s&type=c
hrome&%s' % (projectName, key, titles)) | 359 postFiles(add, 'http://api.crowdin.net/api/project/%s/add-file?key=%s&type=c
hrome&%s' % (projectName, key, titles)) |
| 360 if len(update): | 360 if len(update): |
| 361 postFiles(update, 'http://api.crowdin.net/api/project/%s/update-file?key=%s'
% (projectName, key)) | 361 postFiles(update, 'http://api.crowdin.net/api/project/%s/update-file?key=%s'
% (projectName, key)) |
| 362 for file in existing: | 362 for file in existing: |
| 363 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/delete-file?
key=%s&file=%s' % (projectName, key, file)).read() | 363 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/delete-file?
key=%s&file=%s' % (projectName, key, file)).read() |
| 364 if result.find('<success') < 0: | 364 if result.find('<success') < 0: |
| 365 raise Exception('Server indicated that the operation was not successful\n'
+ result) | 365 raise Exception('Server indicated that the operation was not successful\n'
+ result) |
| 366 | 366 |
| 367 def uploadTranslations(metadata, dir, locale, projectName, localeConfig, key): | 367 def uploadTranslations(localeConfig, metadata, dir, locale, projectName, key): |
| 368 files = [] | 368 files = [] |
| 369 for file in os.listdir(dir): | 369 for file in os.listdir(dir): |
| 370 path = os.path.join(dir, file) | 370 path = os.path.join(dir, file) |
| 371 if os.path.isfile(path): | 371 if os.path.isfile(path): |
| 372 if localeConfig['file_format'] == 'chrome-json' and file.endswith('.json')
: | 372 if localeConfig['file_format'] == 'chrome-json' and file.endswith('.json')
: |
| 373 data = preprocessChromeLocale(path, metadata, False) | 373 data = preprocessChromeLocale(path, metadata, False) |
| 374 newName = file | 374 newName = file |
| 375 elif localeConfig['file_format'] == 'chrome-json': | 375 elif localeConfig['file_format'] == 'chrome-json': |
| 376 fileHandle = codecs.open(path, 'rb', encoding='utf-8') | 376 fileHandle = codecs.open(path, 'rb', encoding='utf-8') |
| 377 data = json.dumps({file: {'message': fileHandle.read()}}) | 377 data = json.dumps({file: {'message': fileHandle.read()}}) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 389 ) | 389 ) |
| 390 | 390 |
| 391 def getTranslations(localeConfig, projectName, key): | 391 def getTranslations(localeConfig, projectName, key): |
| 392 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/export?key=%s'
% (projectName, key)).read() | 392 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/export?key=%s'
% (projectName, key)).read() |
| 393 if result.find('<success') < 0: | 393 if result.find('<success') < 0: |
| 394 raise Exception('Server indicated that the operation was not successful\n' +
result) | 394 raise Exception('Server indicated that the operation was not successful\n' +
result) |
| 395 | 395 |
| 396 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/download/all.z
ip?key=%s' % (projectName, key)).read() | 396 result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/download/all.z
ip?key=%s' % (projectName, key)).read() |
| 397 zip = ZipFile(StringIO(result)) | 397 zip = ZipFile(StringIO(result)) |
| 398 dirs = {} | 398 dirs = {} |
| 399 |
| 400 normalizedDefaultLocale = localeConfig['default_locale'] |
| 401 if localeConfig['name_format'] == 'ISO-15897': |
| 402 normalizedDefaultLocale = normalizedDefaultLocale.replace('_', '-') |
| 403 normalizedDefaultLocale = mapLocale(localeConfig['name_format'], |
| 404 normalizedDefaultLocale) |
| 405 |
| 399 for info in zip.infolist(): | 406 for info in zip.infolist(): |
| 400 if not info.filename.endswith('.json'): | 407 if not info.filename.endswith('.json'): |
| 401 continue | 408 continue |
| 402 | 409 |
| 403 dir, file = os.path.split(info.filename) | 410 dir, file = os.path.split(info.filename) |
| 404 if not re.match(r'^[\w\-]+$', dir) or dir == localeConfig['default_locale']: | 411 if not re.match(r'^[\w\-]+$', dir) or dir == normalizedDefaultLocale: |
| 405 continue | 412 continue |
| 406 if localeConfig['file_format'] == 'chrome-json' and file.count('.') == 1: | 413 if localeConfig['file_format'] == 'chrome-json' and file.count('.') == 1: |
| 407 origFile = file | 414 origFile = file |
| 408 else: | 415 else: |
| 409 origFile = re.sub(r'\.json$', '', file) | 416 origFile = re.sub(r'\.json$', '', file) |
| 410 if (localeConfig['file_format'] == 'gecko-dtd' and | 417 if (localeConfig['file_format'] == 'gecko-dtd' and |
| 411 not origFile.endswith('.dtd') and | 418 not origFile.endswith('.dtd') and |
| 412 not origFile.endswith('.properties')): | 419 not origFile.endswith('.properties')): |
| 413 continue | 420 continue |
| 414 | 421 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 454 |
| 448 # Remove any extra files | 455 # Remove any extra files |
| 449 for dir, files in dirs.iteritems(): | 456 for dir, files in dirs.iteritems(): |
| 450 baseDir = os.path.join(localeConfig['base_path'], dir) | 457 baseDir = os.path.join(localeConfig['base_path'], dir) |
| 451 if not os.path.exists(baseDir): | 458 if not os.path.exists(baseDir): |
| 452 continue | 459 continue |
| 453 for file in os.listdir(baseDir): | 460 for file in os.listdir(baseDir): |
| 454 path = os.path.join(baseDir, file) | 461 path = os.path.join(baseDir, file) |
| 455 if os.path.isfile(path) and (file.endswith('.json') or file.endswith('.pro
perties') or file.endswith('.dtd')) and not file in files: | 462 if os.path.isfile(path) and (file.endswith('.json') or file.endswith('.pro
perties') or file.endswith('.dtd')) and not file in files: |
| 456 os.remove(path) | 463 os.remove(path) |
| LEFT | RIGHT |