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

Delta Between Two Patch Sets: cms/bin/translate.py

Issue 29328085: Issue 3076 - [CMS] Map locale names to match Crowdin's expectations (Closed)
Left Patch Set: Created Sept. 16, 2015, 4:27 p.m.
Right Patch Set: Addressed nits Created Sept. 17, 2015, 2:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 def configure_locales(crowdin_api, local_locales, enabled_locales, 121 def configure_locales(crowdin_api, local_locales, enabled_locales,
122 defaultlocale): 122 defaultlocale):
123 logger.info("Checking which locales are supported by Crowdin...") 123 logger.info("Checking which locales are supported by Crowdin...")
124 response = crowdin_api.request("GET", "supported-languages") 124 response = crowdin_api.request("GET", "supported-languages")
125 125
126 supported_locales = {l["crowdin_code"] for l in response} 126 supported_locales = {l["crowdin_code"] for l in response}
127 127
128 # We need to map the locale names we use to the ones that Crowdin is expecting 128 # We need to map the locale names we use to the ones that Crowdin is expecting
129 # and at the same time ensure that they are supported. 129 # and at the same time ensure that they are supported.
130 required_locales = {} 130 required_locales = {}
131 skipped_locales = []
132 for locale in local_locales: 131 for locale in local_locales:
133 if "_" in locale: 132 if "_" in locale:
Sebastian Noack 2015/09/16 19:37:37 This can be simplified: if "_" in locale: crowd
kzar 2015/09/16 20:02:41 Yea, I did it kinda like that initially but then i
Sebastian Noack 2015/09/17 09:45:14 Complexity-wise you have two |in supported_locales
kzar 2015/09/17 10:16:16 Done.
134 crowdin_locale = locale.replace("_", "-") 133 crowdin_locale = locale.replace("_", "-")
134 elif locale in supported_locales:
135 crowdin_locale = locale
135 else: 136 else:
136 if locale in supported_locales:
137 required_locales[locale] = locale
138 continue
139 crowdin_locale = "%s-%s" % (locale, locale.upper()) 137 crowdin_locale = "%s-%s" % (locale, locale.upper())
140 138
141 if crowdin_locale in supported_locales: 139 if crowdin_locale in supported_locales:
142 required_locales[locale] = crowdin_locale 140 required_locales[locale] = crowdin_locale
143 else: 141 else:
144 skipped_locales.append(locale) 142 logger.warning("Ignoring locale '%s', which Crowdin doesn't support",
145 143 locale)
146 if skipped_locales:
147 logger.warning("Ignoring locales that Crowdin doesn't support: %s",
148 ", ".join(skipped_locales))
149 144
150 required_crowdin_locales = set(required_locales.values()) 145 required_crowdin_locales = set(required_locales.values())
151 if not required_crowdin_locales.issubset(enabled_locales): 146 if not required_crowdin_locales.issubset(enabled_locales):
152 logger.info("Enabling the required locales for the Crowdin project...") 147 logger.info("Enabling the required locales for the Crowdin project...")
153 crowdin_api.request( 148 crowdin_api.request(
154 "POST", "edit-project", 149 "POST", "edit-project",
155 data={"languages": enabled_locales | required_crowdin_locales} 150 data={"languages": enabled_locales | required_crowdin_locales}
156 ) 151 )
157 152
158 return required_locales 153 return required_locales
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 for member in archive.namelist(): 255 for member in archive.namelist():
261 path, file_name = posixpath.split(member) 256 path, file_name = posixpath.split(member)
262 ext = posixpath.splitext(file_name)[1] 257 ext = posixpath.splitext(file_name)[1]
263 path_parts = path.split(posixpath.sep) 258 path_parts = path.split(posixpath.sep)
264 locale, file_path = path_parts[0], path_parts[1:] 259 locale, file_path = path_parts[0], path_parts[1:]
265 if ext.lower() == ".json" and locale in inverted_required_locales: 260 if ext.lower() == ".json" and locale in inverted_required_locales:
266 output_path = os.path.join( 261 output_path = os.path.join(
267 locale_path, inverted_required_locales[locale], 262 locale_path, inverted_required_locales[locale],
268 *file_path + [file_name] 263 *file_path + [file_name]
269 ) 264 )
270 with archive.open(member) as f, open(output_path, "wb") as f_target: 265 with archive.open(member) as source_file, \
Sebastian Noack 2015/09/16 19:37:37 Why not |archive.extract(member, output_path)|?
kzar 2015/09/16 20:02:41 I believe the path parameter to the extract method
kzar 2015/09/16 20:10:29 (I just tested that to make sure, unfortunately my
271 shutil.copyfileobj(f, f_target) 266 open(output_path, "wb") as target_file:
267 shutil.copyfileobj(source_file, target_file)
272 268
273 def crowdin_sync(source_dir, crowdin_api_key): 269 def crowdin_sync(source_dir, crowdin_api_key):
274 with FileSource(source_dir) as source: 270 with FileSource(source_dir) as source:
275 config = source.read_config() 271 config = source.read_config()
276 defaultlocale = config.get("general", "defaultlocale") 272 defaultlocale = config.get("general", "defaultlocale")
277 crowdin_project_name = config.get("general", "crowdin-project-name") 273 crowdin_project_name = config.get("general", "crowdin-project-name")
278 274
279 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) 275 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name)
280 276
281 logger.info("Requesting project information...") 277 logger.info("Requesting project information...")
282 project_info = crowdin_api.request("GET", "info") 278 project_info = crowdin_api.request("GET", "info")
283 page_strings = extract_strings(source, defaultlocale) 279 page_strings = extract_strings(source, defaultlocale)
284 280
285 local_locales = {l for l in source.list_locales() if l != defaultlocale} 281 local_locales = source.list_locales() - {defaultlocale}
Sebastian Noack 2015/09/16 19:37:37 Nit: set(source.list_locales()) - {defaultlocale}
kzar 2015/09/16 20:02:41 Done.
286 enabled_locales = {l["code"] for l in project_info["languages"]} 282 enabled_locales = {l["code"] for l in project_info["languages"]}
287 283
288 required_locales = configure_locales(crowdin_api, local_locales, 284 required_locales = configure_locales(crowdin_api, local_locales,
289 enabled_locales, defaultlocale) 285 enabled_locales, defaultlocale)
290 286
291 remote_files, remote_directories = list_remote_files(project_info) 287 remote_files, remote_directories = list_remote_files(project_info)
292 local_files, local_directories = list_local_files(page_strings) 288 local_files, local_directories = list_local_files(page_strings)
293 289
294 # Avoid deleting all remote content if there was a problem listing local files 290 # Avoid deleting all remote content if there was a problem listing local files
295 if not local_files: 291 if not local_files:
(...skipping 21 matching lines...) Expand all
317 if __name__ == "__main__": 313 if __name__ == "__main__":
318 if len(sys.argv) < 3: 314 if len(sys.argv) < 3:
319 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" 315 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]"
320 sys.exit(1) 316 sys.exit(1)
321 317
322 logging.basicConfig() 318 logging.basicConfig()
323 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) 319 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO)
324 320
325 source_dir, crowdin_api_key = sys.argv[1:3] 321 source_dir, crowdin_api_key = sys.argv[1:3]
326 crowdin_sync(source_dir, crowdin_api_key) 322 crowdin_sync(source_dir, crowdin_api_key)
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld