Blame view

config/old_config/test_config_xml1.py 4.41 KB
ce5f363c   Alain Klotz   Script de test de...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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'
b604fd0a   Etienne Pallier   1 unique requirem...
101
   config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
ce5f363c   Alain Klotz   Script de test de...
102
103
104
105
106
107
108
109
110
111
112
   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))