added state options as an object with dict storing to disk
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@ wheels/
|
|||||||
|
|
||||||
# Virtual environments
|
# Virtual environments
|
||||||
.venv
|
.venv
|
||||||
|
state.json
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -2,12 +2,11 @@
|
|||||||
# Parses and calls commands
|
# Parses and calls commands
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from ctf import commands
|
from ctf.utils import state
|
||||||
from ctf import utils
|
state.testVal = 123
|
||||||
def main():
|
def main():
|
||||||
print("Hello from ctf!")
|
print("Hello from ctf!")
|
||||||
utils.load_config()
|
print(f"Stored: {state.testVal}")
|
||||||
commands.test()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -2,12 +2,50 @@
|
|||||||
# basic utilities
|
# basic utilities
|
||||||
|
|
||||||
import tomllib
|
import tomllib
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from platformdirs import user_config_dir
|
from platformdirs import user_config_dir
|
||||||
|
from platformdirs import user_data_dir
|
||||||
# Parse config file
|
# 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_DIR = Path("/home/venus/code/ctf/")
|
||||||
CONFIG_FILE = CONFIG_DIR / "config.toml"
|
CONFIG_FILE = CONFIG_DIR / "config.toml"
|
||||||
|
|
||||||
|
|
||||||
def load_config():
|
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()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user