.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
config.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Configuration class :py:class:`Config` with deep-update, schema validation
3and deprecated names.
4
5The :py:class:`Config` class implements a configuration that is based on
6structured dictionaries. The configuration schema is defined in a dictionary
7structure and the configuration data is given in a dictionary structure.
8"""
9from __future__ import annotations
10from typing import Any
11
12import copy
13import typing
14import logging
15import pathlib
16
17try:
18 import tomllib
19
20 pytomlpp = None
21 USE_TOMLLIB = True
22except ImportError:
23 import pytomlpp
24
25 tomllib = None
26 USE_TOMLLIB = False
27
28
29__all__ = ['Config', 'UNSET', 'SchemaIssue']
30
31log = logging.getLogger(__name__)
32
33
34class FALSE:
35 """Class of ``False`` singelton"""
36
37 # pylint: disable=multiple-statements
38 def __init__(self, msg):
39 self.msg = msg
40
41 def __bool__(self):
42 return False
43
44 def __str__(self):
45 return self.msg
46
47 __repr__ = __str__
48
49
50UNSET = FALSE('<UNSET>')
51
52
53class SchemaIssue(ValueError):
54 """Exception to store and/or raise a message from a schema issue."""
55
56 def __init__(self, level: typing.Literal['warn', 'invalid'], msg: str):
57 self.level = level
58 super().__init__(msg)
59
60 def __str__(self):
61 return f"[cfg schema {self.level}] {self.args[0]}"
62
63
64class Config:
65 """Base class used for configuration"""
66
67 UNSET = UNSET
68
69 @classmethod
70 def from_toml(cls, schema_file: pathlib.Path, cfg_file: pathlib.Path, deprecated: dict) -> Config:
71
72 # init schema
73
74 log.debug("load schema file: %s", schema_file)
75 cfg = cls(cfg_schema=toml_load(schema_file), deprecated=deprecated)
76 if not cfg_file.exists():
77 log.warning("missing config file: %s", cfg_file)
78 return cfg
79
80 # load configuration
81
82 log.debug("load config file: %s", cfg_file)
83 upd_cfg = toml_load(cfg_file)
84
85 is_valid, issue_list = cfg.validate(upd_cfg)
86 for msg in issue_list:
87 log.error(str(msg))
88 if not is_valid:
89 raise TypeError(f"schema of {cfg_file} is invalid!")
90 cfg.update(upd_cfg)
91 return cfg
92
93 def __init__(self, cfg_schema: typing.Dict, deprecated: typing.Dict[str, str]):
94 """Construtor of class Config.
95
96 :param cfg_schema: Schema of the configuration
97 :param deprecated: dictionary that maps deprecated configuration names to a messages
98
99 These values are needed for validation, see :py:obj:`validate`.
100
101 """
102 self.cfg_schema = cfg_schema
103 self.deprecated = deprecated
104 self.cfg = copy.deepcopy(cfg_schema)
105
106 def __getitem__(self, key: str) -> Any:
107 return self.get(key)
108
109 def validate(self, cfg: dict):
110 """Validation of dictionary ``cfg`` on :py:obj:`Config.SCHEMA`.
111 Validation is done by :py:obj:`validate`."""
112
113 return validate(self.cfg_schema, cfg, self.deprecated)
114
115 def update(self, upd_cfg: dict):
116 """Update this configuration by ``upd_cfg``."""
117
118 dict_deepupdate(self.cfg, upd_cfg)
119
120 def default(self, name: str):
121 """Returns default value of field ``name`` in ``self.cfg_schema``."""
122 return value(name, self.cfg_schema)
123
124 def get(self, name: str, default: Any = UNSET, replace: bool = True) -> Any:
125 """Returns the value to which ``name`` points in the configuration.
126
127 If there is no such ``name`` in the config and the ``default`` is
128 :py:obj:`UNSET`, a :py:obj:`KeyError` is raised.
129 """
130
131 parent = self._get_parent_dict(name)
132 val = parent.get(name.split('.')[-1], UNSET)
133 if val is UNSET:
134 if default is UNSET:
135 raise KeyError(name)
136 val = default
137
138 if replace and isinstance(val, str):
139 val = val % self
140 return val
141
142 def set(self, name: str, val):
143 """Set the value to which ``name`` points in the configuration.
144
145 If there is no such ``name`` in the config, a :py:obj:`KeyError` is
146 raised.
147 """
148 parent = self._get_parent_dict(name)
149 parent[name.split('.')[-1]] = val
150
151 def _get_parent_dict(self, name):
152 parent_name = '.'.join(name.split('.')[:-1])
153 if parent_name:
154 parent = value(parent_name, self.cfg)
155 else:
156 parent = self.cfg
157 if (parent is UNSET) or (not isinstance(parent, dict)):
158 raise KeyError(parent_name)
159 return parent
160
161 def path(self, name: str, default=UNSET):
162 """Get a :py:class:`pathlib.Path` object from a config string."""
163
164 val = self.get(name, default)
165 if val is UNSET:
166 if default is UNSET:
167 raise KeyError(name)
168 return default
169 return pathlib.Path(str(val))
170
171 def pyobj(self, name, default=UNSET):
172 """Get python object refered by full qualiffied name (FQN) in the config
173 string."""
174
175 fqn = self.get(name, default)
176 if fqn is UNSET:
177 if default is UNSET:
178 raise KeyError(name)
179 return default
180 (modulename, name) = str(fqn).rsplit('.', 1)
181 m = __import__(modulename, {}, {}, [name], 0)
182 return getattr(m, name)
183
184
185def toml_load(file_name):
186 if USE_TOMLLIB:
187 # Python >= 3.11
188 try:
189 with open(file_name, "rb") as f:
190 return tomllib.load(f)
191 except tomllib.TOMLDecodeError as exc:
192 msg = str(exc).replace('\t', '').replace('\n', ' ')
193 log.error("%s: %s", file_name, msg)
194 raise
195 # fallback to pytomlpp for Python < 3.11
196 try:
197 return pytomlpp.load(file_name)
198 except pytomlpp.DecodeError as exc:
199 msg = str(exc).replace('\t', '').replace('\n', ' ')
200 log.error("%s: %s", file_name, msg)
201 raise
202
203
204# working with dictionaries
205
206
207def value(name: str, data_dict: dict):
208 """Returns the value to which ``name`` points in the ``dat_dict``.
209
210 .. code: python
211
212 >>> data_dict = {
213 "foo": {"bar": 1 },
214 "bar": {"foo": 2 },
215 "foobar": [1, 2, 3],
216 }
217 >>> value('foobar', data_dict)
218 [1, 2, 3]
219 >>> value('foo.bar', data_dict)
220 1
221 >>> value('foo.bar.xxx', data_dict)
222 <UNSET>
223
224 """
225
226 ret_val = data_dict
227 for part in name.split('.'):
228 if isinstance(ret_val, dict):
229 ret_val = ret_val.get(part, UNSET)
230 if ret_val is UNSET:
231 break
232 return ret_val
233
234
236 schema_dict: typing.Dict, data_dict: typing.Dict, deprecated: typing.Dict[str, str]
237) -> typing.Tuple[bool, list]:
238 """Deep validation of dictionary in ``data_dict`` against dictionary in
239 ``schema_dict``. Argument deprecated is a dictionary that maps deprecated
240 configuration names to a messages::
241
242 deprecated = {
243 "foo.bar" : "config 'foo.bar' is deprecated, use 'bar.foo'",
244 "..." : "..."
245 }
246
247 The function returns a python tuple ``(is_valid, issue_list)``:
248
249 ``is_valid``:
250 A bool value indicating ``data_dict`` is valid or not.
251
252 ``issue_list``:
253 A list of messages (:py:obj:`SchemaIssue`) from the validation::
254
255 [schema warn] data_dict: deprecated 'fontlib.foo': <DEPRECATED['foo.bar']>
256 [schema invalid] data_dict: key unknown 'fontlib.foo'
257 [schema invalid] data_dict: type mismatch 'fontlib.foo': expected ..., is ...
258
259 If ``schema_dict`` or ``data_dict`` is not a dictionary type a
260 :py:obj:`SchemaIssue` is raised.
261
262 """
263 names = []
264 is_valid = True
265 issue_list = []
266
267 if not isinstance(schema_dict, dict):
268 raise SchemaIssue('invalid', "schema_dict is not a dict type")
269 if not isinstance(data_dict, dict):
270 raise SchemaIssue('invalid', f"data_dict issue{'.'.join(names)} is not a dict type")
271
272 is_valid, issue_list = _validate(names, issue_list, schema_dict, data_dict, deprecated)
273 return is_valid, issue_list
274
275
277 names: typing.List,
278 issue_list: typing.List,
279 schema_dict: typing.Dict,
280 data_dict: typing.Dict,
281 deprecated: typing.Dict[str, str],
282) -> typing.Tuple[bool, typing.List]:
283
284 is_valid = True
285
286 for key, data_value in data_dict.items():
287
288 names.append(key)
289 name = '.'.join(names)
290
291 deprecated_msg = deprecated.get(name)
292 # print("XXX %s: key %s // data_value: %s" % (name, key, data_value))
293 if deprecated_msg:
294 issue_list.append(SchemaIssue('warn', f"data_dict '{name}': deprecated - {deprecated_msg}"))
295
296 schema_value = value(name, schema_dict)
297 # print("YYY %s: key %s // schema_value: %s" % (name, key, schema_value))
298 if schema_value is UNSET:
299 if not deprecated_msg:
300 issue_list.append(SchemaIssue('invalid', f"data_dict '{name}': key unknown in schema_dict"))
301 is_valid = False
302
303 elif type(schema_value) != type(data_value): # pylint: disable=unidiomatic-typecheck
304 issue_list.append(
306 'invalid',
307 (f"data_dict: type mismatch '{name}':" f" expected {type(schema_value)}, is: {type(data_value)}"),
308 )
309 )
310 is_valid = False
311
312 elif isinstance(data_value, dict):
313 _valid, _ = _validate(names, issue_list, schema_dict, data_value, deprecated)
314 is_valid = is_valid and _valid
315 names.pop()
316
317 return is_valid, issue_list
318
319
320def dict_deepupdate(base_dict: dict, upd_dict: dict, names=None):
321 """Deep-update of dictionary in ``base_dict`` by dictionary in ``upd_dict``.
322
323 For each ``upd_key`` & ``upd_val`` pair in ``upd_dict``:
324
325 0. If types of ``base_dict[upd_key]`` and ``upd_val`` do not match raise a
326 :py:obj:`TypeError`.
327
328 1. If ``base_dict[upd_key]`` is a dict: recursively deep-update it by ``upd_val``.
329
330 2. If ``base_dict[upd_key]`` not exist: set ``base_dict[upd_key]`` from a
331 (deep-) copy of ``upd_val``.
332
333 3. If ``upd_val`` is a list, extend list in ``base_dict[upd_key]`` by the
334 list in ``upd_val``.
335
336 4. If ``upd_val`` is a set, update set in ``base_dict[upd_key]`` by set in
337 ``upd_val``.
338 """
339 # pylint: disable=too-many-branches
340 if not isinstance(base_dict, dict):
341 raise TypeError("argument 'base_dict' is not a ditionary type")
342 if not isinstance(upd_dict, dict):
343 raise TypeError("argument 'upd_dict' is not a ditionary type")
344
345 if names is None:
346 names = []
347
348 for upd_key, upd_val in upd_dict.items():
349 # For each upd_key & upd_val pair in upd_dict:
350
351 if isinstance(upd_val, dict):
352
353 if upd_key in base_dict:
354 # if base_dict[upd_key] exists, recursively deep-update it
355 if not isinstance(base_dict[upd_key], dict):
356 raise TypeError(f"type mismatch {'.'.join(names)}: is not a dict type in base_dict")
358 base_dict[upd_key],
359 upd_val,
360 names
361 + [
362 upd_key,
363 ],
364 )
365
366 else:
367 # if base_dict[upd_key] not exist, set base_dict[upd_key] from deepcopy of upd_val
368 base_dict[upd_key] = copy.deepcopy(upd_val)
369
370 elif isinstance(upd_val, list):
371
372 if upd_key in base_dict:
373 # if base_dict[upd_key] exists, base_dict[up_key] is extended by
374 # the list from upd_val
375 if not isinstance(base_dict[upd_key], list):
376 raise TypeError(f"type mismatch {'.'.join(names)}: is not a list type in base_dict")
377 base_dict[upd_key].extend(upd_val)
378
379 else:
380 # if base_dict[upd_key] doesn't exists, set base_dict[key] from a deepcopy of the
381 # list in upd_val.
382 base_dict[upd_key] = copy.deepcopy(upd_val)
383
384 elif isinstance(upd_val, set):
385
386 if upd_key in base_dict:
387 # if base_dict[upd_key] exists, base_dict[up_key] is updated by the set in upd_val
388 if not isinstance(base_dict[upd_key], set):
389 raise TypeError(f"type mismatch {'.'.join(names)}: is not a set type in base_dict")
390 base_dict[upd_key].update(upd_val.copy())
391
392 else:
393 # if base_dict[upd_key] doesn't exists, set base_dict[upd_key] from a copy of the
394 # set in upd_val
395 base_dict[upd_key] = upd_val.copy()
396
397 else:
398 # for any other type of upd_val replace or add base_dict[upd_key] by a copy
399 # of upd_val
400 base_dict[upd_key] = copy.copy(upd_val)
Any get(self, str name, Any default=UNSET, bool replace=True)
Definition config.py:124
pyobj(self, name, default=UNSET)
Definition config.py:171
Config from_toml(cls, pathlib.Path schema_file, pathlib.Path cfg_file, dict deprecated)
Definition config.py:70
set(self, str name, val)
Definition config.py:142
Any __getitem__(self, str key)
Definition config.py:106
path(self, str name, default=UNSET)
Definition config.py:161
__init__(self, typing.Dict cfg_schema, typing.Dict[str, str] deprecated)
Definition config.py:93
update(self, dict upd_cfg)
Definition config.py:115
__init__(self, typing.Literal['warn', 'invalid'] level, str msg)
Definition config.py:56
dict_deepupdate(dict base_dict, dict upd_dict, names=None)
Definition config.py:320
typing.Tuple[bool, typing.List] _validate(typing.List names, typing.List issue_list, typing.Dict schema_dict, typing.Dict data_dict, typing.Dict[str, str] deprecated)
Definition config.py:282
value(str name, dict data_dict)
Definition config.py:207
typing.Tuple[bool, list] validate(typing.Dict schema_dict, typing.Dict data_dict, typing.Dict[str, str] deprecated)
Definition config.py:237