Fix translation schema (#2654)

* Fix translation schema

* really fix it

* fix migration path

Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
This commit is contained in:
Joakim Sørensen 2021-03-01 16:39:00 +01:00 committed by GitHub
parent 9e86eda05a
commit 43449c85bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 36 deletions

View File

@ -1,6 +1,5 @@
"""Init file for Supervisor add-on data."""
from copy import deepcopy
import logging
from typing import Any, Dict
from ..const import (
@ -17,8 +16,6 @@ from ..utils.common import FileConfiguration
from .addon import Addon
from .validate import SCHEMA_ADDONS_FILE
_LOGGER: logging.Logger = logging.getLogger(__name__)
Config = Dict[str, Any]

View File

@ -54,9 +54,9 @@ from ..const import (
ATTR_STAGE,
ATTR_STARTUP,
ATTR_STDIN,
ATTR_TANSLATIONS,
ATTR_TIMEOUT,
ATTR_TMPFS,
ATTR_TRANSLATIONS,
ATTR_UART,
ATTR_UDEV,
ATTR_URL,
@ -189,7 +189,7 @@ class AddonModel(CoreSysAttributes, ABC):
@property
def translations(self) -> dict:
"""Return add-on translations."""
return self.data[ATTR_TANSLATIONS]
return self.data[ATTR_TRANSLATIONS]
@property
def latest_version(self) -> AwesomeVersion:

View File

@ -72,9 +72,9 @@ from ..const import (
ATTR_STATE,
ATTR_STDIN,
ATTR_SYSTEM,
ATTR_TANSLATIONS,
ATTR_TIMEOUT,
ATTR_TMPFS,
ATTR_TRANSLATIONS,
ATTR_UART,
ATTR_UDEV,
ATTR_URL,
@ -322,6 +322,20 @@ SCHEMA_BUILD_CONFIG = vol.Schema(
extra=vol.REMOVE_EXTRA,
)
SCHEMA_TRANSLATION_CONFIGURATION = vol.Schema(
{
vol.Required(ATTR_NAME): str,
vol.Optional(ATTR_DESCRIPTON): vol.Maybe(str),
},
extra=vol.REMOVE_EXTRA,
)
SCHEMA_ADDON_TRANSLATIONS = vol.Schema(
{vol.Optional(ATTR_CONFIGURATION): {str: SCHEMA_TRANSLATION_CONFIGURATION}},
extra=vol.REMOVE_EXTRA,
)
# pylint: disable=no-value-for-parameter
SCHEMA_ADDON_USER = vol.Schema(
@ -344,19 +358,15 @@ SCHEMA_ADDON_USER = vol.Schema(
extra=vol.REMOVE_EXTRA,
)
SCHEMA_ADDON_TRANSLATION = vol.Schema(
{vol.Optional(ATTR_CONFIGURATION): {str: str}}, extra=vol.REMOVE_EXTRA
)
SCHEMA_ADDON_SYSTEM = vol.All(
_migrate_addon_config(),
_SCHEMA_ADDON_CONFIG.extend(
{
vol.Required(ATTR_LOCATON): str,
vol.Required(ATTR_REPOSITORY): str,
vol.Optional(ATTR_TANSLATIONS, default={}): SCHEMA_ADDON_TRANSLATION,
vol.Required(ATTR_TRANSLATIONS, default=dict): {
str: SCHEMA_ADDON_TRANSLATIONS
},
}
),
)

View File

@ -82,7 +82,7 @@ from ..const import (
ATTR_STARTUP,
ATTR_STATE,
ATTR_STDIN,
ATTR_TANSLATIONS,
ATTR_TRANSLATIONS,
ATTR_UART,
ATTR_UDEV,
ATTR_UPDATE_AVAILABLE,
@ -266,7 +266,7 @@ class APIAddons(CoreSysAttributes):
ATTR_SERVICES: _pretty_services(addon),
ATTR_DISCOVERY: addon.discovery,
ATTR_IP_ADDRESS: None,
ATTR_TANSLATIONS: addon.translations,
ATTR_TRANSLATIONS: addon.translations,
ATTR_INGRESS: addon.with_ingress,
ATTR_INGRESS_ENTRY: None,
ATTR_INGRESS_URL: None,

View File

@ -283,7 +283,7 @@ ATTR_TIMEZONE = "timezone"
ATTR_TITLE = "title"
ATTR_TMPFS = "tmpfs"
ATTR_TOTP = "totp"
ATTR_TANSLATIONS = "translations"
ATTR_TRANSLATIONS = "translations"
ATTR_TYPE = "type"
ATTR_UART = "uart"
ATTR_UDEV = "udev"

View File

@ -6,12 +6,12 @@ from typing import Any, Dict
import voluptuous as vol
from voluptuous.humanize import humanize_error
from ..addons.validate import SCHEMA_ADDON_CONFIG, SCHEMA_ADDON_TRANSLATION
from ..addons.validate import SCHEMA_ADDON_CONFIG, SCHEMA_ADDON_TRANSLATIONS
from ..const import (
ATTR_LOCATON,
ATTR_REPOSITORY,
ATTR_SLUG,
ATTR_TANSLATIONS,
ATTR_TRANSLATIONS,
FILE_SUFFIX_CONFIGURATION,
REPOSITORY_CORE,
REPOSITORY_LOCAL,
@ -130,7 +130,9 @@ class StoreData(CoreSysAttributes):
# store
addon_config[ATTR_REPOSITORY] = repository
addon_config[ATTR_LOCATON] = str(addon.parent)
addon_config[ATTR_TANSLATIONS] = self._read_addon_translations(addon.parent)
addon_config[ATTR_TRANSLATIONS] = self._read_addon_translations(
addon.parent
)
self.addons[addon_slug] = addon_config
def _set_builtin_repositories(self):
@ -164,12 +166,14 @@ class StoreData(CoreSysAttributes):
for translation in translation_files:
try:
translations[translation.stem] = SCHEMA_ADDON_TRANSLATION(
translations[translation.stem] = SCHEMA_ADDON_TRANSLATIONS(
read_json_or_yaml_file(translation)
)
except (ConfigurationFileError, vol.Invalid):
_LOGGER.warning("Can't read translations from %s", translation)
except (ConfigurationFileError, vol.Invalid) as err:
_LOGGER.warning(
"Can't read translations from %s - %s", translation, err
)
continue
return translations

View File

@ -1,14 +1,9 @@
"""Test loading add-translation."""
# pylint: disable=import-error,protected-access
import json
import os
from ruamel.yaml import YAML
from supervisor.coresys import CoreSys
_YAML = YAML()
_YAML.allow_duplicate_keys = True
from supervisor.utils.common import write_json_or_yaml_file
def test_loading_traslations(coresys: CoreSys, tmp_path):
@ -18,17 +13,22 @@ def test_loading_traslations(coresys: CoreSys, tmp_path):
assert coresys.store.data._read_addon_translations(tmp_path) == {}
for file in ("en.json", "es.json"):
with open(tmp_path / "translations" / file, "w") as lang_file:
lang_file.write(json.dumps({"configuration": {"test": "test"}}))
write_json_or_yaml_file(
tmp_path / "translations" / file,
{"configuration": {"test": {"name": "test", "test": "test"}}},
)
for file in ("no.yaml", "de.yaml"):
_YAML.dump(
{"configuration": {"test": "test"}}, tmp_path / "translations" / file
write_json_or_yaml_file(
tmp_path / "translations" / file,
{"configuration": {"test": {"name": "test", "test": "test"}}},
)
translations = coresys.store.data._read_addon_translations(tmp_path)
assert translations["en"]["configuration"]["test"] == "test"
assert translations["es"]["configuration"]["test"] == "test"
assert translations["no"]["configuration"]["test"] == "test"
assert translations["de"]["configuration"]["test"] == "test"
assert translations["en"]["configuration"]["test"]["name"] == "test"
assert translations["es"]["configuration"]["test"]["name"] == "test"
assert translations["no"]["configuration"]["test"]["name"] == "test"
assert translations["de"]["configuration"]["test"]["name"] == "test"
assert "test" not in translations["en"]["configuration"]["test"]