From 840cbe4729ac8960c0dc0d23aca0405335f9ecb4 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 19 Feb 2024 03:44:40 +0300 Subject: user language support, other important fixes --- include/py/homekit/config/__init__.py | 1 + include/py/homekit/config/config.py | 39 ++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) (limited to 'include/py/homekit') diff --git a/include/py/homekit/config/__init__.py b/include/py/homekit/config/__init__.py index 8fedfa6..6323697 100644 --- a/include/py/homekit/config/__init__.py +++ b/include/py/homekit/config/__init__.py @@ -3,6 +3,7 @@ from .config import ( ConfigUnit, AppConfigUnit, Translation, + Language, config, is_development_mode, setup_logging, diff --git a/include/py/homekit/config/config.py b/include/py/homekit/config/config.py index 1eec97d..c5cc415 100644 --- a/include/py/homekit/config/config.py +++ b/include/py/homekit/config/config.py @@ -11,7 +11,6 @@ from argparse import ArgumentParser from enum import Enum, auto from os.path import join, isdir, isfile from ..util import Addr -from pprint import pprint class MyValidator(cerberus.Validator): @@ -55,6 +54,7 @@ class BaseConfigUnit(ABC): return key in self._data def load_from(self, path: str): + print(f'loading config from {path}') with open(path, 'r') as fd: self._data = yaml.safe_load(fd) if self._data is None: @@ -93,6 +93,7 @@ class ConfigUnit(BaseConfigUnit): NAME = 'dumb' _instance = None + __initialized: bool def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) @@ -101,9 +102,14 @@ class ConfigUnit(BaseConfigUnit): def __new__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super(ConfigUnit, cls).__new__(cls, *args, **kwargs) + if cls._instance is not None: + cls._instance.__initialized = False return cls._instance def __init__(self, name=None, load=True): + if self.__initialized: + return + super().__init__() self._data = {} @@ -116,6 +122,8 @@ class ConfigUnit(BaseConfigUnit): elif name is not None: self.NAME = name + self.__initialized = True + @classmethod def get_config_path(cls, name=None) -> str: if name is None: @@ -262,6 +270,17 @@ class AppConfigUnit(ConfigUnit): return self._logging_verbose +class Language(Enum): + RU = 'ru' + EN = 'en' + + def name(self): + if self == Language.RU: + return 'Русский' + elif self == Language.EN: + return 'English' + + class TranslationUnit(BaseConfigUnit): pass @@ -270,10 +289,10 @@ TranslationInstances = {} class Translation: - LANGUAGES = ('en', 'ru') - DEFAULT_LANGUAGE = 'ru' + DEFAULT_LANGUAGE = Language.RU - _langs: dict[str, TranslationUnit] + _langs: dict[Language, TranslationUnit] + __initialized: bool # def __init_subclass__(cls, **kwargs): # super().__init_subclass__(**kwargs) @@ -283,14 +302,18 @@ class Translation: unit = args[0] if unit not in TranslationInstances: TranslationInstances[unit] = super(Translation, cls).__new__(cls) + TranslationInstances[unit].__initialized = False return TranslationInstances[unit] def __init__(self, name: str): + if self.__initialized: + return + self._langs = {} - for lang in self.LANGUAGES: + for lang in Language: for dirname in CONFIG_DIRECTORIES: if isdir(dirname): - filename = join(dirname, f'i18n-{lang}', f'{name}.yaml') + filename = join(dirname, f'i18n-{lang.value}', f'{name}.yaml') if lang in self._langs: raise RuntimeError(f'{name}: translation unit for lang \'{lang}\' already loaded') self._langs[lang] = TranslationUnit() @@ -301,7 +324,9 @@ class Translation: if len(diff) > 0: raise RuntimeError(f'{name}: translation units have difference in keys: ' + ', '.join(diff)) - def get(self, lang: str = DEFAULT_LANGUAGE) -> TranslationUnit: + self.__initialized = True + + def get(self, lang: Language = DEFAULT_LANGUAGE) -> TranslationUnit: return self._langs[lang] -- cgit v1.2.3