import json import os class Config: name = 'config' config = { "discord_token": None, #format: "dbname=xxx user=xxx password=xxx" "postgresql_conninfo": None, #format: "auth_token=xxx; ct0=xxx;" "x_cookies": None, #format: "C:/Storage/X/" - remember about trailing / "x_download_path": None, "pixiv_token": None, "pixiv_username": None, "pixiv_password": None, #format: "C:/Storage/Pixiv/" - remember about trailing / "pixiv_download_path": None } def __init__(self, name : str = "config"): '"config" name is used as global config' self.name = name if os.path.exists(f"configs/{name}.json"): with open(f"configs/{name}.json", "rt") as f: self.config = json.load(f) else: self.save() def __getitem__(self, key): if key not in self.config: self.config[key] = None self.save() return self.config[key] def __setitem__(self, key, value): self.config[key] = value self.save() def save(self): os.makedirs("configs", exist_ok=True) with open(f"configs/{self.name}.json", "wt") as f: json.dump(self.config, f, indent=1) Global_Config = Config()