 Issue 29329499:
  Issue 3246 - Strip empty locale files provided by Crowdin  (Closed)
    
  
    Issue 29329499:
  Issue 3246 - Strip empty locale files provided by Crowdin  (Closed) 
  | 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, | 
| 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 re | |
| 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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive: | 245 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive: | 
| 246 locale_path = os.path.join(source_dir, "locales") | 246 locale_path = os.path.join(source_dir, "locales") | 
| 247 # First clear existing translation files | 247 # First clear existing translation files | 
| 248 for root, dirs, files in os.walk(locale_path, topdown=True): | 248 for root, dirs, files in os.walk(locale_path, topdown=True): | 
| 249 if root == locale_path: | 249 if root == locale_path: | 
| 250 dirs[:] = [d for d in dirs if d in required_locales] | 250 dirs[:] = [d for d in dirs if d in required_locales] | 
| 251 for f in files: | 251 for f in files: | 
| 252 if f.lower().endswith(".json"): | 252 if f.lower().endswith(".json"): | 
| 253 os.remove(os.path.join(root, f)) | 253 os.remove(os.path.join(root, f)) | 
| 254 # Then extract the new ones in place | 254 # Then extract the new ones in place | 
| 255 non_empty_file_regexp = re.compile(r"[^\s{}[\]]") | |
| 256 for member in archive.namelist(): | 255 for member in archive.namelist(): | 
| 257 path, file_name = posixpath.split(member) | 256 path, file_name = posixpath.split(member) | 
| 258 ext = posixpath.splitext(file_name)[1] | 257 ext = posixpath.splitext(file_name)[1] | 
| 259 path_parts = path.split(posixpath.sep) | 258 path_parts = path.split(posixpath.sep) | 
| 260 locale, file_path = path_parts[0], path_parts[1:] | 259 locale, file_path = path_parts[0], path_parts[1:] | 
| 261 if ext.lower() == ".json" and locale in inverted_required_locales: | 260 if ext.lower() == ".json" and locale in inverted_required_locales: | 
| 262 output_path = os.path.join( | 261 output_path = os.path.join( | 
| 263 locale_path, inverted_required_locales[locale], | 262 locale_path, inverted_required_locales[locale], | 
| 264 *file_path + [file_name] | 263 *file_path + [file_name] | 
| 265 ) | 264 ) | 
| 266 with archive.open(member) as source_file: | 265 with archive.open(member) as source_file: | 
| 267 locale_file_contents = source_file.read() | 266 locale_file_contents = json.load(source_file) | 
| 268 if re.search(non_empty_file_regexp, locale_file_contents): | 267 if len(locale_file_contents): | 
| 
Wladimir Palant
2015/10/29 19:12:35
I don't think that using regular expressions on JS
 
kzar
2015/10/30 12:03:03
Done.
 | |
| 269 with open(output_path, "wb") as target_file: | 268 with codecs.open(output_path, "wb", "utf-8") as target_file: | 
| 270 target_file.write(locale_file_contents) | 269 json.dump(locale_file_contents, target_file, ensure_ascii=False, | 
| 270 sort_keys=True, indent=2, separators=(",", ": ")) | |
| 271 | 271 | 
| 272 def crowdin_sync(source_dir, crowdin_api_key): | 272 def crowdin_sync(source_dir, crowdin_api_key): | 
| 273 with FileSource(source_dir) as source: | 273 with FileSource(source_dir) as source: | 
| 274 config = source.read_config() | 274 config = source.read_config() | 
| 275 defaultlocale = config.get("general", "defaultlocale") | 275 defaultlocale = config.get("general", "defaultlocale") | 
| 276 crowdin_project_name = config.get("general", "crowdin-project-name") | 276 crowdin_project_name = config.get("general", "crowdin-project-name") | 
| 277 | 277 | 
| 278 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) | 278 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) | 
| 279 | 279 | 
| 280 logger.info("Requesting project information...") | 280 logger.info("Requesting project information...") | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if __name__ == "__main__": | 316 if __name__ == "__main__": | 
| 317 if len(sys.argv) < 3: | 317 if len(sys.argv) < 3: | 
| 318 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" | 318 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" | 
| 319 sys.exit(1) | 319 sys.exit(1) | 
| 320 | 320 | 
| 321 logging.basicConfig() | 321 logging.basicConfig() | 
| 322 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) | 322 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) | 
| 323 | 323 | 
| 324 source_dir, crowdin_api_key = sys.argv[1:3] | 324 source_dir, crowdin_api_key = sys.argv[1:3] | 
| 325 crowdin_sync(source_dir, crowdin_api_key) | 325 crowdin_sync(source_dir, crowdin_api_key) | 
| LEFT | RIGHT |