This commit is contained in:
Timothy J. Baek 2024-08-25 17:54:51 +02:00
parent fd0370d801
commit a44bae2d3a

View File

@ -94,7 +94,7 @@ class Config(Base):
updated_at = Column(DateTime, nullable=True, onupdate=func.now()) updated_at = Column(DateTime, nullable=True, onupdate=func.now())
def load_initial_config(): def load_json_config():
with open(f"{DATA_DIR}/config.json", "r") as file: with open(f"{DATA_DIR}/config.json", "r") as file:
return json.load(file) return json.load(file)
@ -107,12 +107,14 @@ def save_to_db(data):
db.add(new_config) db.add(new_config)
else: else:
existing_config.data = data existing_config.data = data
existing_config.updated_at = datetime.now()
db.add(existing_config)
db.commit() db.commit()
# When initializing, check if config.json exists and migrate it to the database # When initializing, check if config.json exists and migrate it to the database
if os.path.exists(f"{DATA_DIR}/config.json"): if os.path.exists(f"{DATA_DIR}/config.json"):
data = load_initial_config() data = load_json_config()
save_to_db(data) save_to_db(data)
os.rename(f"{DATA_DIR}/config.json", f"{DATA_DIR}/old_config.json") os.rename(f"{DATA_DIR}/config.json", f"{DATA_DIR}/old_config.json")
@ -125,6 +127,15 @@ def save_config():
log.exception(e) log.exception(e)
def get_config():
with get_db() as db:
config_entry = db.query(Config).order_by(Config.id.desc()).first()
return config_entry.data if config_entry else {}
CONFIG_DATA = get_config()
def get_config_value(config_path: str): def get_config_value(config_path: str):
path_parts = config_path.split(".") path_parts = config_path.split(".")
cur_config = CONFIG_DATA cur_config = CONFIG_DATA
@ -144,7 +155,7 @@ class PersistentConfig(Generic[T]):
self.env_name = env_name self.env_name = env_name
self.config_path = config_path self.config_path = config_path
self.env_value = env_value self.env_value = env_value
self.config_value = self.load_latest_config_value(config_path) self.config_value = get_config_value(config_path)
if self.config_value is not None: if self.config_value is not None:
log.info(f"'{env_name}' loaded from the latest database entry") log.info(f"'{env_name}' loaded from the latest database entry")
self.value = self.config_value self.value = self.config_value
@ -154,43 +165,29 @@ class PersistentConfig(Generic[T]):
def __str__(self): def __str__(self):
return str(self.value) return str(self.value)
def load_latest_config_value(self, config_path: str): @property
with get_db() as db: def __dict__(self):
config_entry = db.query(Config).order_by(Config.id.desc()).first() raise TypeError(
if config_entry: "PersistentConfig object cannot be converted to dict, use config_get or .value instead."
try: )
path_parts = config_path.split(".")
config_value = config_entry.data def __getattribute__(self, item):
for key in path_parts: if item == "__dict__":
config_value = config_value[key] raise TypeError(
return config_value "PersistentConfig object cannot be converted to dict, use config_get or .value instead."
except KeyError: )
return None return super().__getattribute__(item)
def save(self): def save(self):
if self.env_value == self.value and self.config_value == self.value:
return
log.info(f"Saving '{self.env_name}' to the database") log.info(f"Saving '{self.env_name}' to the database")
path_parts = self.config_path.split(".") path_parts = self.config_path.split(".")
with get_db() as db: sub_config = CONFIG_DATA
existing_config = db.query(Config).first() for key in path_parts[:-1]:
if existing_config: if key not in sub_config:
config = existing_config.data sub_config[key] = {}
for key in path_parts[:-1]: sub_config = sub_config[key]
if key not in config: sub_config[path_parts[-1]] = self.value
config[key] = {} save_to_db(CONFIG_DATA)
config = config[key]
config[path_parts[-1]] = self.value
else: # This case should not actually occur as there should always be at least one entry
new_data = {}
config = new_data
for key in path_parts[:-1]:
config[key] = {}
config = config[key]
config[path_parts[-1]] = self.value
new_config = Config(data=new_data, version=0)
db.add(new_config)
db.commit()
self.config_value = self.value self.config_value = self.value