OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 def __init__(self, api_key, project_name): | 39 def __init__(self, api_key, project_name): |
40 self.api_key = api_key | 40 self.api_key = api_key |
41 self.project_name = project_name | 41 self.project_name = project_name |
42 self.connection = urllib3.connection_from_url('https://api.crowdin.com/'
) | 42 self.connection = urllib3.connection_from_url('https://api.crowdin.com/'
) |
43 | 43 |
44 def raw_request(self, request_method, api_endpoint, query_params=(), **kwarg
s): | 44 def raw_request(self, request_method, api_endpoint, query_params=(), **kwarg
s): |
45 url = '/api/project/%s/%s?%s' % ( | 45 url = '/api/project/%s/%s?%s' % ( |
46 urllib.quote(self.project_name), | 46 urllib.quote(self.project_name), |
47 urllib.quote(api_endpoint), | 47 urllib.quote(api_endpoint), |
48 urllib.urlencode((('key', self.api_key),) + query_params) | 48 urllib.urlencode((('key', self.api_key),) + query_params), |
49 ) | 49 ) |
50 try: | 50 try: |
51 response = self.connection.request( | 51 response = self.connection.request( |
52 request_method, str(url), **kwargs | 52 request_method, str(url), **kwargs, |
53 ) | 53 ) |
54 except urllib3.exceptions.HTTPError: | 54 except urllib3.exceptions.HTTPError: |
55 logger.error('Connection to API endpoint %s failed', url) | 55 logger.error('Connection to API endpoint %s failed', url) |
56 raise | 56 raise |
57 if response.status < 200 or response.status >= 300: | 57 if response.status < 200 or response.status >= 300: |
58 logger.error('API call to %s failed:\n%s', url, response.data) | 58 logger.error('API call to %s failed:\n%s', url, response.data) |
59 raise urllib3.exceptions.HTTPError(response.status) | 59 raise urllib3.exceptions.HTTPError(response.status) |
60 return response | 60 return response |
61 | 61 |
62 def request(self, request_method, api_endpoint, data=None, files=None): | 62 def request(self, request_method, api_endpoint, data=None, files=None): |
63 fields = [] | 63 fields = [] |
64 if data: | 64 if data: |
65 for name, value in data.iteritems(): | 65 for name, value in data.iteritems(): |
66 if isinstance(value, basestring): | 66 if isinstance(value, basestring): |
67 fields.append((name, value)) | 67 fields.append((name, value)) |
68 else: | 68 else: |
69 fields.extend((name + '[]', v) for v in value) | 69 fields.extend((name + '[]', v) for v in value) |
70 if files: | 70 if files: |
71 fields.extend(('files[%s]' % f[0], f) for f in files) | 71 fields.extend(('files[%s]' % f[0], f) for f in files) |
72 | 72 |
73 response = self.raw_request( | 73 response = self.raw_request( |
74 request_method, api_endpoint, (('json', '1'),), | 74 request_method, api_endpoint, (('json', '1'),), |
75 fields=fields, preload_content=False | 75 fields=fields, preload_content=False, |
76 ) | 76 ) |
77 | 77 |
78 try: | 78 try: |
79 return json.load(response) | 79 return json.load(response) |
80 except ValueError: | 80 except ValueError: |
81 logger.error('Invalid response returned by API endpoint %s', url) | 81 logger.error('Invalid response returned by API endpoint %s', url) |
82 raise | 82 raise |
83 | 83 |
84 | 84 |
85 def grouper(iterable, n): | 85 def grouper(iterable, n): |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 required_locales[locale] = crowdin_locale | 141 required_locales[locale] = crowdin_locale |
142 else: | 142 else: |
143 logger.warning("Ignoring locale '%s', which Crowdin doesn't support"
, | 143 logger.warning("Ignoring locale '%s', which Crowdin doesn't support"
, |
144 locale) | 144 locale) |
145 | 145 |
146 required_crowdin_locales = set(required_locales.values()) | 146 required_crowdin_locales = set(required_locales.values()) |
147 if not required_crowdin_locales.issubset(enabled_locales): | 147 if not required_crowdin_locales.issubset(enabled_locales): |
148 logger.info('Enabling the required locales for the Crowdin project...') | 148 logger.info('Enabling the required locales for the Crowdin project...') |
149 crowdin_api.request( | 149 crowdin_api.request( |
150 'POST', 'edit-project', | 150 'POST', 'edit-project', |
151 data={'languages': enabled_locales | required_crowdin_locales} | 151 data={'languages': enabled_locales | required_crowdin_locales}, |
152 ) | 152 ) |
153 | 153 |
154 return required_locales | 154 return required_locales |
155 | 155 |
156 | 156 |
157 def list_remote_files(project_info): | 157 def list_remote_files(project_info): |
158 def parse_file_node(node, path=''): | 158 def parse_file_node(node, path=''): |
159 if node['node_type'] == 'file': | 159 if node['node_type'] == 'file': |
160 remote_files.add(path + node['name']) | 160 remote_files.add(path + node['name']) |
161 elif node['node_type'] == 'directory': | 161 elif node['node_type'] == 'directory': |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 os.remove(os.path.join(root, f)) | 264 os.remove(os.path.join(root, f)) |
265 # Then extract the new ones in place | 265 # Then extract the new ones in place |
266 for member in archive.namelist(): | 266 for member in archive.namelist(): |
267 path, file_name = posixpath.split(member) | 267 path, file_name = posixpath.split(member) |
268 ext = posixpath.splitext(file_name)[1] | 268 ext = posixpath.splitext(file_name)[1] |
269 path_parts = path.split(posixpath.sep) | 269 path_parts = path.split(posixpath.sep) |
270 locale, file_path = path_parts[0], path_parts[1:] | 270 locale, file_path = path_parts[0], path_parts[1:] |
271 if ext.lower() == '.json' and locale in inverted_required_locales: | 271 if ext.lower() == '.json' and locale in inverted_required_locales: |
272 output_path = os.path.join( | 272 output_path = os.path.join( |
273 locale_path, inverted_required_locales[locale], | 273 locale_path, inverted_required_locales[locale], |
274 *file_path + [file_name] | 274 *file_path + [file_name], |
275 ) | 275 ) |
276 with archive.open(member) as source_file: | 276 with archive.open(member) as source_file: |
277 locale_file_contents = json.load(source_file) | 277 locale_file_contents = json.load(source_file) |
278 if len(locale_file_contents): | 278 if len(locale_file_contents): |
279 with codecs.open(output_path, 'wb', 'utf-8') as target_f
ile: | 279 with codecs.open(output_path, 'wb', 'utf-8') as target_f
ile: |
280 json.dump(locale_file_contents, target_file, ensure_
ascii=False, | 280 json.dump(locale_file_contents, target_file, ensure_
ascii=False, |
281 sort_keys=True, indent=2, separators=(',',
': ')) | 281 sort_keys=True, indent=2, separators=(',',
': ')) |
282 | 282 |
283 | 283 |
284 def crowdin_sync(source_dir, crowdin_api_key): | 284 def crowdin_sync(source_dir, crowdin_api_key): |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 if __name__ == '__main__': | 329 if __name__ == '__main__': |
330 if len(sys.argv) < 3: | 330 if len(sys.argv) < 3: |
331 print >>sys.stderr, 'Usage: python -m cms.bin.translate www_directory cr
owdin_project_api_key [logging_level]' | 331 print >>sys.stderr, 'Usage: python -m cms.bin.translate www_directory cr
owdin_project_api_key [logging_level]' |
332 sys.exit(1) | 332 sys.exit(1) |
333 | 333 |
334 logging.basicConfig() | 334 logging.basicConfig() |
335 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) | 335 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) |
336 | 336 |
337 source_dir, crowdin_api_key = sys.argv[1:3] | 337 source_dir, crowdin_api_key = sys.argv[1:3] |
338 crowdin_sync(source_dir, crowdin_api_key) | 338 crowdin_sync(source_dir, crowdin_api_key) |
OLD | NEW |