From bc00890cdabf26693cde981eedc8a126eeea8c10 Mon Sep 17 00:00:00 2001 From: l3uddz Date: Wed, 14 Mar 2018 12:54:46 +0000 Subject: [PATCH] upgrade config capabilities --- misc/config.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/misc/config.py b/misc/config.py index e465c08..6534178 100644 --- a/misc/config.py +++ b/misc/config.py @@ -66,16 +66,7 @@ base_config = { } }, 'notifications': { - 'verbose': True, - 'my slack': { - 'service': 'slack', - 'webhook_url': '' - }, - 'my pushover': { - 'service': 'pushover', - 'app_token': '', - 'user_token': '' - } + 'verbose': True } } cfg = None @@ -122,6 +113,27 @@ def load_config(): return AttrConfig(json.load(fp)) +def upgrade_settings(defaults, current): + upgraded = False + res = current.copy() + for k, v in defaults.items(): + if k not in current: + res[k] = v + upgraded = True + print("Added config option: %s" % k) + + if hasattr(v, 'items'): + if k in current: + sub_upgrade, res[k] = upgrade_settings(v, current[k]) + if sub_upgrade: + upgraded = True + else: + res[k] = v + upgraded = True + + return upgraded, AttrConfig(res) + + ############################################################ # LOAD CFG ############################################################ @@ -131,4 +143,9 @@ if build_config(): print("Please edit the default configuration before running again!") sys.exit(0) else: - cfg = load_config() + 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)