mirror of
https://git.srhtnin.garden/sarah/SaraView.git
synced 2026-10-14 18:38:11 +02:00
Initial commit
This commit is contained in:
commit
8aa223bc7b
40 files changed
+4320
No files matched your search
@@ -0,0 +1,67 @@
|
||||
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]:
|
||||
"""Load SaraView's TOML configuration from disk."""
|
||||
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]:
|
||||
"""Return the configured named roots as resolved, absolute paths."""
|
||||
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:
|
||||
"""Resolve a client-supplied relative path against a named root.
|
||||
|
||||
Raises ValueError if the root is unknown or the resolved path escapes it.
|
||||
"""
|
||||
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:
|
||||
"""Return True if an entry should be hidden as a SaraTag internal file."""
|
||||
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)
|
||||
Reference in new issue
Block a user