refactored the cli and forensics codebases as classes and into seperate
folders for readability and consistent API access
This commit is contained in:
@@ -1,26 +1,38 @@
|
||||
# src/ctf/config.py
|
||||
# {{{ imports
|
||||
import toml
|
||||
import os
|
||||
from pathlib import Path
|
||||
from platformdirs import user_config_dir
|
||||
# }}}
|
||||
|
||||
# {{{ load_config
|
||||
def load_config(config = f"{user_config_dir()}/ctf-config.toml") -> dict:
|
||||
p = Path(config)
|
||||
if p.exists():
|
||||
return toml.load(p)
|
||||
return {}
|
||||
# }}}
|
||||
|
||||
# {{{ write_config
|
||||
def write_config(data: dict, config = f"{user_config_dir()}/ctf"):
|
||||
with open(config, "w") as f:
|
||||
toml.dump(data, f)
|
||||
# {{{ 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
|
||||
config_data = load_config("/home/venus/code/ctf/config.toml")
|
||||
competition = config_data.get("Competition", {})
|
||||
enviroment = config_data.get("Enviroment", {})
|
||||
# Load config instance to expose default values
|
||||
_cfg = Config()
|
||||
competition = _cfg.data.get("Competition", {})
|
||||
enviroment = _cfg.data.get("Enviroment", {})
|
||||
# }}}
|
||||
|
||||
Reference in New Issue
Block a user