From f473b80258978ed26ee08449a26143dc67931ae0 Mon Sep 17 00:00:00 2001 From: ChanceNCounter Date: Thu, 1 Apr 2021 17:10:20 -0700 Subject: [PATCH] Initial, working impl. of #128 (not hooked up) This is a technically-working implementation of #128, which creates an object, exposed at runtime as lingua_franca.config, which descends from dict. More robust get/set methods to follow. The implementation is extensible to support full use of localization and 'full lang codes'. Either that or hooking it up to the functions will be next. --- lingua_franca/__init__.py | 17 ++++++-- lingua_franca/config.py | 1 - lingua_franca/configuration.py | 50 ++++++++++++++++++++++++ lingua_franca/internal.py | 11 +++++- lingua_franca/res/text/en-us/config.json | 3 ++ 5 files changed, 75 insertions(+), 7 deletions(-) delete mode 100644 lingua_franca/config.py create mode 100644 lingua_franca/configuration.py create mode 100644 lingua_franca/res/text/en-us/config.json diff --git a/lingua_franca/__init__.py b/lingua_franca/__init__.py index 0404403c..e03c78d9 100644 --- a/lingua_franca/__init__.py +++ b/lingua_franca/__init__.py @@ -1,6 +1,15 @@ +### DO NOT CHANGE THIS IMPORT ORDER ### +from .internal import get_active_langs, get_supported_locs, \ + get_full_lang_code + +from .configuration import Config + +### END OF IMPORT ORDER ### + from .internal import get_default_lang, set_default_lang, get_default_loc, \ - get_active_langs, _set_active_langs, get_primary_lang_code, \ - get_full_lang_code, resolve_resource_file, load_language, \ - load_languages, unload_language, unload_languages, get_supported_langs + _set_active_langs, get_primary_lang_code, resolve_resource_file, \ + load_language, load_languages, unload_language, unload_languages, \ + get_supported_langs + -from lingua_franca import config +config = Config() diff --git a/lingua_franca/config.py b/lingua_franca/config.py deleted file mode 100644 index 06dc9677..00000000 --- a/lingua_franca/config.py +++ /dev/null @@ -1 +0,0 @@ -load_langs_on_demand = False diff --git a/lingua_franca/configuration.py b/lingua_franca/configuration.py new file mode 100644 index 00000000..74822640 --- /dev/null +++ b/lingua_franca/configuration.py @@ -0,0 +1,50 @@ +import json +from os import path + +from lingua_franca import get_active_langs, get_supported_locs, \ + get_full_lang_code +from lingua_franca.internal import UnsupportedLanguageError, resolve_resource_file + +default_global_values = \ + { + 'load_langs_on_demand': False + } + +class LangConfig(dict): + def __init__(self, lang_code): + if lang_code not in get_supported_locs(): + # DO NOT catch UnsupportedLanguageError! + # If this fails, we want to crash. This can *only* result from + # someone trying to override sanity checks upstairs. There are no + # circumstances under which this should fail and allow the program + # to continue. + lang_code = get_full_lang_code(lang_code) + + + resource_file = resolve_resource_file(f'text/{lang_code}/config.json') + with open(resource_file, 'r', encoding='utf-8') as i_file: + default_values = json.load(i_file) + for k in default_values: + self[k] = default_values[k] + +class Config(dict): + def __init__(self): + self['global'] = dict(default_global_values) + for lang in get_active_langs(): + ''' + TODO proper full loc support here will handle languages similarly to global: + + self['en']['universal'] for 'default' English config + (all dialects if not overridden) + self['en']['en-us'] for overrides specific to en-US + self['en']['en-au'] for overrides specific to en-AU + + and so forth. + ''' + if all((lang not in self.keys(), lang not in get_supported_locs())): + self[lang] = {} + self[lang]['universal'] = LangConfig(lang) + # begin portion that will need to adapt for the todo above + full_loc = lang if lang in get_supported_locs() else \ + get_full_lang_code(lang) + self[lang][full_loc] = LangConfig(lang) \ No newline at end of file diff --git a/lingua_franca/internal.py b/lingua_franca/internal.py index 27ca74bf..20bda682 100644 --- a/lingua_franca/internal.py +++ b/lingua_franca/internal.py @@ -5,8 +5,6 @@ from sys import version from warnings import warn -from lingua_franca import config - _SUPPORTED_LANGUAGES = ("ca", "cs", "da", "de", "en", "es", "fr", "hu", "it", "nl", "pl", "pt", "sl", "sv") @@ -88,6 +86,13 @@ def get_supported_langs(): """ return _SUPPORTED_LANGUAGES +def get_supported_locs(): + """ + Returns: + list(str) + """ + return _SUPPORTED_FULL_LOCALIZATIONS + def get_active_langs(): """ Get the list of currently-loaded language codes @@ -448,6 +453,8 @@ def is_error_type(_type): def localized_function_decorator(func): # Wrapper's logic def _call_localized_function(func, *args, **kwargs): + from lingua_franca import config + lang_code = None load_langs_on_demand = config.load_langs_on_demand unload_language_afterward = False diff --git a/lingua_franca/res/text/en-us/config.json b/lingua_franca/res/text/en-us/config.json new file mode 100644 index 00000000..062916f4 --- /dev/null +++ b/lingua_franca/res/text/en-us/config.json @@ -0,0 +1,3 @@ +{ + "short_scale": true +} \ No newline at end of file