going to bed, updated the headers file to consolidate data and config dir, for now. Also began working on parsing user input. need to check out click
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
[Enviroment]
|
||||
[Competition]
|
||||
competition = "mycomp"
|
||||
catagory = "somecat"
|
||||
challenge = "somechal"
|
||||
[Enviroment]
|
||||
data_dir = "/home/venus/code/ctf/"
|
||||
|
||||
@@ -9,37 +9,12 @@ from platformdirs import user_config_dir
|
||||
# Parse config file
|
||||
|
||||
|
||||
class ConfigManager:
|
||||
CONFIG_DIR = Path(user_config_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/
|
||||
def set_defaults(self, key = "all"):
|
||||
# set all default options for the config
|
||||
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:
|
||||
_path : Path
|
||||
_data: dict
|
||||
def __init__(self, data_dir: Path):
|
||||
# DATA_DIR = Path(user_data_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/
|
||||
data_dir.mkdir(parents = True, exist_ok = True)
|
||||
|
||||
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
|
||||
|
||||
@@ -63,6 +38,5 @@ class StateManager:
|
||||
return f"StateManager({self.__dict__})"
|
||||
|
||||
|
||||
config = ConfigManager("/home/venus/code/ctf/config.json")
|
||||
state = StateManager(config.data_dir)
|
||||
state = StateManager()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user