aboutsummaryrefslogtreecommitdiff
path: root/labbot/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'labbot/config.py')
-rw-r--r--labbot/config.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/labbot/config.py b/labbot/config.py
index f3da425..07c4d48 100644
--- a/labbot/config.py
+++ b/labbot/config.py
@@ -2,9 +2,96 @@ import os
import json
from typing import List, Dict, Any
from appdirs import user_config_dir # type: ignore
+from functools import wraps
CONFIG_FILE = "config.json"
+class Config:
+ def __init__(self, conf_name=None, name=None):
+ self.conf_name = conf_name
+ self.name = name
+
+ self.settings = {
+ "GLOBAL": {},
+ "GROUP": {},
+ "PROJECT": {},
+ }
+ self.group_id = None
+ self.project_id = None
+
+ if self.conf_name and self.name:
+ self.setup(self.conf_name, self.name)
+
+ def setup(self, conf_name, name):
+ instance_path = instance_config_dir(name)
+ if conf_name.endswith(".json"):
+ conf_name = conf_name[:-5]
+
+ if conf_name == "config":
+ raise ValueError("Config cannot be named config")
+
+ self.conf_name = conf_name
+ self.name = name
+
+ try:
+ global_data = self.settings.get("GLOBAL", {})
+
+ self.settings = json.load(
+ open(os.path.join(instance_path, f"{conf_name}.json")))
+
+ # write the hardcoded config data ontop of the loaded data
+ self.settings["GLOBAL"].update(global_data)
+ except (IOError, ValueError):
+ pass
+
+ def __getitem__(self, key):
+ try:
+ return self.settings["PROJECT"][str(self.project_id)][key]
+ except KeyError:
+ try:
+ return self.settings["GROUP"][str(self.group_id)][key]
+ except KeyError:
+ return self.settings["GLOBAL"][key]
+
+ def save(self):
+ instance_path = instance_config_dir(self.name)
+ conf_path = os.path.join(instance_path, f"{self.conf_name}.json")
+
+ with open(conf_path, "w") as f:
+ json.dump(self.settings, f)
+
+ def config_decorator(self, **dkwargs):
+ def decorator(func):
+ @wraps(func)
+ async def wrapper(*args, **kwargs):
+
+ full_kwargs = kwargs.copy()
+ full_kwargs.update(zip(func.__code__.co_varnames, args))
+ try:
+ event = full_kwargs["event"]
+ self.project_id = event.project_id
+ except KeyError:
+ pass
+
+ try:
+ return await func(*args, **kwargs)
+ finally:
+ self.group_id = None
+ self.project_id = None
+ self.save()
+
+ return wrapper
+ return decorator
+
+ def set_global_data(self, **kwargs):
+ self.settings["GLOBAL"] = kwargs
+
+ def set_group_data(self, group_id, **kwargs):
+ self.settings["GROUP"][group_id] = kwargs
+
+ def set_project_data(self, project_id, **kwargs):
+ self.settings["PROJECT"][project_id] = kwargs
+
def list_instances() -> List[str]:
instances = []
with os.scandir(config_dir()) as it: