Translation cleanup (#12804)

* Inline load/save JSON

* Skip cleanup on travis deploy
This commit is contained in:
Adam Mills 2018-02-28 23:04:20 -05:00 committed by Paulus Schoutsen
parent b434ffba2d
commit 9f35d4dfca
3 changed files with 56 additions and 9 deletions

View File

@ -33,6 +33,7 @@ services:
before_deploy:
- docker pull lokalise/lokalise-cli
deploy:
skip_cleanup: true
provider: script
script: script/travis_deploy
on:

View File

@ -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<suffix>\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)

View File

@ -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<suffix>\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)