From bc63ada6d8d9e08e879726fdd387ca403921438e Mon Sep 17 00:00:00 2001 From: wade ~ Pack3tL0ss Date: Fri, 6 Sep 2024 17:37:33 -0500 Subject: [PATCH] :goal_net: Catch error if ser2net is corrupt/invalid (allow menu to load with default serial settings). Resolves #200 :ambulance: hotfix, last update introduced an AttributeError related to partially implemented outlet_group feature --- src/consolepi-menu.py | 2 +- src/pypkg/consolepi/config.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/consolepi-menu.py b/src/consolepi-menu.py index 758f2ca7..941879f1 100755 --- a/src/consolepi-menu.py +++ b/src/consolepi-menu.py @@ -193,7 +193,7 @@ def do_menu_load_warnings(self): log.show('No Local Adapters Detected') if log.error_msgs: # -- remove no ser2net.conf found msg if no local adapters - log.error_msgs = [m for m in log.error_msgs if 'ser2net' not in m] + log.error_msgs = [m for m in log.error_msgs if 'No ser2net configuration found' not in m] def picocom_help(self): print('----------------------- picocom Command Sequences -----------------------\n') diff --git a/src/pypkg/consolepi/config.py b/src/pypkg/consolepi/config.py index c22dd49c..b9d96abb 100644 --- a/src/pypkg/consolepi/config.py +++ b/src/pypkg/consolepi/config.py @@ -75,6 +75,7 @@ def __init__(self): self.hosts = self.get_hosts() self.power = self.cfg.get('power', False) self.do_dli_menu = None # updated in get_outlets_from_file() + self.outlet_groups = {} # updated in get_outlets_from_file() self.outlets = {} if not self.power else self.get_outlets_from_file() self.remotes = self.get_remotes_from_file() self.remote_update = self.get_remotes_from_file @@ -518,13 +519,18 @@ def get_ser2netv4(self, file: Path = None) -> Dict: ######################################################## ser2net_conf = {} if file is None and self.ser2net_file is None: - return () + return {} raw = self.ser2net_file.read_text() if not file else file.read_text() raw_mod = "\n".join([line if not line.startswith("connection") else f"{line.split('&')[-1]}:" for line in raw.splitlines()]) banner = [line for line in raw.splitlines() if line.startswith("define: &banner")] banner = None if not banner else banner[-1].replace("define: &banner", "").lstrip() - ser_dict = yaml.safe_load(raw_mod) + try: + ser_dict = yaml.safe_load(raw_mod) + except Exception as e: + log.error(f"Error ({e.__class__.__name__}) occured parsing {file or self.ser2net_file}, verify file contents.", show=True) + log.exception(e) + return {} for k, v in ser_dict.items(): if not isinstance(v, dict):