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