updated config logic to read from one file
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
|
|
||||||
{
|
|
||||||
"data_dir": "/home/venus/code/ctf/"
|
|
||||||
}
|
|
||||||
4
config.toml
Normal file
4
config.toml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[Enviroment]
|
||||||
|
competition = "mycomp"
|
||||||
|
catagory = "somecat"
|
||||||
|
challenge = "somechal"
|
||||||
@@ -3,11 +3,31 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from ctf.utils import state
|
from ctf.utils import state
|
||||||
state.testVal = 123
|
|
||||||
state.testVal2 = 123
|
def setArguments():
|
||||||
|
parser = argparse.ArgumentParser( #type:ignore
|
||||||
|
prog="ctf",
|
||||||
|
description="A collection of cli tools to improve your ctf workflow",
|
||||||
|
epilog="")
|
||||||
|
parser.add_argument('action') # positional argument, action to be taken
|
||||||
|
parser.add_argument('-c', '--count') # option that takes a value
|
||||||
|
parser.add_argument('-v', '--verbose', action='store_true') # on/off flag
|
||||||
|
args = parser.parse_args()
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Hello from ctf!")
|
state.info = "me"
|
||||||
print(f"Stored: {state.testVal}")
|
# Parse arguments
|
||||||
|
# Current Ctf
|
||||||
|
# Current Challenge
|
||||||
|
# Validate args
|
||||||
|
# Call library functiions
|
||||||
|
# Set Ctf
|
||||||
|
args = setArguments()
|
||||||
|
if args.action == set:
|
||||||
|
print(f"setting ctf to{args.ctf}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -3,15 +3,16 @@
|
|||||||
|
|
||||||
import tomllib
|
import tomllib
|
||||||
import json
|
import json
|
||||||
|
import toml
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
# 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/
|
||||||
def set_defaults(self, key = "all"):
|
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")
|
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"):
|
def __init__(self, file_path = "user_config_dir/ctf.json"):
|
||||||
@@ -33,19 +34,19 @@ class ConfigManager:
|
|||||||
return f"ConfigManager({self.__dict__})"
|
return f"ConfigManager({self.__dict__})"
|
||||||
|
|
||||||
class StateManager:
|
class StateManager:
|
||||||
_state_file : Path
|
_path : Path
|
||||||
_data: dict
|
_data: dict
|
||||||
def __init__(self, data_dir: Path):
|
def __init__(self, data_dir: Path):
|
||||||
# DATA_DIR = Path(user_data_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/
|
# DATA_DIR = Path(user_data_dir("ctf")) #config directory is $XDG_CONFIG_HOME/ctf/
|
||||||
data_dir.mkdir(parents = True, exist_ok = True)
|
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, "_path", "user_config_dir/config.json") # set self._path to data_dir safely
|
||||||
object.__setattr__(self, "_data", self._load()) # load objects into self
|
object.__setattr__(self, "_data", self._load()) # load objects into self
|
||||||
|
|
||||||
def _load(self):
|
def _load(self):
|
||||||
if self._state_file.exists():
|
if self._path.exists():
|
||||||
with open(self._state_file, "r") as f:
|
with open(self._path, "r") as f:
|
||||||
return json.load(f)
|
return toml.load(f)
|
||||||
return{}
|
return{}
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
@@ -56,7 +57,7 @@ class StateManager:
|
|||||||
# use state.challenge = "run" to set challenge and run to dictionary
|
# use state.challenge = "run" to set challenge and run to dictionary
|
||||||
# writes the whole dict to disk
|
# writes the whole dict to disk
|
||||||
self._data[name] = value
|
self._data[name] = value
|
||||||
with open(self._state_file, "w",) as f:
|
with open(self._path, "w",) as f:
|
||||||
json.dump(self._data, f, indent=4)
|
json.dump(self._data, f, indent=4)
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"StateManager({self.__dict__})"
|
return f"StateManager({self.__dict__})"
|
||||||
|
|||||||
Reference in New Issue
Block a user