diff --git a/.gitignore b/.gitignore index 505a3b1..b2a6283 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ wheels/ # Virtual environments .venv +state.json diff --git a/config.toml b/config.toml deleted file mode 100644 index c759ace..0000000 --- a/config.toml +++ /dev/null @@ -1,8 +0,0 @@ -#config -#file with global options -#key = "val" -[CTF-DATA] -current-challenge = "testChal" -current-comp = "testComp" -key-format = "SKY-aaaa-0000" -#comments ignored diff --git a/src/ctf/main.py b/src/ctf/main.py index 7f8299f..327e3ba 100644 --- a/src/ctf/main.py +++ b/src/ctf/main.py @@ -2,12 +2,11 @@ # Parses and calls commands import argparse -from ctf import commands -from ctf import utils +from ctf.utils import state +state.testVal = 123 def main(): print("Hello from ctf!") - utils.load_config() - commands.test() + print(f"Stored: {state.testVal}") if __name__ == "__main__": diff --git a/src/ctf/utils.py b/src/ctf/utils.py index e433674..0a99c42 100644 --- a/src/ctf/utils.py +++ b/src/ctf/utils.py @@ -2,12 +2,50 @@ # basic utilities import tomllib +import json from pathlib import Path from platformdirs import user_config_dir +from platformdirs import user_data_dir # Parse config file -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/") CONFIG_FILE = CONFIG_DIR / "config.toml" + def load_config(): - print(CONFIG_FILE) + 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 {} + +class StateManager: + _state_file : Path + _data: dict + def __init__(self): + # 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 + object.__setattr__(self, "_data", self._load()) # load objects into self + + def _load(self): + if self._state_file.exists(): + with open(self._state_file, "r") as f: + return json.load(f) + return{} + + def __getattr__(self, name): + # use state.challenge to return value for challenge from dictionary + return self._data.get(name) + + def __setattr__(self, name, value): + # use state.challenge = "run" to set challenge and run to dictionary + # writes the whole dict to disk + self._data[name] = value + with open(self._state_file, "w",) as f: + json.dump(self._data, f, indent=4) + + +state = StateManager() +