| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # pylint: disable=missing-module-docstring | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from __future__ import annotations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import pathlib | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  | import msgspec | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .cache import FaviconCacheConfig | 
					
						
							|  |  |  | from .proxy import FaviconProxyConfig | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CONFIG_SCHEMA: int = 1 | 
					
						
							|  |  |  | """Version of the configuration schema.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 18:08:40 +02:00
										 |  |  | TOML_CACHE_CFG: dict[str, "FaviconConfig"] = {} | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | """Cache config objects by TOML's filename.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 18:08:40 +02:00
										 |  |  | DEFAULT_CFG_TOML_PATH = pathlib.Path(__file__).parent / "favicons.toml" | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  | class FaviconConfig(msgspec.Struct):  # pylint: disable=too-few-public-methods | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  |     """The class aggregates configurations of the favicon tools""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cfg_schema: int | 
					
						
							|  |  |  |     """Config's schema version.  The specification of the version of the schema
 | 
					
						
							|  |  |  |     is mandatory, currently only version :py:obj:`CONFIG_SCHEMA` is supported. | 
					
						
							|  |  |  |     By specifying a version, it is possible to ensure downward compatibility in | 
					
						
							|  |  |  |     the event of future changes to the configuration schema"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  |     cache: FaviconCacheConfig = FaviconCacheConfig | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  |     """Setup of the :py:obj:`.cache.FaviconCacheConfig`.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  |     proxy: FaviconProxyConfig = FaviconCacheConfig | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  |     """Setup of the :py:obj:`.proxy.FaviconProxyConfig`.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def from_toml_file(cls, cfg_file: pathlib.Path, use_cache: bool) -> "FaviconConfig": | 
					
						
							|  |  |  |         """Create a config object from a TOML file, the ``use_cache`` argument
 | 
					
						
							|  |  |  |         specifies whether a cache should be used. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 18:08:40 +02:00
										 |  |  |         cached = TOML_CACHE_CFG.get(str(cfg_file)) | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  |         if use_cache and cached: | 
					
						
							|  |  |  |             return cached | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with cfg_file.open("rb") as f: | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  |             data = f.read() | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  |         cfg = msgspec.toml.decode(data, type=_FaviconConfig) | 
					
						
							|  |  |  |         schema = cfg.favicons.cfg_schema | 
					
						
							|  |  |  |         if schema != CONFIG_SCHEMA: | 
					
						
							|  |  |  |             raise ValueError( | 
					
						
							|  |  |  |                 f"config schema version {CONFIG_SCHEMA} is needed, version {schema} is given in {cfg_file}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  |         cfg = cfg.favicons | 
					
						
							|  |  |  |         if use_cache and cached: | 
					
						
							|  |  |  |             TOML_CACHE_CFG[str(cfg_file.resolve())] = cfg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return cfg | 
					
						
							| 
									
										
										
										
											2024-08-19 17:47:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 21:17:26 +02:00
										 |  |  | class _FaviconConfig(msgspec.Struct):  # pylint: disable=too-few-public-methods | 
					
						
							|  |  |  |     # wrapper struct for root object "favicons." | 
					
						
							|  |  |  |     favicons: FaviconConfig |