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

Side by Side Diff: translations.py

Issue 29323730: Issue 2752 - Automate translation download (Closed)
Patch Set: Created Aug. 16, 2015, 9:04 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 | « ensure_dependencies.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import sys
6 import urllib2
7 from StringIO import StringIO
8 from zipfile import ZipFile
9
10 import buildtools.localeTools
11
12 BASE_PATH = os.path.dirname(os.path.abspath(__file__))
13 LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales",
14 "adblockbrowser")
15 PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid"
16
17 def print_usage():
18 print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0])
19
20 def get_translations(api_key):
21 # We could use buildtools.localeTools.getTranslations here if we were
22 # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it.
23 # See https://issues.adblockplus.org/2911.
24
25 result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key))
26 if result.read().find("<success") < 0:
27 raise Exception("Export failed: " + result)
28
29 result = urllib2.urlopen("%s/download/all.zip?key=%s" % \
30 (PROJECT_URL, api_key))
31 zip = ZipFile(StringIO(result.read()))
32 for info in zip.infolist():
33 if not info.filename.endswith(".dtd"):
34 continue
35
36 dir, file = os.path.split(info.filename)
37 if not re.match(r"^[\w\-]+$", dir) or dir == "en-US":
38 continue
39
40 mapping = buildtools.localeTools.langMappingGecko
41 for key, value in mapping.iteritems():
42 if value == dir:
43 dir = key
44
45 data = zip.open(info.filename).read()
46 if not data:
47 continue
48
49 path = os.path.join(LOCALES_PATH, dir, file)
50 if not os.path.exists(os.path.dirname(path)):
51 os.makedirs(os.path.dirname(path))
52 with open(path, "w") as file:
53 file.write(data)
54
55 if __name__ == "__main__":
56 if len(sys.argv) < 3:
57 print_usage()
58 sys.exit(1)
59
60 command, api_key = sys.argv[1:]
61 if command != "get":
62 print_usage()
63 sys.exit(2)
64
65 get_translations(api_key)
OLDNEW
« no previous file with comments | « ensure_dependencies.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld