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