diff --git a/.travis.yml b/.travis.yml index c1d70d528b3dee..48fb7b19560652 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ services: before_deploy: - docker pull lokalise/lokalise-cli deploy: + skip_cleanup: true provider: script script: script/travis_deploy on: diff --git a/script/translations_download_split.py b/script/translations_download_split.py index 08ea9fbcccc0dd..03718cf7cab723 100755 --- a/script/translations_download_split.py +++ b/script/translations_download_split.py @@ -1,14 +1,37 @@ #!/usr/bin/env python3 """Merge all translation sources into a single JSON file.""" import glob +import json import os import re - -from homeassistant.util import json as json_util +from typing import Union, List, Dict FILENAME_FORMAT = re.compile(r'strings\.(?P\w+)\.json') +def load_json(filename: str) \ + -> Union[List, Dict]: + """Load JSON data from a file and return as dict or list. + + Defaults to returning empty dict if file is not found. + """ + with open(filename, encoding='utf-8') as fdesc: + return json.loads(fdesc.read()) + return {} + + +def save_json(filename: str, data: Union[List, Dict]): + """Save JSON data to a file. + + Returns True on success. + """ + data = json.dumps(data, sort_keys=True, indent=4) + with open(filename, 'w', encoding='utf-8') as fdesc: + fdesc.write(data) + return True + return False + + def get_language(path): """Get the language code for the given file path.""" return os.path.splitext(os.path.basename(path))[0] @@ -55,13 +78,13 @@ def save_language_translations(lang, translations): if base_translations: path = get_component_path(lang, component) os.makedirs(os.path.dirname(path), exist_ok=True) - json_util.save_json(path, base_translations) + save_json(path, base_translations) for platform, platform_translations in component_translations.get( 'platform', {}).items(): path = get_platform_path(lang, component, platform) os.makedirs(os.path.dirname(path), exist_ok=True) - json_util.save_json(path, platform_translations) + save_json(path, platform_translations) def main(): @@ -73,7 +96,7 @@ def main(): paths = glob.iglob("build/translations-download/*.json") for path in paths: lang = get_language(path) - translations = json_util.load_json(path) + translations = load_json(path) save_language_translations(lang, translations) diff --git a/script/translations_upload_merge.py b/script/translations_upload_merge.py index 6382c8d9abed6b..450a4c9ba0fb64 100755 --- a/script/translations_upload_merge.py +++ b/script/translations_upload_merge.py @@ -2,14 +2,37 @@ """Merge all translation sources into a single JSON file.""" import glob import itertools +import json import os import re - -from homeassistant.util import json as json_util +from typing import Union, List, Dict FILENAME_FORMAT = re.compile(r'strings\.(?P\w+)\.json') +def load_json(filename: str) \ + -> Union[List, Dict]: + """Load JSON data from a file and return as dict or list. + + Defaults to returning empty dict if file is not found. + """ + with open(filename, encoding='utf-8') as fdesc: + return json.loads(fdesc.read()) + return {} + + +def save_json(filename: str, data: Union[List, Dict]): + """Save JSON data to a file. + + Returns True on success. + """ + data = json.dumps(data, sort_keys=True, indent=4) + with open(filename, 'w', encoding='utf-8') as fdesc: + fdesc.write(data) + return True + return False + + def find_strings_files(): """Return the paths of the strings source files.""" return itertools.chain( @@ -66,14 +89,14 @@ def main(): for path in paths: component, platform = get_component_platform(path) parent = get_translation_dict(translations, component, platform) - strings = json_util.load_json(path) + strings = load_json(path) parent.update(strings) os.chdir(root) os.makedirs("build", exist_ok=True) - json_util.save_json( + save_json( os.path.join("build", "translations-upload.json"), translations)