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
|
|
|
|
|
2021-09-28 03:33:00 +00:00
|
|
|
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
|
|
|
|
2021-10-29 16:43:19 +00:00
|
|
|
def __init__(self, user_id=False):
|
|
|
|
self.user_id = user_id
|
2021-09-05 17:10:14 +00:00
|
|
|
self.config = self.get_config()
|
2021-10-29 16:43:19 +00:00
|
|
|
self.colors = self.get_colors()
|
2021-09-05 17:10:14 +00:00
|
|
|
|
|
|
|
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-10-29 16:43:19 +00:00
|
|
|
if self.user_id:
|
|
|
|
key = f"{self.user_id}:page_size"
|
|
|
|
page_size = RedisArchivist().get_message(key)["status"]
|
|
|
|
if page_size:
|
|
|
|
config["archive"]["page_size"] = page_size
|
|
|
|
|
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-10-20 11:41:39 +00:00
|
|
|
host_uid_env = os.environ.get("HOST_UID")
|
|
|
|
if host_uid_env:
|
|
|
|
host_uid = int(host_uid_env)
|
|
|
|
else:
|
|
|
|
host_uid = False
|
|
|
|
|
|
|
|
host_gid_env = os.environ.get("HOST_GID")
|
|
|
|
if host_gid_env:
|
|
|
|
host_gid = int(host_gid_env)
|
|
|
|
else:
|
|
|
|
host_gid = False
|
|
|
|
|
2021-10-28 08:49:58 +00:00
|
|
|
es_pass = os.environ.get("ELASTIC_PASSWORD")
|
|
|
|
es_user = os.environ.get("ELASTIC_USER", default="elastic")
|
|
|
|
|
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"),
|
2021-10-28 08:49:58 +00:00
|
|
|
"es_auth": (es_user, es_pass),
|
2021-10-20 11:41:39 +00:00
|
|
|
"HOST_UID": host_uid,
|
|
|
|
"HOST_GID": 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"""
|
2021-09-28 03:33:00 +00:00
|
|
|
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-10-29 15:37:31 +00:00
|
|
|
config_dict, config_value = key.split("_", maxsplit=1)
|
2021-09-05 17:10:14 +00:00
|
|
|
config[config_dict][config_value] = to_write
|
|
|
|
|
2021-09-28 03:33:00 +00:00
|
|
|
RedisArchivist().set_message("config", config, expire=False)
|
2021-09-20 10:45:01 +00:00
|
|
|
|
2021-10-29 15:37:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_user_config(form_post, user_id):
|
|
|
|
"""set values in redis for user settings"""
|
|
|
|
for key, value in form_post.items():
|
|
|
|
to_write = value[0]
|
|
|
|
if len(to_write):
|
|
|
|
if to_write.isdigit():
|
|
|
|
to_write = int(to_write)
|
|
|
|
message = {"status": to_write}
|
|
|
|
redis_key = f"{user_id}:{key}"
|
|
|
|
RedisArchivist().set_message(redis_key, message, expire=False)
|
|
|
|
|
2021-10-29 16:43:19 +00:00
|
|
|
def get_colors(self):
|
|
|
|
"""overwrite config if user has set custom values"""
|
|
|
|
colors = False
|
|
|
|
if self.user_id:
|
|
|
|
col_dict = RedisArchivist().get_message(f"{self.user_id}:colors")
|
|
|
|
colors = col_dict["status"]
|
|
|
|
|
|
|
|
if not colors:
|
|
|
|
colors = self.config["application"]["colors"]
|
|
|
|
|
|
|
|
self.config["application"]["colors"] = colors
|
|
|
|
return colors
|
|
|
|
|
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:
|
2021-09-28 03:33:00 +00:00
|
|
|
RedisArchivist().set_message("config", redis_config, expire=False)
|