diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-03-29 11:35:51 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-03-29 11:38:27 +0200 |
commit | 0ffe0731e6aca624695dd528761d2e459256ee67 (patch) | |
tree | 124b14d70b237289b1104aca76a0386557cebf7c /labbot/config.py | |
download | lab-bot-0ffe0731e6aca624695dd528761d2e459256ee67.tar.gz lab-bot-0ffe0731e6aca624695dd528761d2e459256ee67.zip |
Initial commit0.0.0
Diffstat (limited to 'labbot/config.py')
-rw-r--r-- | labbot/config.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/labbot/config.py b/labbot/config.py new file mode 100644 index 0000000..3e8bcba --- /dev/null +++ b/labbot/config.py @@ -0,0 +1,27 @@ +import os +import json +from appdirs import user_config_dir + +def config_dir() -> str: + path = user_config_dir("labbot") + os.makedirs(path, exist_ok=True) + + return path + +def write_config(name: str, data: dict) -> None: + conf_path = os.path.join(config_dir(), f"{name}.json") + + with open(conf_path, "w") as file: + json.dump(data, file) + + if data != read_config(name): + raise ValueError("Config could not be saved properly") + +def read_config(name: str) -> dict: + conf_path = os.path.join(config_dir(), f"{name}.json") + + try: + with open(conf_path, "r") as file: + return json.load(file) + except FileNotFoundError: + return {}
\ No newline at end of file |