.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.botdetection.config Namespace Reference

Classes

class  Config
class  FALSE
class  SchemaIssue

Functions

 set_global_cfg (Config cfg)
Config get_global_cfg ()
 toml_load (file_name)
 value (str name, dict data_dict)
tuple[bool, list[str]] validate (dict[str, typing.Any] schema_dict, dict[str, typing.Any] data_dict, dict[str, str] deprecated)
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)
 dict_deepupdate (dict base_dict, dict upd_dict, names=None)

Variables

list __all__ = ['Config', 'UNSET', 'SchemaIssue', 'set_global_cfg', 'get_global_cfg']
 log = logging.getLogger(__name__)
Config CFG = None
 UNSET = FALSE('<UNSET>')

Detailed Description

Configuration class :py:class:`Config` with deep-update, schema validation
and deprecated names.

The :py:class:`Config` class implements a configuration that is based on
structured dictionaries.  The configuration schema is defined in a dictionary
structure and the configuration data is given in a dictionary structure.

Function Documentation

◆ _validate()

typing.Tuple[bool, typing.List] searx.botdetection.config._validate ( typing.List names,
typing.List issue_list,
typing.Dict schema_dict,
typing.Dict data_dict,
typing.Dict[str, str] deprecated )
protected

Definition at line 270 of file config.py.

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(
299 SchemaIssue(
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

References _validate(), and value().

Referenced by _validate(), and validate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dict_deepupdate()

searx.botdetection.config.dict_deepupdate ( dict base_dict,
dict upd_dict,
names = None )
Deep-update of dictionary in ``base_dict`` by dictionary in ``upd_dict``.

For each ``upd_key`` & ``upd_val`` pair in ``upd_dict``:

0. If types of ``base_dict[upd_key]`` and ``upd_val`` do not match raise a
   :py:obj:`TypeError`.

1. If ``base_dict[upd_key]`` is a dict: recursively deep-update it by ``upd_val``.

2. If ``base_dict[upd_key]`` not exist: set ``base_dict[upd_key]`` from a
   (deep-) copy of ``upd_val``.

3. If ``upd_val`` is a list, extend list in ``base_dict[upd_key]`` by the
   list in ``upd_val``.

4. If ``upd_val`` is a set, update set in ``base_dict[upd_key]`` by set in
   ``upd_val``.

Definition at line 314 of file config.py.

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")
351 dict_deepupdate(
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)

References dict_deepupdate().

Referenced by dict_deepupdate(), and searx.botdetection.config.Config.update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_global_cfg()

Config searx.botdetection.config.get_global_cfg ( )

Definition at line 31 of file config.py.

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

◆ set_global_cfg()

searx.botdetection.config.set_global_cfg ( Config cfg)

Definition at line 26 of file config.py.

26def set_global_cfg(cfg: Config):
27 global CFG # pylint: disable=global-statement
28 CFG = cfg
29
30

◆ toml_load()

searx.botdetection.config.toml_load ( file_name)

Definition at line 188 of file config.py.

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

Referenced by searx.botdetection.config.Config.from_toml().

Here is the caller graph for this function:

◆ validate()

tuple[bool, list[str]] searx.botdetection.config.validate ( dict[str, typing.Any] schema_dict,
dict[str, typing.Any] data_dict,
dict[str, str] deprecated )
Deep validation of dictionary in ``data_dict`` against dictionary in
``schema_dict``.  Argument deprecated is a dictionary that maps deprecated
configuration names to a messages::

    deprecated = {
        "foo.bar" : "config 'foo.bar' is deprecated, use 'bar.foo'",
        "..."     : "..."
    }

The function returns a python tuple ``(is_valid, issue_list)``:

``is_valid``:
  A bool value indicating ``data_dict`` is valid or not.

``issue_list``:
  A list of messages (:py:obj:`SchemaIssue`) from the validation::

      [schema warn] data_dict: deprecated 'fontlib.foo': <DEPRECATED['foo.bar']>
      [schema invalid] data_dict: key unknown 'fontlib.foo'
      [schema invalid] data_dict: type mismatch 'fontlib.foo': expected ..., is ...

If ``schema_dict`` or ``data_dict`` is not a dictionary type a
:py:obj:`SchemaIssue` is raised.

Definition at line 229 of file config.py.

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

References _validate().

Here is the call graph for this function:

◆ value()

searx.botdetection.config.value ( str name,
dict data_dict )
Returns the value to which ``name`` points in the ``dat_dict``.

.. code: python

    >>> data_dict = {
            "foo": {"bar": 1 },
            "bar": {"foo": 2 },
            "foobar": [1, 2, 3],
        }
    >>> value('foobar', data_dict)
    [1, 2, 3]
    >>> value('foo.bar', data_dict)
    1
    >>> value('foo.bar.xxx', data_dict)
    <UNSET>

Definition at line 201 of file config.py.

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

Referenced by searx.botdetection.config.Config._get_parent_dict(), _validate(), and searx.botdetection.config.Config.default().

Here is the caller graph for this function:

Variable Documentation

◆ __all__

list searx.botdetection.config.__all__ = ['Config', 'UNSET', 'SchemaIssue', 'set_global_cfg', 'get_global_cfg']
private

Definition at line 18 of file config.py.

◆ CFG

Config searx.botdetection.config.CFG = None

Definition at line 22 of file config.py.

◆ log

searx.botdetection.config.log = logging.getLogger(__name__)

Definition at line 20 of file config.py.

◆ UNSET

searx.botdetection.config.UNSET = FALSE('<UNSET>')

Definition at line 53 of file config.py.