| 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 167 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 |