| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 logger = logging.getLogger("cms.bin.translate") | 34 logger = logging.getLogger("cms.bin.translate") |
| 35 | 35 |
| 36 class CrowdinAPI: | 36 class CrowdinAPI: |
| 37 FILES_PER_REQUEST = 20 | 37 FILES_PER_REQUEST = 20 |
| 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, **kwargs): | 44 def raw_request(self, request_method, api_endpoint, query_params=(), **kwargs) : |
|
Wladimir Palant
2015/07/16 12:17:23
Nit: Have query_params default to [] and remove it
Sebastian Noack
2015/07/16 12:23:56
Mutable objects in default arguments are a footgun
kzar
2015/07/16 12:49:32
I remember that one from the interview, which is w
| |
| 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 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 crowdin_api.request("POST", "delete-directory", data={"name": directory}) | 217 crowdin_api.request("POST", "delete-directory", data={"name": directory}) |
| 218 | 218 |
| 219 def download_translations(crowdin_api, source_dir, required_locales): | 219 def download_translations(crowdin_api, source_dir, required_locales): |
| 220 logger.info("Requesting generation of fresh translations archive...") | 220 logger.info("Requesting generation of fresh translations archive...") |
| 221 result = crowdin_api.request("GET", "export") | 221 result = crowdin_api.request("GET", "export") |
| 222 if result.get("success", {}).get("status") == "skipped": | 222 if result.get("success", {}).get("status") == "skipped": |
| 223 logger.warning("Archive generation skipped, either " | 223 logger.warning("Archive generation skipped, either " |
| 224 "no changes or API usage excessive") | 224 "no changes or API usage excessive") |
| 225 | 225 |
| 226 logger.info("Downloading translations archive...") | 226 logger.info("Downloading translations archive...") |
| 227 response = crowdin_api.raw_request("GET", "download/all.zip", []) | 227 response = crowdin_api.raw_request("GET", "download/all.zip") |
| 228 | 228 |
| 229 logger.info("Extracting translations archive...") | 229 logger.info("Extracting translations archive...") |
| 230 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive: | 230 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive: |
| 231 locale_path = os.path.join(source_dir, "locales") | 231 locale_path = os.path.join(source_dir, "locales") |
| 232 # First clear existing translation files | 232 # First clear existing translation files |
| 233 for root, dirs, files in os.walk(locale_path, topdown=True): | 233 for root, dirs, files in os.walk(locale_path, topdown=True): |
| 234 if root == locale_path: | 234 if root == locale_path: |
| 235 dirs[:] = [d for d in dirs if d in required_locales] | 235 dirs[:] = [d for d in dirs if d in required_locales] |
| 236 for f in files: | 236 for f in files: |
| 237 if f.lower().endswith(".json"): | 237 if f.lower().endswith(".json"): |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 if __name__ == "__main__": | 291 if __name__ == "__main__": |
| 292 if len(sys.argv) < 3: | 292 if len(sys.argv) < 3: |
| 293 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" | 293 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" |
| 294 sys.exit(1) | 294 sys.exit(1) |
| 295 | 295 |
| 296 logging.basicConfig() | 296 logging.basicConfig() |
| 297 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) | 297 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) |
| 298 | 298 |
| 299 source_dir, crowdin_api_key = sys.argv[1:3] | 299 source_dir, crowdin_api_key = sys.argv[1:3] |
| 300 crowdin_sync(source_dir, crowdin_api_key) | 300 crowdin_sync(source_dir, crowdin_api_key) |
| LEFT | RIGHT |