byoconfig 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- byo_config/__init__.py +4 -0
- byo_config/config.py +112 -0
- byo_config/error.py +19 -0
- byoconfig-0.0.1.dist-info/METADATA +25 -0
- byoconfig-0.0.1.dist-info/RECORD +7 -0
- byoconfig-0.0.1.dist-info/WHEEL +5 -0
- byoconfig-0.0.1.dist-info/top_level.txt +1 -0
byo_config/__init__.py
ADDED
byo_config/config.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import inspect
|
|
3
|
+
from typing import Optional, Type
|
|
4
|
+
|
|
5
|
+
from byo_config.sources import (
|
|
6
|
+
BaseVariableSource,
|
|
7
|
+
FileVariableSource,
|
|
8
|
+
EnvVariableSource,
|
|
9
|
+
FileTypes,
|
|
10
|
+
)
|
|
11
|
+
from byo_config.error import BYOConfigError
|
|
12
|
+
|
|
13
|
+
__all__ = ['Config']
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Config(FileVariableSource, EnvVariableSource):
|
|
19
|
+
"""
|
|
20
|
+
A versatile config object that can parse many file types, load environment variables,
|
|
21
|
+
and load dictionary keys as class attributes.
|
|
22
|
+
- Multiple config objects from different unique sources can be collated easily with a precedence value.
|
|
23
|
+
- Config instances with higher precedence have their values overwrite the values of lower precedence
|
|
24
|
+
instances when merging objects.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
source_file_path: Optional[str] = None,
|
|
30
|
+
forced_file_type: Optional[FileTypes] = None,
|
|
31
|
+
env_prefix: Optional[str] = None,
|
|
32
|
+
var_source_name: Optional[str] = "Config",
|
|
33
|
+
precedence: Optional[int] = 1,
|
|
34
|
+
**kwargs
|
|
35
|
+
):
|
|
36
|
+
"""
|
|
37
|
+
Initialize a Config object.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
source_file_path (str):
|
|
41
|
+
The path to the source file.
|
|
42
|
+
|
|
43
|
+
forced_file_type (FileTypes):
|
|
44
|
+
The file type of the source file, if you don't want to use the file's extension.
|
|
45
|
+
|
|
46
|
+
var_source_name (str):
|
|
47
|
+
The name of the variable source.
|
|
48
|
+
|
|
49
|
+
env_prefix (str):
|
|
50
|
+
The configuration keys will be loaded from the environment variables with this prefix.
|
|
51
|
+
Use the "*" wildcard if you want to load all environment variables as configuration keys.
|
|
52
|
+
|
|
53
|
+
precedence (int):
|
|
54
|
+
The precedence of the variable source.
|
|
55
|
+
|
|
56
|
+
**kwargs:
|
|
57
|
+
Arbitrary keyword arguments, to be loaded as class attributes.
|
|
58
|
+
"""
|
|
59
|
+
try:
|
|
60
|
+
self.precedence = precedence
|
|
61
|
+
self.var_source_name = var_source_name
|
|
62
|
+
super().__init__(
|
|
63
|
+
source_file=source_file_path,
|
|
64
|
+
forced_file_type=forced_file_type
|
|
65
|
+
)
|
|
66
|
+
super().load_env(env_prefix)
|
|
67
|
+
|
|
68
|
+
self.set_data(kwargs)
|
|
69
|
+
logger.debug(f"Config object {self.var_source_name} created with precedence {self.precedence}")
|
|
70
|
+
|
|
71
|
+
except BYOConfigError as e:
|
|
72
|
+
raise e
|
|
73
|
+
|
|
74
|
+
except FileNotFoundError as e:
|
|
75
|
+
raise e
|
|
76
|
+
|
|
77
|
+
except ValueError as e:
|
|
78
|
+
raise e
|
|
79
|
+
|
|
80
|
+
except Exception as e:
|
|
81
|
+
raise e
|
|
82
|
+
|
|
83
|
+
def include(self, plugin_class: Type[BaseVariableSource], **kwargs):
|
|
84
|
+
"""
|
|
85
|
+
Include a plugin class in the config object.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
plugin_class (Type[BaseVariableSource]):
|
|
89
|
+
The plugin class to include in the config object.
|
|
90
|
+
|
|
91
|
+
**kwargs:
|
|
92
|
+
Arbitrary keyword arguments to pass to the plugin class.
|
|
93
|
+
"""
|
|
94
|
+
try:
|
|
95
|
+
# get signature of plugin class
|
|
96
|
+
sig = inspect.signature(plugin_class)
|
|
97
|
+
# Compare kwargs to signature
|
|
98
|
+
for k, v in kwargs.items():
|
|
99
|
+
if k not in sig.parameters:
|
|
100
|
+
raise BYOConfigError(
|
|
101
|
+
f"Invalid parameter '{k}' for plugin class '{plugin_class.__name__}'",
|
|
102
|
+
self
|
|
103
|
+
)
|
|
104
|
+
plugin = plugin_class(**kwargs) # type: ignore
|
|
105
|
+
self.set_data(plugin.get_data())
|
|
106
|
+
logger.debug(f"Initialized plugin '{plugin_class.__name__}' with data: {plugin.get_data()}")
|
|
107
|
+
|
|
108
|
+
except BYOConfigError as e:
|
|
109
|
+
raise e
|
|
110
|
+
|
|
111
|
+
except Exception as e:
|
|
112
|
+
raise e
|
byo_config/error.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
__all__ = ['BYOConfigError']
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BYOConfigError(ValueError):
|
|
6
|
+
"""Subclass of ValueError with the following additional properties:
|
|
7
|
+
args:
|
|
8
|
+
msg: The unformatted error message
|
|
9
|
+
instance_name: The name of which VariableSource instance raised the error
|
|
10
|
+
"""
|
|
11
|
+
def __init__(self, msg, instance):
|
|
12
|
+
errmsg = f"{msg} in VariableSource instance '{instance.var_source_name}'"
|
|
13
|
+
super().__init__(self, errmsg)
|
|
14
|
+
self.msg = msg
|
|
15
|
+
self.instance = instance.var_source_name
|
|
16
|
+
|
|
17
|
+
def __reduce__(self):
|
|
18
|
+
return self.__class__, (self.msg, self.instance)
|
|
19
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: byoconfig
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A configuration class that supports plugins, multiple file formats, heirarchical configuration, and more.
|
|
5
|
+
Author-email: Cam Ratchford <camratchford@gmail.com>
|
|
6
|
+
Classifier: Development Status :: 3 - Alpha
|
|
7
|
+
Classifier: Programming Language :: Python
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Intended Audience :: Information Technology
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Environment :: Win32 (MS Windows)
|
|
13
|
+
Classifier: Environment :: MacOS X
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Requires-Dist: PyYAML ==6.0.2
|
|
17
|
+
Requires-Dist: toml ==0.10.2
|
|
18
|
+
Provides-Extra: development
|
|
19
|
+
Requires-Dist: mkdocs ; extra == 'development'
|
|
20
|
+
Requires-Dist: mkdocs-material ; extra == 'development'
|
|
21
|
+
Requires-Dist: ruff ; extra == 'development'
|
|
22
|
+
Requires-Dist: pytest ; extra == 'development'
|
|
23
|
+
Requires-Dist: build ; extra == 'development'
|
|
24
|
+
Requires-Dist: twine ; extra == 'development'
|
|
25
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
byo_config/__init__.py,sha256=fOEImax01vH8EZ92kMMerj5k_0UzKRyFM9oQ9CPwNQw,128
|
|
2
|
+
byo_config/config.py,sha256=AB8U-E6jiGTUxVa52xeaitAdFI_x0c-b64mCLcGOFjA,3770
|
|
3
|
+
byo_config/error.py,sha256=tgYMbn9E9yrj_f1r18J7ZqKcVS9sLgSUWYGzpklS4nk,627
|
|
4
|
+
byoconfig-0.0.1.dist-info/METADATA,sha256=UurTDjhSsApYkHXxmeGaS0JiZFnEBXdM0YfnqPeAixs,1088
|
|
5
|
+
byoconfig-0.0.1.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
6
|
+
byoconfig-0.0.1.dist-info/top_level.txt,sha256=qRw1-WIuppmhfRyphfkE3qtGuBHZH-6wbiLNwkUWXpg,11
|
|
7
|
+
byoconfig-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
byo_config
|