Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config.load_langs_on_demand: permits on-the-fly loading/unloading, to facilitate proc-gen use cases #158

Merged
merged 5 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lingua_franca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
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

from lingua_franca import config
1 change: 1 addition & 0 deletions lingua_franca/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
load_langs_on_demand = False
32 changes: 24 additions & 8 deletions lingua_franca/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sys import version
from warnings import warn

from lingua_franca import config

_SUPPORTED_LANGUAGES = ("cs", "da", "de", "en", "es", "fr", "hu",
"it", "nl", "pl", "pt", "sv")

Expand Down Expand Up @@ -457,16 +459,14 @@ def is_error_type(_type):
def localized_function_decorator(func):
# Wrapper's logic
def _call_localized_function(func, *args, **kwargs):
lang_code = None
load_langs_on_demand = config.load_langs_on_demand
unload_language_afterward = False
func_signature = signature(func)
func_params = list(func_signature.parameters)
lang_param_index = func_params.index('lang')
full_lang_code = None

# Momentarily assume we're not passing a lang code
lang_code = get_default_lang()
if not lang_code:
raise NoSuchModuleError("No language module loaded.")

# Check if we're passing a lang as a kwarg
if 'lang' in kwargs.keys():
lang_param = kwargs['lang']
Expand All @@ -487,6 +487,15 @@ def _call_localized_function(func, *args, **kwargs):
lang_code = args[lang_param_index]
args = args[:lang_param_index] + args[lang_param_index+1:]

# Turns out, we aren't passing a lang code at all
lang_code = lang_code or get_default_lang()
if not lang_code:
if load_langs_on_demand:
raise NoSuchModuleError("No language module loaded "
"and none specified.")
else:
raise NoSuchModuleError("No language module loaded.")

if lang_code not in _SUPPORTED_LANGUAGES:
try:
tmp = lang_code
Expand Down Expand Up @@ -528,9 +537,14 @@ def _call_localized_function(func, *args, **kwargs):
raise NoSuchModuleError("Module lingua_franca." +
_module_name + " not recognized")
if lang_code not in _localized_functions[_module_name].keys():
raise NoSuchModuleError(_module_name + " module of language '" +
lang_code + "' is not currently loaded.")

if load_langs_on_demand:
load_language(lang_code)
unload_language_afterward = True
else:
raise NoSuchModuleError(_module_name +
" module of language '" +
lang_code +
"' is not currently loaded.")
func_name = func.__name__.split('.')[-1]
# At some point in the past, both the module and the language
# were imported/loaded, respectively.
Expand Down Expand Up @@ -573,6 +587,8 @@ def _call_localized_function(func, *args, **kwargs):
# Unload all the stuff we just assembled and imported
del localized_func
del _module
if unload_language_afterward:
unload_language(lang_code)
return r_val

# Actual wrapper
Expand Down