2018-10-10 22:43:50 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import os.path
|
2018-10-27 00:33:45 +00:00
|
|
|
from collections import ChainMap
|
|
|
|
from configparser import ConfigParser
|
2018-10-10 22:43:50 +00:00
|
|
|
from shutil import copyfile
|
2018-10-27 00:33:45 +00:00
|
|
|
from typing import Optional, Any
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
from django.conf import settings as dj_settings
|
2018-10-10 22:43:50 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
from .models import UserSettings, Subscription
|
|
|
|
from .utils.extended_interpolation_with_env import ExtendedInterpolatorWithEnv
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
class AppSettings(ConfigParser):
|
|
|
|
_DEFAULT_INTERPOLATION = ExtendedInterpolatorWithEnv()
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(allow_no_value=True, *args, **kwargs)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def initialize(self):
|
2018-10-31 23:01:21 +00:00
|
|
|
self.read([dj_settings.DEFAULTS_FILE, dj_settings.CONFIG_FILE])
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def save(self):
|
2018-10-31 23:01:21 +00:00
|
|
|
if os.path.exists(dj_settings.CONFIG_FILE):
|
2018-10-27 00:33:45 +00:00
|
|
|
# Create a backup
|
2018-10-31 23:01:21 +00:00
|
|
|
copyfile(dj_settings.CONFIG_FILE, dj_settings.CONFIG_FILE + ".backup")
|
2018-10-27 00:33:45 +00:00
|
|
|
else:
|
|
|
|
# Ensure directory exists
|
2018-10-31 23:01:21 +00:00
|
|
|
settings_dir = os.path.dirname(dj_settings.CONFIG_FILE)
|
2018-10-27 00:33:45 +00:00
|
|
|
os.makedirs(settings_dir, exist_ok=True)
|
|
|
|
|
2018-10-31 23:01:21 +00:00
|
|
|
with open(dj_settings.CONFIG_FILE, 'w') as f:
|
2018-10-27 00:33:45 +00:00
|
|
|
self.write(f)
|
|
|
|
|
|
|
|
def __get_combined_dict(self, vars: Optional[Any], sub: Optional[Subscription], user: Optional[User]) -> ChainMap:
|
|
|
|
vars_dict = {}
|
|
|
|
sub_overloads_dict = {}
|
|
|
|
user_settings_dict = {}
|
|
|
|
|
|
|
|
if vars is not None:
|
|
|
|
vars_dict = vars
|
|
|
|
|
|
|
|
if sub is not None:
|
|
|
|
sub_overloads_dict = sub.get_overloads_dict()
|
|
|
|
|
|
|
|
if user is not None:
|
|
|
|
user_settings = UserSettings.find_by_user(user)
|
|
|
|
if user_settings is not None:
|
|
|
|
user_settings_dict = user_settings.to_dict()
|
|
|
|
|
|
|
|
return ChainMap(vars_dict, sub_overloads_dict, user_settings_dict)
|
|
|
|
|
|
|
|
def get_user(self, user: User, section: str, option: Any, vars=None, fallback=object()) -> str:
|
|
|
|
return super().get(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, None, user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getboolean_user(self, user: User, section: str, option: Any, vars=None, fallback=object()) -> bool:
|
|
|
|
return super().getboolean(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, None, user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getint_user(self, user: User, section: str, option: Any, vars=None, fallback=object()) -> int:
|
|
|
|
return super().getint(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, None, user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getfloat_user(self, user: User, section: str, option: Any, vars=None, fallback=object()) -> float:
|
|
|
|
return super().getfloat(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, None, user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def get_sub(self, sub: Subscription, section: str, option: Any, vars=None, fallback=object()) -> str:
|
|
|
|
return super().get(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, sub, sub.user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getboolean_sub(self, sub: Subscription, section: str, option: Any, vars=None, fallback=object()) -> bool:
|
|
|
|
return super().getboolean(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, sub, sub.user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getint_sub(self, sub: Subscription, section: str, option: Any, vars=None, fallback=object()) -> int:
|
|
|
|
return super().getint(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, sub, sub.user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
def getfloat_sub(self, sub: Subscription, section: str, option: Any, vars=None, fallback=object()) -> float:
|
|
|
|
return super().getfloat(section, option,
|
|
|
|
fallback=fallback,
|
|
|
|
vars=self.__get_combined_dict(vars, sub, sub.user))
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-27 00:33:45 +00:00
|
|
|
|
|
|
|
settings = AppSettings()
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_app_config():
|
|
|
|
settings.initialize()
|
|
|
|
__initialize_logger()
|
|
|
|
logging.info('Application started!')
|
|
|
|
|
|
|
|
|
|
|
|
def __initialize_logger():
|
2018-10-31 23:01:21 +00:00
|
|
|
log_dir = os.path.dirname(dj_settings.LOG_FILE)
|
|
|
|
os.makedirs(log_dir, exist_ok=True)
|
2018-10-27 00:33:45 +00:00
|
|
|
|
2018-10-31 23:01:21 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
filename=dj_settings.LOG_FILE,
|
|
|
|
level=dj_settings.LOG_LEVEL,
|
|
|
|
format=dj_settings.LOG_FORMAT)
|