tubearchivist/tubearchivist/home/src/config.py

104 lines
3.1 KiB
Python
Raw Normal View History

2021-09-05 17:10:14 +00:00
"""
Functionality:
- read and write config
- load config variables into redis
- needs to be a separate module to avoid circular import
"""
import json
import os
from home.src.helper import RedisArchivist
2021-09-05 17:10:14 +00:00
class AppConfig:
2021-09-21 09:25:22 +00:00
"""handle user settings and application variables"""
2021-09-05 17:10:14 +00:00
def __init__(self):
self.config = self.get_config()
def get_config(self):
2021-09-21 09:25:22 +00:00
"""get config from default file or redis if changed"""
2021-09-05 17:10:14 +00:00
config = self.get_config_redis()
if not config:
2021-09-20 10:45:01 +00:00
config = self.get_config_file()
2021-09-21 09:25:22 +00:00
config["application"].update(self.get_config_env())
2021-09-05 17:10:14 +00:00
return config
2021-09-20 10:45:01 +00:00
def get_config_file(self):
2021-09-21 09:25:22 +00:00
"""read the defaults from config.json"""
with open("home/config.json", "r", encoding="utf-8") as f:
2021-09-20 10:45:01 +00:00
config_str = f.read()
config_file = json.loads(config_str)
2021-09-21 09:25:22 +00:00
config_file["application"].update(self.get_config_env())
2021-09-20 10:45:01 +00:00
return config_file
@staticmethod
def get_config_env():
2021-09-21 09:25:22 +00:00
"""read environment application variables"""
2021-09-20 10:45:01 +00:00
application = {
2021-09-21 09:25:22 +00:00
"REDIS_HOST": os.environ.get("REDIS_HOST"),
"es_url": os.environ.get("ES_URL"),
"HOST_UID": int(os.environ.get("HOST_UID")),
"HOST_GID": int(os.environ.get("HOST_GID")),
2021-09-20 10:45:01 +00:00
}
return application
2021-09-05 17:10:14 +00:00
@staticmethod
def get_config_redis():
2021-09-21 09:25:22 +00:00
"""read config json set from redis to overwrite defaults"""
config = RedisArchivist().get_message("config")
2021-09-05 17:10:14 +00:00
if not list(config.values())[0]:
return False
return config
def update_config(self, form_post):
2021-09-21 09:25:22 +00:00
"""update config values from settings form"""
2021-09-05 17:10:14 +00:00
config = self.config
for key, value in form_post.items():
to_write = value[0]
if len(to_write):
2021-09-21 09:25:22 +00:00
if to_write == "0":
2021-09-05 17:10:14 +00:00
to_write = False
2021-09-21 09:25:22 +00:00
elif to_write == "1":
2021-09-20 10:45:01 +00:00
to_write = True
2021-09-05 17:10:14 +00:00
elif to_write.isdigit():
to_write = int(to_write)
2021-09-21 09:25:22 +00:00
config_dict, config_value = key.split(".")
2021-09-05 17:10:14 +00:00
config[config_dict][config_value] = to_write
RedisArchivist().set_message("config", config, expire=False)
2021-09-20 10:45:01 +00:00
def load_new_defaults(self):
2021-09-21 09:25:22 +00:00
"""check config.json for missing defaults"""
2021-09-20 10:45:01 +00:00
default_config = self.get_config_file()
redis_config = self.get_config_redis()
# check for customizations
if not redis_config:
return
needs_update = False
for key, value in default_config.items():
# missing whole main key
if key not in redis_config:
redis_config.update({key: value})
needs_update = True
continue
# missing nested values
for sub_key, sub_value in value.items():
if sub_key not in redis_config[key].keys():
redis_config[key].update({sub_key: sub_value})
needs_update = True
if needs_update:
RedisArchivist().set_message("config", redis_config, expire=False)