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