39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
# src/ctf/config.py
|
|
# {{{ imports
|
|
import toml
|
|
import os
|
|
from pathlib import Path
|
|
# }}}
|
|
|
|
# {{{ Config
|
|
class Config:
|
|
"""Manages CTF challenge configuration loading and persistence."""
|
|
def __init__(self, path: str | Path | None = None):
|
|
if path is None:
|
|
# Check environment variable first, then fallback to hardcoded path
|
|
path = os.environ.get("CTF_CONFIG_PATH", "/home/venus/code/ctf/config.toml")
|
|
self.path = Path(path)
|
|
self.data = self._load()
|
|
|
|
def _load(self) -> dict:
|
|
if self.path.exists():
|
|
try:
|
|
return toml.load(self.path)
|
|
except Exception:
|
|
return {}
|
|
return {}
|
|
|
|
def save(self, data: dict):
|
|
self.data = data
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
with open(self.path, "w") as f:
|
|
toml.dump(self.data, f)
|
|
# }}}
|
|
|
|
# {{{ exports
|
|
# Load config instance to expose default values
|
|
_cfg = Config()
|
|
competition = _cfg.data.get("Competition", {})
|
|
enviroment = _cfg.data.get("Enviroment", {})
|
|
# }}}
|