From 2a6f67a1bee85b67e15cc1c75994e4f5058f9ccd Mon Sep 17 00:00:00 2001 From: paul-loedige Date: Tue, 15 Dec 2020 00:32:31 +0100 Subject: [PATCH] added base version of the xml reader --- xml_reader.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 xml_reader.py diff --git a/xml_reader.py b/xml_reader.py new file mode 100644 index 0000000..e9dedd2 --- /dev/null +++ b/xml_reader.py @@ -0,0 +1,64 @@ +import xml.etree.ElementTree as ET + +class xml_reader: + """ reader for the XML configuration """ + + def __init__(self, path): + """inits the reader + + Args: + path (string): path to the XML config file + """ + self.root = ET.parse(path).getroot() + + def get_device_names(self): + """returns the names of all devices in the config + + Returns: + list: names of the devices + """ + return [device.get("name") for device in self.root.findall('Device')] + + def get_value_info(self,device_name): + """returns the value information for a device + + Args: + device_name (string): the name of the relevant device + + Returns: + dict: {'type','unit','offset','factor'} + """ + if self.root.find("Device[@name='%s']" % device_name) is None: + return None + value_info = self.root.find( + "Device[@name='%s']/ValueInfo" % device_name) + return { + 'type': value_info.get('type'), + 'unit': value_info.get('unit'), + 'offset': None if value_info.find("Offset") is None + else value_info.find("Offset").text, + 'factor': None if value_info.find("Factor") is None + else value_info.find("Factor").text, + } + + def get_port(self,device_name): + """returns the port details for a device + + Args: + device_name (string): the name of the relevant device + + Returns: + dict: {'protocol', [list: pins]} + """ + if self.root.find("Device[@name='%s']" % device_name) is None: + return None + port = self.root.find( + "Device[@name='%s']/Port" % device_name) + return { + 'protocol': port.get('protocol'), + 'pins': [ + pin.text for pin in self.root.findall( + "Device[@name='%s']/Port/Pin" % device_name + ) + ] + } \ No newline at end of file