Skip to content

Commit

Permalink
feat: autogenerate minimal translations
Browse files Browse the repository at this point in the history
Detect if translations file is created and if not create a default
en.json
closes #8
  • Loading branch information
alandtse committed Mar 29, 2021
1 parent 30b9000 commit 55f56d1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
71 changes: 71 additions & 0 deletions custom_components/pr_custom_component/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import asyncio
import base64
import json
from json.decoder import JSONDecodeError
import logging
import os
import shutil
Expand All @@ -27,10 +28,13 @@
API_PATH_PREFIX,
COMPONENT_PATH,
CUSTOM_COMPONENT_PATH,
ENGLISH_JSON,
EXCEPTION_TEMPLATE,
PATCH_DOMAIN,
PATCH_PATH_PREFIX,
PATCH_PATH_SUFFIX,
STRING_FILE,
TRANSLATIONS_PATH,
)
from .exceptions import RateLimitException

Expand Down Expand Up @@ -216,6 +220,7 @@ async def async_download(self, url: str, path: str) -> bool:
os.mkdir(file_path)
tasks.append(self.async_download(file_json["url"], path))
await asyncio.gather(*tasks)
await self.async_create_translations()
return True
if isinstance(result, dict):
path.split(os.sep)
Expand All @@ -241,6 +246,72 @@ async def async_download(self, url: str, path: str) -> bool:
return True
return False

async def async_create_translations(self) -> bool:
"""Create translations directory if needed.
Returns:
bool: Whether translations directory exists
"""
if not self._config_path:
_LOGGER.debug("Config path not initialized")
return False
if not self._component_name:
_LOGGER.debug("Component name not initialized")
return False
component_path: str = os.path.join(
self._config_path, CUSTOM_COMPONENT_PATH, self._component_name
)
translations_path = os.path.join(component_path, TRANSLATIONS_PATH)
strings_path = os.path.join(component_path, STRING_FILE)
english_path = os.path.join(translations_path, ENGLISH_JSON)
_LOGGER.debug("Checking for translations in %s", component_path)
if os.path.isdir(translations_path) and os.path.isfile(english_path):
_LOGGER.debug("Translations directory and en.json already exists")
return True
if not os.path.isfile(strings_path):
_LOGGER.debug(
"%s does not exist, not able to create translations directory",
strings_path,
)
return False
else:
if not os.path.isdir(translations_path):
_LOGGER.debug("Creating translations directory %s", translations_path)
try:
os.mkdir(translations_path)
except (OSError) as ex:
_LOGGER.debug(
"Error creating directory %s",
translations_path,
EXCEPTION_TEMPLATE.format(type(ex).__name__, ex.args),
)
return False
try:
async with aiofiles.open(strings_path) as localfile:
contents = await localfile.read()
strings_json: str = json.loads(contents)
except (OSError, EOFError, JSONDecodeError, TypeError) as ex:
_LOGGER.debug(
"Error reading file %s: %s",
strings_path,
EXCEPTION_TEMPLATE.format(type(ex).__name__, ex.args),
)
return False
if strings_json.get("title") and self._manifest.get("name"):
strings_json["title"] = self._manifest["name"]
contents = json.dumps(strings_json).encode("utf-8")
try:
async with aiofiles.open(english_path, mode="wb") as localfile:
await localfile.write(contents)
except (OSError) as ex:
_LOGGER.debug(
"Error saving file %s: %s",
english_path,
EXCEPTION_TEMPLATE.format(type(ex).__name__, ex.args),
)
return False
return True

async def api_wrapper(
self,
method: str,
Expand Down
3 changes: 3 additions & 0 deletions custom_components/pr_custom_component/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
# HA Constants
COMPONENT_PATH = "homeassistant/components/"
CUSTOM_COMPONENT_PATH = "custom_components/"
TRANSLATIONS_PATH = "translations/"
STRING_FILE = "strings.json"
ENGLISH_JSON = "en.json"

# Icons
ICON = "mdi:update"
Expand Down

0 comments on commit 55f56d1

Please sign in to comment.