# python c:\srv\develop\pyros\config\test_config_xml1.py # Parameter attributes = section, key, value, type, unit, access, comment, label, reference, timestamp from lxml import etree def get_unit_component_params(config_filename, verbose=0): errno = 0 unit_component_params = [] try: tree = etree.parse(config_filename) # --- Get root from the tree root = tree.getroot() # === Verify the root tag is 'unit' verify_unit = root.tag if (verify_unit != "unit"): errno = 1 if (verbose==1): print("Probleme car le tag root n'est pas 'unit'"); return (errno, unit_component_params); # === Get the alias value of the root tag 'unit' alias_unit = root.attrib['alias'] if (alias_unit == ""): errno = 1 if (verbose==1): print("Probleme car le tag root n'a pas d'attribut alias"); return (errno, unit_component_params); # === Get the mount aliases alias_mounts = [] for elem in root.xpath("/unit/mount"): # print(elem.tag, elem.attrib) alias_mounts.append(elem.attrib['alias']) if (verbose==1): print("alias_mounts = {}".format(alias_mounts)) # === Get the channel aliases alias_channels = [] for elem in root.xpath("/unit/channel"): # print(elem.tag, elem.attrib) alias_channels.append(elem.attrib['alias']) if (verbose==1): print("alias_channels = {}".format(alias_channels)) # === Get the association of channels for this mount unit_components = [] for elem in root: if elem.tag == "param": attributes = elem.attrib if attributes['section'] != "instanciation": continue alias_param_unit = attributes['value'] for alias_mount in alias_mounts: if alias_param_unit == alias_mount: key = attributes['key'] dico = {'tag':'mount','instanciation':key,'alias':alias_param_unit} unit_components.append(dico) for alias_channel in alias_channels: if alias_param_unit == alias_channel: key = attributes['key'] dico = {'tag':'channel','instanciation':key,'alias':alias_param_unit} unit_components.append(dico) if (verbose==1): print("unit_components = {}".format(unit_components)) # === Get detailed informations about the unit components for unit_component in unit_components: tag = unit_component['tag'] instanciation = unit_component['instanciation'] alias = unit_component['alias'] params = [] for elem in root: if (elem.tag == tag) and (elem.attrib['alias'] == alias): for el in elem: if (el.tag == "param"): params.append(el.attrib) unit_component_params.append({'component':tag, 'instanciation':instanciation, 'alias':alias, 'params':params}) if (verbose==1): print("unit_component_params = {}".format(unit_component_params)) except: errno = 100 return (errno, unit_component_params) def get_paramvalue(unit_component_params, instanciation, section, key): errno = 0 paramvalue = "" for unit_component_param in unit_component_params: component_instanciation = unit_component_param['instanciation'] if (component_instanciation == instanciation): for param in unit_component_param['params']: param_section = param['section'] param_key = param['key'] if (param_section == section) and (param_key == key): paramvalue = param['value'] break if (paramvalue != ""): break return (errno, paramvalue) if __name__ == "__main__": config_filename = 'c:/srv/develop/pyros_unit_taca/config_unit_taca.xml' config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml' errno, unit_component_params = get_unit_component_params(config_filename) print("errno = {}".format(errno)) errno, paramvalue = get_paramvalue(unit_component_params, 'mount1', 'localization', 'home') print("paramvalue = {}".format(paramvalue)) #print("unit_component_params = {}".format(unit_component_params)) #component = unit_component_params[0] #params = component['params'] #print("params = {}".format(params))