changed config to use dict and tomlload

This commit is contained in:
venus
2026-04-23 05:18:46 -05:00
parent df46501731
commit 13a5603773
5 changed files with 65 additions and 39 deletions

View File

@@ -9,34 +9,16 @@ from platformdirs import user_config_dir
# Parse config file
class StateManager:
_path : Path
_data: dict
def __init__(self):
# Assume data directory exists on install and defined in config
# TODO check datadir valid
object.__setattr__(self, "_path", "user_config_dir/config.json") # set self._path to data_dir safely
object.__setattr__(self, "_data", self._load()) # load objects into self
def load_config(config = f"{user_config_dir()}/ctf-config.toml") -> dict:
p = Path(config)
if p.exists():
return toml.load(p)
return{}
def _load(self):
if self._path.exists():
with open(self._path, "r") as f:
return toml.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._path, "w",) as f:
json.dump(self._data, f, indent=4)
def __repr__(self):
return f"StateManager({self.__dict__})"
def write_config(data: dict, config = f"{user_config_dir()}/ctf"):
with open(config, "w") as f:
toml.dump(data, f)
state = StateManager()
config = load_config("/home/venus/code/ctf/config.toml")