1
2
3
4
5
6
7 from coherence.extern.et import ET, indent
8
17
18
20
25
32
34 for n in node:
35 if n.get('active','yes') == 'yes':
36 if len(n) == 0:
37 a = {}
38 for attr,value in n.items():
39 if attr == 'active':
40 continue
41 a[attr] = value
42 if len(a):
43 self.append(a)
44 else:
45 self.append(self.nodes_to_dict(n))
46
48
53
66
68 for attr,value in node.items():
69 if attr == 'active':
70 continue
71 self[attr] = value
72
73 for n in node:
74 if n.get('active','yes') == 'yes':
75 if len(n) == 0:
76 if n.text is not None and len(n.text)>0:
77 self[n.get('name',n.tag)] = n.text
78 for attr,value in n.items():
79 if attr == 'active':
80 continue
81 self[attr] = value
82 else:
83 tag = n.tag
84
85
86 self[n.get('name',tag)] = self.nodes_to_dict(n)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
121 """
122 an incomplete XML file to dict and vice versa mapper
123
124 - nodes with an attribute 'active' set to 'no' are ignored
125 and not transferred into the dict
126
127 - nodes with tags ending with 'list' are transferrend into
128 an item with the key = 'tag' and a list with the subnodes
129 as the value
130
131 at the moment we parse the xml file and create dicts or lists out
132 of the nodes, but maybe it is much easier to keep the xml structure
133 as it is and simulate the dict/list access behavior on it?
134
135 """
136
138 self.file = file
139 dict.__init__(self)
140 try:
141 xml = ET.parse(file)
142 except SyntaxError, msg:
143 raise SyntaxError, msg
144 except IOError, msg:
145 raise IOError, msg
146 except Exception, msg:
147 raise SyntaxError, msg
148
149 xmlroot = xml.getroot()
150 self.name = xmlroot.tag
151 self.from_element(xmlroot)
152
153 - def save(self,file=None):
169
170
171 if __name__ == '__main__':
172
173 import sys
174
175 config = Config(sys.argv[1])
176 print config
177 config['serverport'] = 55555
178 config['test'] = 'test'
179 config['logging']['level'] = 'info'
180 del config['controlpoint']
181
182 print config
183 config.save('/tmp/t')
184