From f6b1c65faa1eceb778515249bda865a6ae0e5748 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Mon, 3 Feb 2025 01:23:26 +1300 Subject: [PATCH] use built in rns config parser for parsing interface config files --- src/backend/interface_config_parser.py | 63 +++++++++----------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/src/backend/interface_config_parser.py b/src/backend/interface_config_parser.py index 75ab0b2..94bf823 100644 --- a/src/backend/interface_config_parser.py +++ b/src/backend/interface_config_parser.py @@ -1,50 +1,31 @@ +import RNS.vendor.configobj + + class InterfaceConfigParser: @staticmethod def parse(text): - # process config + # get lines from provided text + lines = text.splitlines() + + # ensure [interfaces] section exists + if "[interfaces]" not in lines: + lines.insert(0, "[interfaces]") + + # parse lines as rns config object + config = RNS.vendor.configobj.ConfigObj(lines) + + # get interfaces from config + config_interfaces = config.get("interfaces") + + # process interfaces interfaces = [] - current_interface = None - for line in text.splitlines(): + for interface_name in config_interfaces: - # 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, - } - - # we found a key=value line, so we will set these values on the interface - elif current_interface is not None and "=" in line: - - # parse key and value - line_parts = line.split("=", 1) - key = line_parts[0].strip() - value = line_parts[1].strip().strip('"').strip("'") - - # skip empty values or values equal to "None" - if value is None or value == "None": - continue - - # set key/value on interface - current_interface[key] = value - - # add the final interface to the list - if current_interface: - interfaces.append(current_interface) + # ensure interface has a name + interface_config = config_interfaces[interface_name] + interface_config["name"] = interface_name + interfaces.append(interface_config) return interfaces