|
|
@ -4,8 +4,37 @@ import sys
|
|
|
|
|
|
|
|
|
|
|
|
from attrdict import AttrDict
|
|
|
|
from attrdict import AttrDict
|
|
|
|
|
|
|
|
|
|
|
|
config_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'config.json')
|
|
|
|
|
|
|
|
base_config = {
|
|
|
|
class Singleton(type):
|
|
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
|
|
|
|
if cls not in cls._instances:
|
|
|
|
|
|
|
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cls._instances[cls]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AttrConfig(AttrDict):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Simple AttrDict subclass to return None when requested attribute does not exist
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
|
|
|
|
super().__init__(config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return super().__getattr__(item)
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Default behaviour
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config(object, metaclass=Singleton):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
base_config = {
|
|
|
|
'core': {
|
|
|
|
'core': {
|
|
|
|
'debug': False
|
|
|
|
'debug': False
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -13,7 +42,7 @@ base_config = {
|
|
|
|
'api_key': ''
|
|
|
|
'api_key': ''
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'sonarr': {
|
|
|
|
'sonarr': {
|
|
|
|
'url': 'http://localhost:8989',
|
|
|
|
'url': 'http://localhost:8989/',
|
|
|
|
'api_key': '',
|
|
|
|
'api_key': '',
|
|
|
|
'profile': 'HD-1080p',
|
|
|
|
'profile': 'HD-1080p',
|
|
|
|
'root_folder': '/tv/',
|
|
|
|
'root_folder': '/tv/',
|
|
|
@ -21,7 +50,7 @@ base_config = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'radarr': {
|
|
|
|
'radarr': {
|
|
|
|
'url': 'http://localhost:7878',
|
|
|
|
'url': 'http://localhost:7878/',
|
|
|
|
'api_key': '',
|
|
|
|
'api_key': '',
|
|
|
|
'profile': 'HD-1080p',
|
|
|
|
'profile': 'HD-1080p',
|
|
|
|
'root_folder': '/movies/'
|
|
|
|
'root_folder': '/movies/'
|
|
|
@ -64,52 +93,64 @@ base_config = {
|
|
|
|
'notifications': {
|
|
|
|
'notifications': {
|
|
|
|
'verbose': True
|
|
|
|
'verbose': True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config_path, logfile):
|
|
|
|
|
|
|
|
"""Initializes config"""
|
|
|
|
|
|
|
|
self.conf = None
|
|
|
|
|
|
|
|
|
|
|
|
class AttrConfig(AttrDict):
|
|
|
|
self.config_path = config_path
|
|
|
|
"""
|
|
|
|
self.log_path = logfile
|
|
|
|
Simple AttrDict subclass to return None when requested attribute does not exist
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
@property
|
|
|
|
super().__init__(config)
|
|
|
|
def cfg(self):
|
|
|
|
|
|
|
|
# Return existing loaded config
|
|
|
|
|
|
|
|
if self.conf:
|
|
|
|
|
|
|
|
return self.conf
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
# Built initial config if it doesn't exist
|
|
|
|
try:
|
|
|
|
if self.build_config():
|
|
|
|
return super().__getattr__(item)
|
|
|
|
print("Please edit the default configuration before running again!")
|
|
|
|
except AttributeError:
|
|
|
|
sys.exit(0)
|
|
|
|
pass
|
|
|
|
# Load config, upgrade if necessary
|
|
|
|
# Default behaviour
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
tmp = self.load_config()
|
|
|
|
|
|
|
|
self.conf, upgraded = self.upgrade_settings(tmp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Save config if upgraded
|
|
|
|
|
|
|
|
if upgraded:
|
|
|
|
|
|
|
|
self.dump_config()
|
|
|
|
|
|
|
|
print("New config options were added, adjust and restart!")
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self.conf
|
|
|
|
|
|
|
|
|
|
|
|
def build_config():
|
|
|
|
@property
|
|
|
|
if not os.path.exists(config_path):
|
|
|
|
def logfile(self):
|
|
|
|
print("Dumping default config to: %s" % config_path)
|
|
|
|
return self.log_path
|
|
|
|
with open(config_path, 'w') as fp:
|
|
|
|
|
|
|
|
json.dump(base_config, fp, sort_keys=True, indent=2)
|
|
|
|
def build_config(self):
|
|
|
|
|
|
|
|
if not os.path.exists(self.config_path):
|
|
|
|
|
|
|
|
print("Dumping default config to: %s" % self.config_path)
|
|
|
|
|
|
|
|
with open(self.config_path, 'w') as fp:
|
|
|
|
|
|
|
|
json.dump(self.base_config, fp, sort_keys=True, indent=2)
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dump_config(self):
|
|
|
|
def dump_config():
|
|
|
|
if os.path.exists(self.config_path):
|
|
|
|
if os.path.exists(config_path):
|
|
|
|
with open(self.config_path, 'w') as fp:
|
|
|
|
with open(config_path, 'w') as fp:
|
|
|
|
json.dump(self.conf, fp, sort_keys=True, indent=2)
|
|
|
|
json.dump(cfg, fp, sort_keys=True, indent=2)
|
|
|
|
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(self):
|
|
|
|
def load_config():
|
|
|
|
with open(self.config_path, 'r') as fp:
|
|
|
|
with open(config_path, 'r') as fp:
|
|
|
|
|
|
|
|
return AttrConfig(json.load(fp))
|
|
|
|
return AttrConfig(json.load(fp))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade_settings(self, currents):
|
|
|
|
def upgrade_settings(defaults, currents):
|
|
|
|
|
|
|
|
upgraded = False
|
|
|
|
upgraded = False
|
|
|
|
|
|
|
|
|
|
|
|
def inner_upgrade(default, current, key=None):
|
|
|
|
def inner_upgrade(default, current, key=None):
|
|
|
@ -128,7 +169,7 @@ def upgrade_settings(defaults, currents):
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
# iterate children
|
|
|
|
# iterate children
|
|
|
|
if isinstance(v, dict) or isinstance(v, list):
|
|
|
|
if isinstance(v, dict) or isinstance(v, list):
|
|
|
|
did_upgrade, merged[k] = inner_upgrade(default[k], current[k], key=k)
|
|
|
|
merged[k], did_upgrade = inner_upgrade(default[k], current[k], key=k)
|
|
|
|
sub_upgraded = did_upgrade if did_upgrade else sub_upgraded
|
|
|
|
sub_upgraded = did_upgrade if did_upgrade else sub_upgraded
|
|
|
|
|
|
|
|
|
|
|
|
elif isinstance(default, list) and key:
|
|
|
|
elif isinstance(default, list) and key:
|
|
|
@ -138,24 +179,7 @@ def upgrade_settings(defaults, currents):
|
|
|
|
sub_upgraded = True
|
|
|
|
sub_upgraded = True
|
|
|
|
print("Added to config option %r: %s" % (str(key), str(v)))
|
|
|
|
print("Added to config option %r: %s" % (str(key), str(v)))
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
return sub_upgraded, merged
|
|
|
|
return merged, sub_upgraded
|
|
|
|
|
|
|
|
|
|
|
|
upgraded, upgraded_settings = inner_upgrade(defaults, currents)
|
|
|
|
|
|
|
|
return upgraded, AttrConfig(upgraded_settings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upgraded_settings, upgraded = inner_upgrade(self.base_config, currents)
|
|
|
|
############################################################
|
|
|
|
return AttrConfig(upgraded_settings), upgraded
|
|
|
|
# LOAD CFG
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# dump/load config
|
|
|
|
|
|
|
|
if build_config():
|
|
|
|
|
|
|
|
print("Please edit the default configuration before running again!")
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
tmp = load_config()
|
|
|
|
|
|
|
|
upgraded, cfg = upgrade_settings(base_config, tmp)
|
|
|
|
|
|
|
|
if upgraded:
|
|
|
|
|
|
|
|
dump_config()
|
|
|
|
|
|
|
|
print("New config options were added, adjust and restart!")
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|