.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# pylint: disable=missing-module-docstring
3
4from __future__ import annotations
5
6import pathlib
7import msgspec
8
9from .cache import FaviconCacheConfig
10from .proxy import FaviconProxyConfig
11
12CONFIG_SCHEMA: int = 1
13"""Version of the configuration schema."""
14
15TOML_CACHE_CFG: dict[str, "FaviconConfig"] = {}
16"""Cache config objects by TOML's filename."""
17
18DEFAULT_CFG_TOML_PATH = pathlib.Path(__file__).parent / "favicons.toml"
19
20
21class FaviconConfig(msgspec.Struct): # pylint: disable=too-few-public-methods
22 """The class aggregates configurations of the favicon tools"""
23
24 cfg_schema: int
25 """Config's schema version. The specification of the version of the schema
26 is mandatory, currently only version :py:obj:`CONFIG_SCHEMA` is supported.
27 By specifying a version, it is possible to ensure downward compatibility in
28 the event of future changes to the configuration schema"""
29
30 cache: FaviconCacheConfig = msgspec.field(default_factory=FaviconCacheConfig)
31 """Setup of the :py:obj:`.cache.FaviconCacheConfig`."""
32
33 proxy: FaviconProxyConfig = msgspec.field(default_factory=FaviconProxyConfig)
34 """Setup of the :py:obj:`.proxy.FaviconProxyConfig`."""
35
36 @classmethod
37 def from_toml_file(cls, cfg_file: pathlib.Path, use_cache: bool) -> "FaviconConfig":
38 """Create a config object from a TOML file, the ``use_cache`` argument
39 specifies whether a cache should be used.
40 """
41
42 cached = TOML_CACHE_CFG.get(str(cfg_file))
43 if use_cache and cached:
44 return cached
45
46 with cfg_file.open("rb") as f:
47 data = f.read()
48
49 cfg = msgspec.toml.decode(data, type=_FaviconConfig)
50 schema = cfg.favicons.cfg_schema
51 if schema != CONFIG_SCHEMA:
52 raise ValueError(
53 f"config schema version {CONFIG_SCHEMA} is needed, version {schema} is given in {cfg_file}"
54 )
55
56 cfg = cfg.favicons
57 if use_cache and cached:
58 TOML_CACHE_CFG[str(cfg_file.resolve())] = cfg
59
60 return cfg
61
62
63class _FaviconConfig(msgspec.Struct): # pylint: disable=too-few-public-methods
64 # wrapper struct for root object "favicons."
65 favicons: FaviconConfig
"FaviconConfig" from_toml_file(cls, pathlib.Path cfg_file, bool use_cache)
Definition config.py:37