@@ -1,384 +1,338 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on September , 2012 |
|
3 | 3 | @author: |
|
4 | 4 | ''' |
|
5 | 5 | from xml.etree.ElementTree import Element, SubElement, ElementTree |
|
6 | 6 | from xml.etree import ElementTree as ET |
|
7 | 7 | from xml.dom import minidom |
|
8 | 8 | |
|
9 | 9 | import sys |
|
10 | 10 | |
|
11 | 11 | def prettify(elem): |
|
12 | 12 | """Return a pretty-printed XML string for the Element. |
|
13 | 13 | """ |
|
14 | 14 | rough_string = ET.tostring(elem, 'utf-8') |
|
15 | 15 | reparsed = minidom.parseString(rough_string) |
|
16 | 16 | return reparsed.toprettyxml(indent=" ") |
|
17 | 17 | |
|
18 | 18 | #def save(a, b): |
|
19 | 19 | # |
|
20 | 20 | # nameP = "Alexnder" |
|
21 | 21 | # descripcion = self.projectWindow.Text() |
|
22 | 22 | # id = 1 |
|
23 | 23 | # x = self.data.projectWindow.cmbbox.value() |
|
24 | 24 | # |
|
25 |
# projectObj = |
|
|
25 | # projectObj = Controller(id, name, description) | |
|
26 | 26 | # |
|
27 | 27 | # projectObj.setup(id, name, description) |
|
28 | 28 | |
|
29 |
class |
|
|
29 | class Controller(): | |
|
30 | 30 | |
|
31 | 31 | id = None |
|
32 | 32 | name = None |
|
33 | 33 | description = None |
|
34 | 34 | readBranchObjList = None |
|
35 | 35 | procBranchObjList = None |
|
36 | 36 | |
|
37 | 37 | def __init__(self): |
|
38 | 38 | |
|
39 | 39 | # self.id = id |
|
40 | 40 | # self.name = name |
|
41 | 41 | # self.description = description |
|
42 | 42 | |
|
43 | 43 | self.readBranchObjList = [] |
|
44 | 44 | self.procBranchObjList = [] |
|
45 | 45 | |
|
46 | 46 | def setParms(self, id, name, description): |
|
47 | 47 | |
|
48 | 48 | self.id = id |
|
49 | 49 | self.name = name |
|
50 | 50 | self.description = description |
|
51 | 51 | |
|
52 | 52 | def addReadBranch(self, dpath, dataformat, readMode, startDate='', endDate='', startTime='', endTime=''): |
|
53 | 53 | |
|
54 | 54 | id = len(self.readBranchObjList) + 1 |
|
55 | 55 | |
|
56 | 56 | readBranchObj = ReadBranch(id, dpath, dataformat, readMode, startDate, endDate, startTime, endTime) |
|
57 | 57 | |
|
58 | 58 | self.readBranchObjList.append(readBranchObj) |
|
59 | 59 | |
|
60 | 60 | return readBranchObj |
|
61 | 61 | |
|
62 | 62 | def addProcBranch(self, name): |
|
63 | 63 | |
|
64 | 64 | id = len(self.procBranchObjList) + 1 |
|
65 | 65 | |
|
66 | 66 | procBranchObj = ProcBranch(id, name) |
|
67 | 67 | |
|
68 | 68 | self.procBranchObjList.append(procBranchObj) |
|
69 | 69 | |
|
70 | 70 | return procBranchObj |
|
71 | 71 | |
|
72 | 72 | def makeXml(self): |
|
73 | 73 | |
|
74 |
projectElement = Element(' |
|
|
74 | projectElement = Element('Controller') | |
|
75 | 75 | projectElement.set('id', str(self.id)) |
|
76 | 76 | projectElement.set('name', self.name) |
|
77 | 77 | #projectElement.set('description', self.description) |
|
78 | 78 | |
|
79 | 79 | se = SubElement(projectElement, 'description',description=self.description)#ESTO ES LO ULTIMO QUE SE TRABAJO |
|
80 | 80 | #se.text = self.description #ULTIMA MODIFICACION PARA SACAR UN SUB ELEMENT |
|
81 | 81 | |
|
82 | 82 | for readBranchObj in self.readBranchObjList: |
|
83 | 83 | readBranchObj.makeXml(projectElement) |
|
84 | 84 | |
|
85 | 85 | for procBranchObj in self.procBranchObjList: |
|
86 | 86 | procBranchObj.makeXml(projectElement) |
|
87 | 87 | |
|
88 | 88 | self.projectElement = projectElement |
|
89 | 89 | |
|
90 | 90 | def writeXml(self, filename): |
|
91 | 91 | |
|
92 | 92 | self.makeXml() |
|
93 | 93 | ElementTree(self.projectElement).write(filename, method='xml') |
|
94 | 94 | print prettify(self.projectElement) |
|
95 | 95 | |
|
96 | 96 | def readXml(self, filename): |
|
97 | 97 | |
|
98 | 98 | #tree = ET.parse(filename) |
|
99 | 99 | self.projectElement = None |
|
100 | 100 | tree = ElementTree(self.projectElement).parse(filename) |
|
101 | 101 | |
|
102 | 102 | self.project = self.projectElement.tag |
|
103 | 103 | |
|
104 | 104 | self.id = self.projectElement.get('id') |
|
105 | 105 | self.name = self.projectElement.get('name') |
|
106 | 106 | |
|
107 | 107 | readElement = self.projectElement.getiterator('readBranch') |
|
108 | 108 | |
|
109 | 109 | root = tree.getroot() |
|
110 | 110 | self.project = root.tag |
|
111 | 111 | self.id = root.attrib.get('id') |
|
112 | 112 | self.name = root.attrib.get('name') |
|
113 | 113 | |
|
114 | 114 | for description in root.findall('description'): |
|
115 | 115 | description = description.get('description') |
|
116 | 116 | |
|
117 | 117 | self.description= description |
|
118 | 118 | |
|
119 | 119 | for readBranch in root.findall('readBranch'): |
|
120 | 120 | id = readBranch.get('id') |
|
121 | 121 | self.idrb=id |
|
122 | 122 | |
|
123 | 123 | for procBranch in root.findall('procBranch'): |
|
124 | 124 | id = readBranch.get('id') |
|
125 | 125 | name = readBranch.get('name') |
|
126 | 126 | self.idpb=id |
|
127 | 127 | self.nameBranch=name |
|
128 | # | |
|
129 | ####ESTO DEL MEDIO ESTABA COMENTADO | |
|
130 | # print root.tag , root.attrib | |
|
131 | # | |
|
132 | # print root.attrib.get('id') | |
|
133 | # print root.attrib.get('name') | |
|
134 | ||
|
135 | ||
|
136 | # for description in root.findall('description'): | |
|
137 | # description = root.find('description').text | |
|
138 | # name = root.get('name') | |
|
139 | # print name, description | |
|
140 | ||
|
141 | # description=root.find('description').text | |
|
142 | # print description | |
|
143 | # ESTO FUNCIONABA HACIA ABAJO | |
|
144 | 128 | |
|
145 | 129 | root=tree.getroot() |
|
146 | 130 | print root.tag , root.attrib |
|
147 | 131 | for child in root: |
|
148 | 132 | print child.tag ,child.attrib |
|
149 | 133 | for child in child: |
|
150 | 134 | print child.tag ,child.attrib |
|
151 | 135 | for child in child: |
|
152 | 136 | print child.tag ,child.attrib |
|
153 | 137 | for child in child: |
|
154 | 138 | print child.tag ,child.attrib |
|
155 | 139 | # |
|
156 | 140 | class ReadBranch(): |
|
157 | 141 | |
|
158 | 142 | id = None |
|
159 | 143 | dpath = None |
|
160 | 144 | dataformat = None |
|
161 | 145 | readMode = None |
|
162 | 146 | startDate = None |
|
163 | 147 | endDate = None |
|
164 | 148 | startTime = None |
|
165 | 149 | endTime = None |
|
166 | 150 | |
|
167 | 151 | def __init__(self, id, dpath, dataformat, readMode, startDate, endDate, startTime, endTime): |
|
168 | 152 | |
|
169 | 153 | self.id = id |
|
170 | 154 | self.dpath = dpath |
|
171 | 155 | self.dataformat = dataformat |
|
172 | 156 | self.readMode = readMode |
|
173 | 157 | self.startDate = startDate |
|
174 | 158 | self.endDate = endDate |
|
175 | 159 | self.startTime = startTime |
|
176 | 160 | self.endTime = endTime |
|
177 | 161 | |
|
178 | 162 | def makeXml(self, projectElement): |
|
179 | 163 | |
|
180 | 164 | readBranchElement = SubElement(projectElement, 'readBranch') |
|
181 | 165 | readBranchElement.set('id', str(self.id)) |
|
182 | 166 | |
|
183 | # readBranchElement.set('dpath', self.dpath) | |
|
184 | # readBranchElement.set('dataformat', self.dataformat) | |
|
185 | # readBranchElement.set('startDate', self.startDate) | |
|
186 | # readBranchElement.set('endDate', self.endDate) | |
|
187 | # readBranchElement.set('startTime', self.startTime) | |
|
188 | # readBranchElement.set('endTime', self.endTime) | |
|
189 | # readBranchElement.set('readMode', str(self.readMode)) | |
|
190 | ||
|
191 | # se = SubElement(readBranchElement, 'dpath')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
192 | # se.text = self.dpath | |
|
193 | # | |
|
194 | # se = SubElement(readBranchElement, 'dataformat')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
195 | # se.text = self.dataformat | |
|
196 | # | |
|
197 | # se = SubElement(readBranchElement, 'startDate')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
198 | # se.text = self.startDate | |
|
199 | # | |
|
200 | # se = SubElement(readBranchElement, 'endDate')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
201 | # se.text = self.endDate | |
|
202 | # | |
|
203 | # se = SubElement(readBranchElement, 'startTime')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
204 | # se.text = self.startTime | |
|
205 | # | |
|
206 | # se = SubElement(readBranchElement, 'endTime')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
207 | # se.text = self.endTime | |
|
208 | # | |
|
209 | # se = SubElement(readBranchElement, 'readMode')#ESTO ES LO ULTIMO QUE SE TRABAJO | |
|
210 | # se.text = str(self.readMode) | |
|
211 | ||
|
212 | ########################################################################## | |
|
213 | 167 | se = SubElement(readBranchElement, 'parameter', name='dpath' , value=self.dpath) |
|
214 | 168 | se = SubElement(readBranchElement, 'parameter', name='dataformat', value=self.dataformat) |
|
215 | 169 | se = SubElement(readBranchElement, 'parameter', name='startDate' , value=self.startDate) |
|
216 | 170 | se = SubElement(readBranchElement, 'parameter', name='endDate' , value=self.endDate) |
|
217 | 171 | se = SubElement(readBranchElement, 'parameter', name='startTime' , value=self.startTime) |
|
218 | 172 | se = SubElement(readBranchElement, 'parameter', name='endTime' , value=self.endTime) |
|
219 | 173 | se = SubElement(readBranchElement, 'parameter', name='readMode' , value=str(self.readMode)) |
|
220 | 174 | |
|
221 | 175 | |
|
222 | 176 | class ProcBranch(): |
|
223 | 177 | |
|
224 | 178 | id = None |
|
225 | 179 | name = None |
|
226 | 180 | |
|
227 | 181 | upObjList = None |
|
228 | 182 | |
|
229 | 183 | def __init__(self, id, name): |
|
230 | 184 | |
|
231 | 185 | self.id = id |
|
232 | 186 | self.name = name |
|
233 | 187 | |
|
234 | 188 | self.upObjList = [] |
|
235 | 189 | |
|
236 | 190 | def addUP(self, name, type): |
|
237 | 191 | |
|
238 | 192 | id = len(self.upObjList) + 1 |
|
239 | 193 | |
|
240 | upObj = UP(id, name, type) | |
|
194 | upObj = UPConf(id, name, type) | |
|
241 | 195 | |
|
242 | 196 | self.upObjList.append(upObj) |
|
243 | 197 | |
|
244 | 198 | return upObj |
|
245 | 199 | |
|
246 | 200 | def makeXml(self, projectElement): |
|
247 | 201 | |
|
248 | 202 | procBranchElement = SubElement(projectElement, 'procBranch') |
|
249 | 203 | procBranchElement.set('id', str(self.id)) |
|
250 | 204 | procBranchElement.set('name', self.name) |
|
251 | 205 | |
|
252 | 206 | for upObj in self.upObjList: |
|
253 | 207 | upObj.makeXml(procBranchElement) |
|
254 | 208 | |
|
255 | class UP(): | |
|
209 | class UPConf(): | |
|
256 | 210 | |
|
257 | 211 | id = None |
|
258 | 212 | name = None |
|
259 | 213 | type = None |
|
260 | 214 | |
|
261 | 215 | opObjList = [] |
|
262 | 216 | |
|
263 | 217 | def __init__(self, id, name, type): |
|
264 | 218 | |
|
265 | 219 | self.id = id |
|
266 | 220 | self.name = name |
|
267 | 221 | self.type = type |
|
268 | 222 | |
|
269 | 223 | self.opObjList = [] |
|
270 | 224 | |
|
271 | 225 | def addOperation(self, name, priority): |
|
272 | 226 | |
|
273 | 227 | id = len(self.opObjList) + 1 |
|
274 | 228 | |
|
275 | opObj = Operation(id, name, priority) | |
|
229 | opObj = OperationConf(id, name, priority) | |
|
276 | 230 | |
|
277 | 231 | self.opObjList.append(opObj) |
|
278 | 232 | |
|
279 | 233 | return opObj |
|
280 | 234 | |
|
281 | 235 | def makeXml(self, procBranchElement): |
|
282 | 236 | |
|
283 | 237 | upElement = SubElement(procBranchElement, 'UP') |
|
284 | 238 | upElement.set('id', str(self.id)) |
|
285 | 239 | upElement.set('name', self.name) |
|
286 | 240 | upElement.set('type', self.type) |
|
287 | 241 | |
|
288 | 242 | for opObj in self.opObjList: |
|
289 | 243 | opObj.makeXml(upElement) |
|
290 | 244 | |
|
291 | class Operation(): | |
|
245 | class OperationConf(): | |
|
292 | 246 | |
|
293 | 247 | id = 0 |
|
294 | 248 | name = None |
|
295 | 249 | priority = None |
|
296 | 250 | parmObjList = [] |
|
297 | 251 | |
|
298 | 252 | def __init__(self, id, name, priority): |
|
299 | 253 | |
|
300 | 254 | self.id = id |
|
301 | 255 | self.name = name |
|
302 | 256 | self.priority = priority |
|
303 | 257 | |
|
304 | 258 | self.parmObjList = [] |
|
305 | 259 | |
|
306 | 260 | def addParameter(self, name, value): |
|
307 | 261 | |
|
308 | 262 | id = len(self.parmObjList) + 1 |
|
309 | 263 | |
|
310 | parmObj = Parameter(id, name, value) | |
|
264 | parmObj = ParameterConf(id, name, value) | |
|
311 | 265 | |
|
312 | 266 | self.parmObjList.append(parmObj) |
|
313 | 267 | |
|
314 | 268 | return parmObj |
|
315 | 269 | |
|
316 | 270 | def makeXml(self, upElement): |
|
317 | 271 | |
|
318 | 272 | opElement = SubElement(upElement, 'Operation') |
|
319 | 273 | opElement.set('id', str(self.id)) |
|
320 | 274 | opElement.set('name', self.name) |
|
321 | 275 | opElement.set('priority', str(self.priority)) |
|
322 | 276 | |
|
323 | 277 | for parmObj in self.parmObjList: |
|
324 | 278 | parmObj.makeXml(opElement) |
|
325 | 279 | |
|
326 | class Parameter(): | |
|
280 | class ParameterConf(): | |
|
327 | 281 | |
|
328 | 282 | id = None |
|
329 | 283 | name = None |
|
330 | 284 | value = None |
|
331 | 285 | |
|
332 | 286 | def __init__(self, id, name, value): |
|
333 | 287 | |
|
334 | 288 | self.id = id |
|
335 | 289 | self.name = name |
|
336 | 290 | self.value = value |
|
337 | 291 | |
|
338 | 292 | def makeXml(self, opElement): |
|
339 | 293 | |
|
340 | 294 | parmElement = SubElement(opElement, 'Parameter') |
|
341 | 295 | parmElement.set('name', self.name) |
|
342 | 296 | parmElement.set('value', self.value) |
|
343 | 297 | |
|
344 | 298 | def readXml(self, opElement): |
|
345 | 299 | |
|
346 | 300 | |
|
347 | 301 | pass |
|
348 | 302 | # se = SubElement(parmElement, 'value')#ESTO ES LO ULTIMO QUE SE TRABAJO |
|
349 | 303 | # se.text = self.value |
|
350 | 304 | |
|
351 | 305 | if __name__ == '__main__': |
|
352 | 306 | |
|
353 | 307 | desc = "Este es un test" |
|
354 | 308 | filename = "test.xml" |
|
355 | 309 | |
|
356 |
projectObj = |
|
|
310 | projectObj = Controller() | |
|
357 | 311 | |
|
358 | 312 | projectObj.setParms(id = '11', name='test01', description=desc) |
|
359 | 313 | |
|
360 | 314 | readBranchObj = projectObj.addReadBranch(dpath='mydata', dataformat='rawdata', readMode=0, startDate='1', endDate='3', startTime='4', endTime='5') |
|
361 | 315 | |
|
362 | 316 | procBranchObj = projectObj.addProcBranch(name='Branch1') |
|
363 | 317 | |
|
364 | 318 | procBranchObj1 = projectObj.addProcBranch(name='Branch2') |
|
365 | 319 | upObj1 = procBranchObj.addUP(name='UP1', type='Voltage') |
|
366 | 320 | upObj2 = procBranchObj.addUP(name='UP2', type='Voltage') |
|
367 | 321 | |
|
368 | 322 | opObj11 = upObj1.addOperation(name='removeDC', priority=1) |
|
369 | 323 | opObj11.addParameter(name='type', value='1') |
|
370 | 324 | |
|
371 | 325 | |
|
372 | 326 | opObj12 = upObj1.addOperation(name='decodification', priority=2) |
|
373 | 327 | opObj12.addParameter(name='ncode', value='2') |
|
374 | 328 | opObj12.addParameter(name='nbauds', value='8') |
|
375 | 329 | opObj12.addParameter(name='code1', value='001110011') |
|
376 | 330 | opObj12.addParameter(name='code2', value='001110011') |
|
377 | 331 | |
|
378 | 332 | print "Escribiendo el XML" |
|
379 | 333 | |
|
380 | 334 | projectObj.writeXml(filename) |
|
381 | 335 | |
|
382 | 336 | print "Leyendo el XML" |
|
383 | 337 | projectObj.readXml(filename) |
|
384 | 338 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now