From 0b6b390388a8e9d547c7bb9a1886abe6d82e5a50 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Wed, 1 Jan 2025 22:04:58 +1300 Subject: [PATCH] refactor interface parser to its own class --- meshchat.py | 40 ++---------------------- src/backend/interface_config_parser.py | 43 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 src/backend/interface_config_parser.py diff --git a/meshchat.py b/meshchat.py index a608ea9..5a4bc3a 100644 --- a/meshchat.py +++ b/meshchat.py @@ -24,6 +24,7 @@ from serial.tools import list_ports import database from src.backend.announce_handler import AnnounceHandler from src.backend.colour_utils import ColourUtils +from src.backend.interface_config_parser import InterfaceConfigParser from src.backend.lxmf_message_fields import LxmfImageField, LxmfFileAttachmentsField, LxmfFileAttachment, LxmfAudioField from src.backend.audio_call_manager import AudioCall, AudioCallManager @@ -621,43 +622,8 @@ class ReticulumMeshChat: data = await request.json() config = data.get('config') - # process config - interfaces = [] - current_interface = None - for line in config.splitlines(): - - # strip whitespace from either side of string - line = line.strip() - - # skip empty lines and commented out lines - if not line or line.startswith("#"): - continue - - # check if we found a line containing an interface name - if line.startswith("[[") and line.endswith("]]"): - - # we found a new interface, so add the previously parsed one to the interfaces list - if current_interface: - interfaces.append(current_interface) - - # get interface name by removing the "[[" and "]]" - interface_name = line[2:-2] - current_interface = { - "name": interface_name, - "type": None - } - - # we found a key=value line, so we will set these values on the interface - elif current_interface is not None and "=" in line: - line_parts = line.split("=", 1) - key = line_parts[0].strip() - value = line_parts[1].strip() - if key == "type": - current_interface["type"] = value - - # add the final interface to the list - if current_interface: - interfaces.append(current_interface) + # parse config + interfaces = InterfaceConfigParser.parse(config) return web.json_response({ "interfaces": interfaces diff --git a/src/backend/interface_config_parser.py b/src/backend/interface_config_parser.py new file mode 100644 index 0000000..8ac6601 --- /dev/null +++ b/src/backend/interface_config_parser.py @@ -0,0 +1,43 @@ +class InterfaceConfigParser: + + @staticmethod + def parse(text): + + # process config + interfaces = [] + current_interface = None + for line in text.splitlines(): + + # strip whitespace from either side of string + line = line.strip() + + # skip empty lines and commented out lines + if not line or line.startswith("#"): + continue + + # check if we found a line containing an interface name + if line.startswith("[[") and line.endswith("]]"): + + # we found a new interface, so add the previously parsed one to the interfaces list + if current_interface: + interfaces.append(current_interface) + + # get interface name by removing the "[[" and "]]" + interface_name = line[2:-2] + current_interface = { + "name": interface_name, + "type": None + } + + # we found a key=value line, so we will set these values on the interface + elif current_interface is not None and "=" in line: + line_parts = line.split("=", 1) + key = line_parts[0].strip() + value = line_parts[1].strip() + current_interface[key] = value + + # add the final interface to the list + if current_interface: + interfaces.append(current_interface) + + return interfaces