added config class to validate config entries
This commit is contained in:
4
config.json
Normal file
4
config.json
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
{
|
||||
"data_dir": "/home/venus/code/ctf/"
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
# functions for commands needed
|
||||
# src/commands.py
|
||||
import os
|
||||
from ctf.utils import state
|
||||
from pathlib import path
|
||||
|
||||
def test():
|
||||
print("hello from test")
|
||||
def Set_Challenge(comp: str, chal: str, setDirectory: bool):
|
||||
# set the current challenge and competition from input
|
||||
if state.current_comp != comp:
|
||||
state.current_comp=comp
|
||||
state.comp_dir=pathlib
|
||||
# TODO archive the old competitions
|
||||
|
||||
if state.current_chal != chal:
|
||||
state.current_chal=chal
|
||||
print("challenge already set")
|
||||
# TODO archive the old challenges
|
||||
# TODO set the directory to challenge directory, with ignore option
|
||||
|
||||
# Read variables
|
||||
|
||||
# Initialize a challenge
|
||||
## Copy files into directory
|
||||
## add section to log
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import argparse
|
||||
from ctf.utils import state
|
||||
state.testVal = 123
|
||||
state.testVal2 = 123
|
||||
def main():
|
||||
print("Hello from ctf!")
|
||||
print(f"Stored: {state.testVal}")
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user