added state options as an object with dict storing to disk

This commit is contained in:
venus
2026-04-06 00:26:43 -05:00
parent b208cc2ddb
commit a74553bd88
4 changed files with 44 additions and 14 deletions

View File

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