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

View File

@@ -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)