added config class to validate config entries

This commit is contained in:
venus
2026-04-07 02:04:00 -05:00
parent f0d2f18364
commit 09b4947e6e
4 changed files with 45 additions and 14 deletions

4
config.json Normal file
View File

@@ -0,0 +1,4 @@
{
"data_dir": "/home/venus/code/ctf/"
}

View File

@@ -1,12 +1,22 @@
# functions for commands needed # functions for commands needed
# src/commands.py # src/commands.py
import os import os
from ctf.utils import state
from pathlib import path
def test(): def test():
print("hello from 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

View File

@@ -4,6 +4,7 @@
import argparse import argparse
from ctf.utils import state from ctf.utils import state
state.testVal = 123 state.testVal = 123
state.testVal2 = 123
def main(): def main():
print("Hello from ctf!") print("Hello from ctf!")
print(f"Stored: {state.testVal}") print(f"Stored: {state.testVal}")

View File

@@ -9,21 +9,34 @@ from platformdirs import user_data_dir
# Parse config file # 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(user_config_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/
CONFIG_DIR = Path("/home/venus/code/ctf/") def set_defaults(self, key = "all"):
CONFIG_FILE = CONFIG_DIR / "config.toml" if key == "data_dir" or key == "all" : self.data_dir = Path("user_data_dir/ctf")
if not CONFIG_FILE.exists():
print("nothing in config, relying on default opts") def __init__(self, file_path = "user_config_dir/ctf.json"):
#this is where to declare default vals if not otherwise specified _path = Path(file_path)
return {} 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: class StateManager:
_state_file : Path _state_file : Path
_data: dict _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(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) data_dir.mkdir(parents = True, exist_ok = True)
object.__setattr__(self, "_state_file", data_dir / "state.json") # set self._path to data_dir safely 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 self._data[name] = value
with open(self._state_file, "w",) as f: with open(self._state_file, "w",) as f:
json.dump(self._data, f, indent=4) 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)