From f8cf4f72800ca8cb5fa6ee920ebaea8948cfab91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=B6rster?= Date: Wed, 19 Mar 2025 12:58:45 +0100 Subject: [PATCH] new internal config incl. handling --- src/pycage/config.py | 72 ++++++++++++++++++++++++++++++++++++++++++ src/pycage/config.toml | 12 +++++++ 2 files changed, 84 insertions(+) create mode 100644 src/pycage/config.py create mode 100644 src/pycage/config.toml diff --git a/src/pycage/config.py b/src/pycage/config.py new file mode 100644 index 0000000..e50a407 --- /dev/null +++ b/src/pycage/config.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +import platform +from pathlib import Path +from typing import Any, Final + +from dopt_basics import configs, io + +from pycage.types import OsInfo + + +class LazyConfigLoader: + def __init__( + self, + cfg_path: Path, + ) -> None: + if not cfg_path.exists(): + raise FileNotFoundError(f"Config path does not seem to exist: {cfg_path}") + + self.cfg_path = cfg_path + self._cfg: dict[str, Any] | None = None + + def _load_config(self) -> None: + self._cfg = configs.load_toml(self.cfg_path) + + def __getitem__( + self, + key: str, + ) -> Any: + if self._cfg is None: + self._load_config() + + assert self._cfg is not None, "tried to access not loaded config" + return self._cfg[key] + + +class PycageConfigLoader(LazyConfigLoader): + def __init__( + self, + cfg_path: Path, + ) -> None: + super().__init__(cfg_path) + self._os_info: OsInfo | None = None + + @property + def os_info(self) -> OsInfo: + if self._os_info is None: + self._os_info = _parse_os_info(self) + + return self._os_info + + +def _parse_os_info( + cfg: PycageConfigLoader, +) -> OsInfo: + system = platform.system() + + os_file_info: OsInfo + match system: + case "Windows": + os_file_info = OsInfo(**cfg["platforms"]["win"]) + case _: + raise NotImplementedError(f"Platform {system} not implemented yet.") + + return os_file_info + + +cfg_path = io.search_file_iterative( + Path(__file__), glob_pattern="config.toml", stop_folder_name="pycage" +) +assert cfg_path is not None, "static config path not found" +CFG: Final[PycageConfigLoader] = PycageConfigLoader(cfg_path) diff --git a/src/pycage/config.toml b/src/pycage/config.toml new file mode 100644 index 0000000..2d102cf --- /dev/null +++ b/src/pycage/config.toml @@ -0,0 +1,12 @@ +[metadata] +URL = 'https://raw.githubusercontent.com/astral-sh/python-build-standalone/latest-release/latest-release.json' + +[package] +PACKAGE_TYPE = 'install_only_stripped' + +[platforms] +[platforms.win] +PLATFORM = 'x86_64' +OS = 'pc-windows-msvc' +FILE_EXT = '.tar.gz' +INTERPRETER = 'python.exe'