Skip to content

Commit

Permalink
Translation cleanup (home-assistant#12804)
Browse files Browse the repository at this point in the history
* Inline load/save JSON

* Skip cleanup on travis deploy
  • Loading branch information
emlove authored and balloob committed Mar 1, 2018
1 parent b434ffb commit 9f35d4d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
before_deploy:
- docker pull lokalise/lokalise-cli
deploy:
skip_cleanup: true
provider: script
script: script/travis_deploy
on:
Expand Down
33 changes: 28 additions & 5 deletions script/translations_download_split.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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():
Expand All @@ -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)


Expand Down
31 changes: 27 additions & 4 deletions script/translations_upload_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 9f35d4d

Please sign in to comment.