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

Side by Side Diff: cms/bin/translate.py

Issue 29329499: Issue 3246 - Strip empty locale files provided by Crowdin (Closed)
Patch Set: Parse the JSON instead of using regexp Created Oct. 30, 2015, 12:02 p.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 | « no previous file | 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
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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import codecs
18 import collections 19 import collections
19 import io 20 import io
20 import itertools 21 import itertools
21 import json 22 import json
22 import logging 23 import logging
23 import os 24 import os
24 import posixpath 25 import posixpath
25 import shutil
26 import sys 26 import sys
27 import urllib 27 import urllib
28 import zipfile 28 import zipfile
29 29
30 import urllib3 30 import urllib3
31 31
32 import cms.utils 32 import cms.utils
33 from cms.sources import FileSource 33 from cms.sources import FileSource
34 34
35 logger = logging.getLogger("cms.bin.translate") 35 logger = logging.getLogger("cms.bin.translate")
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 for member in archive.namelist(): 255 for member in archive.namelist():
256 path, file_name = posixpath.split(member) 256 path, file_name = posixpath.split(member)
257 ext = posixpath.splitext(file_name)[1] 257 ext = posixpath.splitext(file_name)[1]
258 path_parts = path.split(posixpath.sep) 258 path_parts = path.split(posixpath.sep)
259 locale, file_path = path_parts[0], path_parts[1:] 259 locale, file_path = path_parts[0], path_parts[1:]
260 if ext.lower() == ".json" and locale in inverted_required_locales: 260 if ext.lower() == ".json" and locale in inverted_required_locales:
261 output_path = os.path.join( 261 output_path = os.path.join(
262 locale_path, inverted_required_locales[locale], 262 locale_path, inverted_required_locales[locale],
263 *file_path + [file_name] 263 *file_path + [file_name]
264 ) 264 )
265 with archive.open(member) as source_file, \ 265 with archive.open(member) as source_file:
266 open(output_path, "wb") as target_file: 266 try:
267 shutil.copyfileobj(source_file, target_file) 267 locale_file_contents = json.load(source_file)
268 except ValueError:
269 continue
Wladimir Palant 2015/10/30 18:02:40 I don't think that sweeping exceptions under the c
kzar 2015/10/30 18:17:29 Fair point, this might hide an unrelated issue. (I
270 if len(locale_file_contents):
271 with codecs.open(output_path, "wb", "utf-8") as target_file:
272 json.dump(locale_file_contents, target_file, ensure_ascii=False,
273 sort_keys=True, indent=2, separators=(",", ": "))
268 274
269 def crowdin_sync(source_dir, crowdin_api_key): 275 def crowdin_sync(source_dir, crowdin_api_key):
270 with FileSource(source_dir) as source: 276 with FileSource(source_dir) as source:
271 config = source.read_config() 277 config = source.read_config()
272 defaultlocale = config.get("general", "defaultlocale") 278 defaultlocale = config.get("general", "defaultlocale")
273 crowdin_project_name = config.get("general", "crowdin-project-name") 279 crowdin_project_name = config.get("general", "crowdin-project-name")
274 280
275 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) 281 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name)
276 282
277 logger.info("Requesting project information...") 283 logger.info("Requesting project information...")
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 if __name__ == "__main__": 319 if __name__ == "__main__":
314 if len(sys.argv) < 3: 320 if len(sys.argv) < 3:
315 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" 321 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]"
316 sys.exit(1) 322 sys.exit(1)
317 323
318 logging.basicConfig() 324 logging.basicConfig()
319 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) 325 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO)
320 326
321 source_dir, crowdin_api_key = sys.argv[1:3] 327 source_dir, crowdin_api_key = sys.argv[1:3]
322 crowdin_sync(source_dir, crowdin_api_key) 328 crowdin_sync(source_dir, crowdin_api_key)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld