chg: [diagnostic] Improved config comparison

diagnosticTool
mokaddem 2019-06-20 15:40:14 +02:00
parent a50bab4e77
commit 4463361192
2 changed files with 23 additions and 14 deletions

View File

@ -125,21 +125,22 @@ def check_configuration(spinner):
cfg.read(configfile) cfg.read(configfile)
configuration_file = cfg configuration_file = cfg
cfg = {s: dict(cfg.items(s)) for s in cfg.sections()} cfg = {s: dict(cfg.items(s)) for s in cfg.sections()}
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg.default') configfile_default = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg.default')
cfg_default = configparser.ConfigParser() cfg_default = configparser.ConfigParser()
cfg_default.read(configfile) cfg_default.read(configfile_default)
cfg_default = {s: dict(cfg_default.items(s)) for s in cfg_default.sections()} cfg_default = {s: dict(cfg_default.items(s)) for s in cfg_default.sections()}
# Check if all fields from config.default exists in config # Check if all fields from config.default exists in config
result, faulties = diagnostic_util.dict_compare(cfg_default, cfg) result, faulties = diagnostic_util.dict_compare(cfg_default, cfg)
faulties = [item for sublist in faulties for item in sublist]
if result: if result:
port = configuration_file.get("Server", "port") port = configuration_file.get("Server", "port")
return (True, '') return (True, '')
else: else:
return (False, f'''Configuration incomplete. return_text = '''Configuration incomplete.
\tUpdate your configuration file `config.cfg`.\n\t Faulty fields: {", ".join(faulties)}''') \tUpdate your configuration file `config.cfg`.\n\t Faulty fields:\n'''
# TODO: propose auto fix for field_name in faulties:
return_text += f'\t\t- {field_name}\n'
return (False, return_text)
@add_spinner(name='dot') @add_spinner(name='dot')

View File

@ -1,19 +1,27 @@
def dict_compare(dict1, dict2): import configparser
def dict_compare(dict1, dict2, itercount=0):
dict1_keys = set(dict1.keys()) dict1_keys = set(dict1.keys())
dict2_keys = set(dict2.keys()) dict2_keys = set(dict2.keys())
intersection = dict1_keys.difference(dict2_keys) intersection = dict1_keys.difference(dict2_keys)
if len(intersection) > 0: faulties = []
if itercount > 0 and len(intersection) > 0:
return (False, list(intersection)) return (False, list(intersection))
flag_no_error = True flag_no_error = True
faulties = []
for k, v in dict1.items(): for k, v in dict1.items():
if (isinstance(v, dict)): if isinstance(v, dict):
status, faulty = dict_compare(v, dict2[k]) if k not in dict2:
flag_no_error = flag_no_error and status faulties.append({k: dict1[k]})
faulties.append(faulty) flag_no_error = False
else:
status, faulty = dict_compare(v, dict2[k], itercount+1)
flag_no_error = flag_no_error and status
if len(faulty) > 0:
faulties.append({k: faulty})
else: else:
(True, []) return (True, [])
if flag_no_error: if flag_no_error:
return (True, []) return (True, [])
else: else: