new internal config incl. handling

This commit is contained in:
Florian Förster 2025-03-19 12:58:45 +01:00
parent 949657827a
commit f8cf4f7280
2 changed files with 84 additions and 0 deletions

72
src/pycage/config.py Normal file
View File

@ -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)

12
src/pycage/config.toml Normal file
View File

@ -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'