from __future__ import annotations import fnmatch import tomllib from pathlib import Path from typing import Any ApplicationDirectory = Path(__file__).resolve().parent ConfigPath = ApplicationDirectory / "SaraViewConf.toml" def LoadConfig() -> dict[str, Any]: if not ConfigPath.is_file(): raise FileNotFoundError(f"Missing configuration file: {ConfigPath}") with ConfigPath.open("rb") as ConfigFile: return tomllib.load(ConfigFile) Config = LoadConfig() def GetRoots() -> dict[str, Path]: RawRoots = Config.get("Roots", {}) Roots: dict[str, Path] = {} for Name, RawPath in RawRoots.items(): Roots[Name] = Path(RawPath).expanduser().resolve() return Roots def ResolvePath(RootName: str, RelativePath: str) -> Path: Roots = GetRoots() Root = Roots.get(RootName) if Root is None: raise ValueError(f"Unknown root: {RootName}") Candidate = (Root / RelativePath.lstrip("/")).resolve() if Candidate != Root and Root not in Candidate.parents: raise ValueError("Path escapes the configured root.") return Candidate def IsInternalPath(EntryPath: Path) -> bool: SaraTagConfig = Config.get("SaraTag", {}) if not SaraTagConfig.get("HideInternalFiles", True): return False Patterns = SaraTagConfig.get( "InternalPatterns", [".saratag", ".SaraTag*", "*.book.toml"] ) return any(fnmatch.fnmatch(EntryPath.name, Pattern) for Pattern in Patterns)