-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_proxy.py
More file actions
279 lines (221 loc) · 8.72 KB
/
config_proxy.py
File metadata and controls
279 lines (221 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""Configuration proxy
"""
import logging
import os
import json
from typing import Any, Dict, List, Optional, Type
import jsonschema
from jsonpath_ng import parse as jsonpath
logger = logging.getLogger(__name__)
class ConfigProxy:
"""Proxy to your configuration. It loads json file,
checks it against json schema (if found) and provides
an easy way of accessing json keys using JSON path.
If you need to change any of the class' settings,
you should create a subclass.
E.g. here we change environmental variable that contains
our config file path to `PATH_TO_CONFIG` and default name
of the configuration file to `settings.json`:
```python
from config_proxy import ConfigProxy as _ConfigProxy
class ConfigProxy(_ConfigProxy):
env_location = "PATH_TO_CONFIG"
config_file_names = ["settings.json"]
```
If you create a subclass, do not forget to actually pass
it to all of your properties:
```python
property = StringProperty(..., proxy=MyConfigProxySubclass)
```
"""
env_location: str = "CONFIG_PATH"
config_file_names: List[str] = ["config.json"]
current_config: Optional["ConfigProxy"] = None
strict: bool = True
def __init__(self, config_path: Optional[str]):
"""Class constructor. You are not supposed to actually create
an instance of this class, instead you should use *Property classes
or use `get_config` static method.
Arguments:
config_path {str} -- An actual path to json configuration file
Raises:
FileNotFoundError: Specified configuration file was not found.
"""
self.config_path = config_path
if self.config_path is None:
if not self.strict:
self.config = {}
self.schema = {}
return
raise FileNotFoundError("Config file was not found")
if not os.path.exists(self.config_path):
raise FileNotFoundError(f"Configuration file not found in {self.config_path}")
main_dirname = os.path.abspath(os.path.dirname(__file__))
schema_path = os.path.join(main_dirname, "config.schema.json")
with open(self.config_path, "r", encoding="utf-8") as fid:
self.config: dict = json.load(fid)
if not os.path.exists(schema_path):
logger.warning("Configuration schema was not found in %s. Continuing without schema.", schema_path)
self.schema = {}
return
with open(schema_path, "r", encoding="utf-8") as fid:
self.schema = json.load(fid)
jsonschema.validate(self.config, self.schema)
def get_value(self, path: str, use_list: Optional[bool] = None) -> Any:
"""Return value from json config file using JSON path.
Arguments:
path {str} -- A JSON path valid string.
"""
expr = jsonpath(path).find(self.config)
if not expr:
return [] if use_list else None
if use_list:
return [e.value for e in expr]
elif use_list == False:
return expr[0].value
# Guess whether to return list or not by default
if len(expr) == 1:
return expr[0].value
else:
return [e.value for e in expr]
@classmethod
def get_config_path(cls) -> str:
"""Config files are sought in following order:
1. `env_location` environmental variable specifying path to config file
2. `config_file_names` in current working directory
"""
if (config_path := os.getenv(cls.env_location, None)) :
logger.info("Using config file from env %s=%s", cls.env_location, config_path)
return config_path
wd = os.path.abspath(os.path.dirname("./"))
pd = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
paths = [os.path.join(dirname, fname) for dirname in (wd, pd) for fname in cls.config_file_names]
for config_path in paths:
logger.info("Searching for config path in %s", config_path)
if os.path.exists(config_path):
return config_path
raise FileNotFoundError(
(
"Configuration file was not found in any of the usual locations. "
f"Please, use env varibale {cls.env_location} instead."
)
)
@classmethod
def get_config(cls) -> "ConfigProxy":
"""Creates an instance of `ConfigProxy`, while actually searching for
the correct config file. If this method already was called, existing
configuration is returned.
"""
if cls.current_config:
return cls.current_config
try:
config_path = cls.get_config_path()
except FileNotFoundError as error:
if cls.strict:
raise error
config_path = None
cls.current_config = cls(config_path)
return cls.current_config
@classmethod
def reload(cls) -> "ConfigProxy":
"""Same as `get_config` but ensures that the configuration file is
read again if it already was opened before.
"""
cls.current_config = None
return cls.get_config()
class ConfigProperty:
"""A base class for access properties of the configuration
file (and / or environmental variables).
Various subclasses are provided in order to make the most use of
Python's typehinting and making your IDE actually autocomplete
your config values' types. If you do not need this functionality,
you are free to use this base class instead of actual subclasses.
Example:
### Config file
```json
{
"database": {
"host": "mydb.host.com",
"port": 1234
}
}
```
### Python file
```python
from config_proxy import ConfigProperty, StringProperty, IntProperty
# Here we get autocomplete because we know the result will be `str` and `int` respectively:
host = StringProperty("database.host", "DB_HOST", "localhost")
port = IntProperty("database.port", "DB_PORT", 5432)
# Here we have no autocomplete because we used base class instead of typed subclasses:
password = ConfigProperty("database.password", "DB_PASSWORD")
```
"""
path: Optional[str]
env: Optional[str]
default: Optional[Any] = None
def __init__(
self,
path: Optional[str] = None,
env: Optional[str] = None,
default: Optional[Any] = None,
proxy: Type[ConfigProxy] = ConfigProxy,
):
self.path = path
self.env = env
self.default = default
self.ProxyType = proxy
def get_value(self, use_list: Optional[bool] = None, forced: Optional[bool] = None) -> Any:
if self.env and (value := os.getenv(self.env, None)):
return value
config = self.ProxyType.get_config()
if self.path and (value := config.get_value(self.path, use_list=use_list)):
return value
if self.default is not None:
return self.default
if forced:
raise ValueError(f"Property {self.env} / {self.path} has no value")
return [] if use_list else None
@property
def value(self) -> Optional[str]:
"""Returns value found either in env, config file or default"""
return self.get_value()
@property
def fvalue(self) -> str:
"""Returns value. If the value is not present in either env or config file, ValueError is raised."""
return self.get_value(forced=True)
class StringProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> Optional[str]:
return self.get_value(use_list=False)
@property
def fvalue(self) -> str:
return self.get_value(use_list=False, forced=True)
class IntProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> Optional[int]:
return self.get_value(use_list=False)
@property
def fvalue(self) -> int:
return self.get_value(use_list=False, forced=True)
class ListOfIntsProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> List[int]:
return self.get_value(use_list=True)
class ListOfStringsProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> List[str]:
return self.get_value(use_list=True)
class ListOfObjectsProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> List[Dict]:
return self.get_value(use_list=True)
class ListOfListsProperty(ConfigProperty):
"""See `ConfigProperty` for more."""
@property
def value(self) -> List[List]:
return self.get_value(use_list=True)