2015-08-25 11:54:11 +00:00
|
|
|
from importlib import import_module
|
2010-11-30 23:20:23 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
from . import LazyConfig
|
|
|
|
|
from . import settings
|
2023-04-07 13:16:39 +00:00
|
|
|
|
|
|
|
|
config = LazyConfig()
|
2013-04-12 15:39:10 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
|
2010-11-30 23:20:23 +00:00
|
|
|
def import_module_attr(path):
|
2025-10-07 09:25:07 +00:00
|
|
|
package, module = path.rsplit(".", 1)
|
2010-11-30 23:20:23 +00:00
|
|
|
return getattr(import_module(package), module)
|
2023-04-07 13:16:39 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
|
2023-04-07 13:16:39 +00:00
|
|
|
def get_values():
|
|
|
|
|
"""
|
|
|
|
|
Get dictionary of values from the backend
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
# First load a mapping between config name and default value
|
2024-07-03 14:21:33 +00:00
|
|
|
default_initial = ((name, options[0]) for name, options in settings.CONFIG.items())
|
2023-04-07 13:16:39 +00:00
|
|
|
# Then update the mapping with actually values from the backend
|
2024-07-05 14:38:26 +00:00
|
|
|
return dict(default_initial, **dict(config._backend.mget(settings.CONFIG)))
|
2025-01-14 16:31:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_values_for_keys(keys):
|
|
|
|
|
"""
|
|
|
|
|
Retrieve values for specified keys from the backend.
|
|
|
|
|
|
|
|
|
|
:param keys: List of keys to retrieve.
|
|
|
|
|
:return: Dictionary with values for the specified keys.
|
|
|
|
|
:raises AttributeError: If any key is not found in the configuration.
|
|
|
|
|
"""
|
|
|
|
|
if not isinstance(keys, (list, tuple, set)):
|
2025-10-07 09:25:07 +00:00
|
|
|
raise TypeError("keys must be a list, tuple, or set of strings")
|
2025-01-14 16:31:32 +00:00
|
|
|
|
|
|
|
|
# Prepare default initial mapping
|
|
|
|
|
default_initial = {name: options[0] for name, options in settings.CONFIG.items() if name in keys}
|
|
|
|
|
|
|
|
|
|
# Check if all keys are present in the default_initial mapping
|
|
|
|
|
missing_keys = [key for key in keys if key not in default_initial]
|
|
|
|
|
if missing_keys:
|
|
|
|
|
raise AttributeError(f'"{", ".join(missing_keys)}" keys not found in configuration.')
|
|
|
|
|
|
|
|
|
|
# Merge default values and backend values, prioritizing backend values
|
|
|
|
|
return dict(default_initial, **dict(config._backend.mget(keys)))
|