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 ) ] }