From 09b4947e6efae5a5aefbc727c973d269bf97cb8b Mon Sep 17 00:00:00 2001 From: venus Date: Tue, 7 Apr 2026 02:04:00 -0500 Subject: [PATCH] added config class to validate config entries --- config.json | 4 ++++ src/ctf/commands.py | 18 ++++++++++++++---- src/ctf/main.py | 1 + src/ctf/utils.py | 36 ++++++++++++++++++++++++++---------- 4 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..6fc9e1b --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ + +{ + "data_dir": "/home/venus/code/ctf/" +} diff --git a/src/ctf/commands.py b/src/ctf/commands.py index 8150abd..c6718de 100644 --- a/src/ctf/commands.py +++ b/src/ctf/commands.py @@ -1,12 +1,22 @@ # functions for commands needed # src/commands.py import os +from ctf.utils import state +from pathlib import path def test(): print("hello from test") +def Set_Challenge(comp: str, chal: str, setDirectory: bool): + # set the current challenge and competition from input + if state.current_comp != comp: + state.current_comp=comp + state.comp_dir=pathlib + # TODO archive the old competitions + + if state.current_chal != chal: + state.current_chal=chal + print("challenge already set") + # TODO archive the old challenges + # TODO set the directory to challenge directory, with ignore option -# Read variables -# Initialize a challenge -## Copy files into directory -## add section to log diff --git a/src/ctf/main.py b/src/ctf/main.py index 327e3ba..c7143f7 100644 --- a/src/ctf/main.py +++ b/src/ctf/main.py @@ -4,6 +4,7 @@ import argparse from ctf.utils import state state.testVal = 123 +state.testVal2 = 123 def main(): print("Hello from ctf!") print(f"Stored: {state.testVal}") diff --git a/src/ctf/utils.py b/src/ctf/utils.py index 3a58eb1..a6588ff 100644 --- a/src/ctf/utils.py +++ b/src/ctf/utils.py @@ -9,21 +9,34 @@ from platformdirs import user_data_dir # Parse config file -def load_config(): +class ConfigManager: # CONFIG_DIR = Path(user_config_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/ - CONFIG_DIR = Path("/home/venus/code/ctf/") - CONFIG_FILE = CONFIG_DIR / "config.toml" - if not CONFIG_FILE.exists(): - print("nothing in config, relying on default opts") - #this is where to declare default vals if not otherwise specified - return {} + def set_defaults(self, key = "all"): + if key == "data_dir" or key == "all" : self.data_dir = Path("user_data_dir/ctf") + + def __init__(self, file_path = "user_config_dir/ctf.json"): + _path = Path(file_path) + if _path.exists(): + _data = json.loads(_path.read_text()) + for key, value in _data.items(): + setattr(self, key, value) + else: + print("no config file found, loading defaults") + self.set_defaults() + ## validate config elements + self.data_dir = Path(self.data_dir) + if not self.data_dir.exists(): + self.set_defaults("data_dir") + + + def __repr__(self): + return f"ConfigManager({self.__dict__})" class StateManager: _state_file : Path _data: dict - def __init__(self): + def __init__(self, data_dir: Path): # DATA_DIR = Path(user_data_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/ - data_dir = Path("/home/venus/code/ctf/") data_dir.mkdir(parents = True, exist_ok = True) object.__setattr__(self, "_state_file", data_dir / "state.json") # set self._path to data_dir safely @@ -45,7 +58,10 @@ class StateManager: self._data[name] = value with open(self._state_file, "w",) as f: json.dump(self._data, f, indent=4) + def __repr__(self): + return f"StateManager({self.__dict__})" -state = StateManager() +config = ConfigManager("/home/venus/code/ctf/config.json") +state = StateManager(config.data_dir)