@@ -1,338 +1,341 | |||
|
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 | 25 | # projectObj = Controller(id, name, description) |
|
26 | 26 | # |
|
27 | 27 | # projectObj.setup(id, name, description) |
|
28 | 28 | |
|
29 | 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 | 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 | 128 | |
|
129 | 129 | root=tree.getroot() |
|
130 | 130 | print root.tag , root.attrib |
|
131 | 131 | for child in root: |
|
132 | 132 | print child.tag ,child.attrib |
|
133 | 133 | for child in child: |
|
134 | 134 | print child.tag ,child.attrib |
|
135 | 135 | for child in child: |
|
136 | 136 | print child.tag ,child.attrib |
|
137 | 137 | for child in child: |
|
138 | 138 | print child.tag ,child.attrib |
|
139 | 139 | # |
|
140 | 140 | class ReadBranch(): |
|
141 | 141 | |
|
142 | 142 | id = None |
|
143 | 143 | dpath = None |
|
144 | 144 | dataformat = None |
|
145 | 145 | readMode = None |
|
146 | 146 | startDate = None |
|
147 | 147 | endDate = None |
|
148 | 148 | startTime = None |
|
149 | 149 | endTime = None |
|
150 | 150 | |
|
151 | 151 | def __init__(self, id, dpath, dataformat, readMode, startDate, endDate, startTime, endTime): |
|
152 | 152 | |
|
153 | 153 | self.id = id |
|
154 | 154 | self.dpath = dpath |
|
155 | 155 | self.dataformat = dataformat |
|
156 | 156 | self.readMode = readMode |
|
157 | 157 | self.startDate = startDate |
|
158 | 158 | self.endDate = endDate |
|
159 | 159 | self.startTime = startTime |
|
160 | 160 | self.endTime = endTime |
|
161 | 161 | |
|
162 | 162 | def makeXml(self, projectElement): |
|
163 | 163 | |
|
164 | 164 | readBranchElement = SubElement(projectElement, 'readBranch') |
|
165 | 165 | readBranchElement.set('id', str(self.id)) |
|
166 | 166 | |
|
167 | 167 | se = SubElement(readBranchElement, 'parameter', name='dpath' , value=self.dpath) |
|
168 | 168 | se = SubElement(readBranchElement, 'parameter', name='dataformat', value=self.dataformat) |
|
169 | 169 | se = SubElement(readBranchElement, 'parameter', name='startDate' , value=self.startDate) |
|
170 | 170 | se = SubElement(readBranchElement, 'parameter', name='endDate' , value=self.endDate) |
|
171 | 171 | se = SubElement(readBranchElement, 'parameter', name='startTime' , value=self.startTime) |
|
172 | 172 | se = SubElement(readBranchElement, 'parameter', name='endTime' , value=self.endTime) |
|
173 | 173 | se = SubElement(readBranchElement, 'parameter', name='readMode' , value=str(self.readMode)) |
|
174 | 174 | |
|
175 | 175 | |
|
176 | 176 | class ProcBranch(): |
|
177 | 177 | |
|
178 | 178 | id = None |
|
179 | 179 | name = None |
|
180 | 180 | |
|
181 | 181 | upObjList = None |
|
182 | 182 | |
|
183 | 183 | def __init__(self, id, name): |
|
184 | 184 | |
|
185 | 185 | self.id = id |
|
186 | 186 | self.name = name |
|
187 | 187 | |
|
188 | 188 | self.upObjList = [] |
|
189 | 189 | |
|
190 | 190 | def addUP(self, name, type): |
|
191 | 191 | |
|
192 | 192 | id = len(self.upObjList) + 1 |
|
193 | 193 | |
|
194 | 194 | upObj = UPConf(id, name, type) |
|
195 | 195 | |
|
196 | 196 | self.upObjList.append(upObj) |
|
197 | 197 | |
|
198 | 198 | return upObj |
|
199 | 199 | |
|
200 | 200 | def makeXml(self, projectElement): |
|
201 | 201 | |
|
202 | 202 | procBranchElement = SubElement(projectElement, 'procBranch') |
|
203 | 203 | procBranchElement.set('id', str(self.id)) |
|
204 | 204 | procBranchElement.set('name', self.name) |
|
205 | 205 | |
|
206 | 206 | for upObj in self.upObjList: |
|
207 | 207 | upObj.makeXml(procBranchElement) |
|
208 | 208 | |
|
209 | 209 | class UPConf(): |
|
210 | 210 | |
|
211 | 211 | id = None |
|
212 | 212 | name = None |
|
213 | 213 | type = None |
|
214 | 214 | |
|
215 | 215 | opObjList = [] |
|
216 | 216 | |
|
217 | 217 | def __init__(self, id, name, type): |
|
218 | 218 | |
|
219 | 219 | self.id = id |
|
220 | 220 | self.name = name |
|
221 | 221 | self.type = type |
|
222 | 222 | |
|
223 | 223 | self.opObjList = [] |
|
224 | 224 | |
|
225 | def addOperation(self, name, priority): | |
|
225 | def addOperation(self, name, priority, type='self'): | |
|
226 | 226 | |
|
227 | 227 | id = len(self.opObjList) + 1 |
|
228 | 228 | |
|
229 | opObj = OperationConf(id, name, priority) | |
|
229 | opObj = OperationConf(id, name, priority, type) | |
|
230 | 230 | |
|
231 | 231 | self.opObjList.append(opObj) |
|
232 | 232 | |
|
233 | 233 | return opObj |
|
234 | 234 | |
|
235 | 235 | def makeXml(self, procBranchElement): |
|
236 | 236 | |
|
237 | 237 | upElement = SubElement(procBranchElement, 'UP') |
|
238 | 238 | upElement.set('id', str(self.id)) |
|
239 | 239 | upElement.set('name', self.name) |
|
240 | 240 | upElement.set('type', self.type) |
|
241 | 241 | |
|
242 | 242 | for opObj in self.opObjList: |
|
243 | 243 | opObj.makeXml(upElement) |
|
244 | 244 | |
|
245 | 245 | class OperationConf(): |
|
246 | 246 | |
|
247 | 247 | id = 0 |
|
248 | 248 | name = None |
|
249 | 249 | priority = None |
|
250 | type = 'self' | |
|
251 | ||
|
250 | 252 | parmObjList = [] |
|
251 | 253 | |
|
252 | def __init__(self, id, name, priority): | |
|
254 | def __init__(self, id, name, priority, type): | |
|
253 | 255 | |
|
254 | 256 | self.id = id |
|
255 | 257 | self.name = name |
|
256 | 258 | self.priority = priority |
|
259 | self.type = type | |
|
257 | 260 | |
|
258 | 261 | self.parmObjList = [] |
|
259 | 262 | |
|
260 | 263 | def addParameter(self, name, value): |
|
261 | 264 | |
|
262 | 265 | id = len(self.parmObjList) + 1 |
|
263 | 266 | |
|
264 | 267 | parmObj = ParameterConf(id, name, value) |
|
265 | 268 | |
|
266 | 269 | self.parmObjList.append(parmObj) |
|
267 | 270 | |
|
268 | 271 | return parmObj |
|
269 | 272 | |
|
270 | 273 | def makeXml(self, upElement): |
|
271 | 274 | |
|
272 | 275 | opElement = SubElement(upElement, 'Operation') |
|
273 | 276 | opElement.set('id', str(self.id)) |
|
274 | 277 | opElement.set('name', self.name) |
|
275 | 278 | opElement.set('priority', str(self.priority)) |
|
276 | 279 | |
|
277 | 280 | for parmObj in self.parmObjList: |
|
278 | 281 | parmObj.makeXml(opElement) |
|
279 | 282 | |
|
280 | 283 | class ParameterConf(): |
|
281 | 284 | |
|
282 | 285 | id = None |
|
283 | 286 | name = None |
|
284 | 287 | value = None |
|
285 | 288 | |
|
286 | 289 | def __init__(self, id, name, value): |
|
287 | 290 | |
|
288 | 291 | self.id = id |
|
289 | 292 | self.name = name |
|
290 | 293 | self.value = value |
|
291 | 294 | |
|
292 | 295 | def makeXml(self, opElement): |
|
293 | 296 | |
|
294 | 297 | parmElement = SubElement(opElement, 'Parameter') |
|
295 | 298 | parmElement.set('name', self.name) |
|
296 | 299 | parmElement.set('value', self.value) |
|
297 | 300 | |
|
298 | 301 | def readXml(self, opElement): |
|
299 | 302 | |
|
300 | 303 | |
|
301 | 304 | pass |
|
302 | 305 | # se = SubElement(parmElement, 'value')#ESTO ES LO ULTIMO QUE SE TRABAJO |
|
303 | 306 | # se.text = self.value |
|
304 | 307 | |
|
305 | 308 | if __name__ == '__main__': |
|
306 | 309 | |
|
307 | 310 | desc = "Este es un test" |
|
308 | 311 | filename = "test.xml" |
|
309 | 312 | |
|
310 | 313 | projectObj = Controller() |
|
311 | 314 | |
|
312 | 315 | projectObj.setParms(id = '11', name='test01', description=desc) |
|
313 | 316 | |
|
314 | 317 | readBranchObj = projectObj.addReadBranch(dpath='mydata', dataformat='rawdata', readMode=0, startDate='1', endDate='3', startTime='4', endTime='5') |
|
315 | 318 | |
|
316 | 319 | procBranchObj = projectObj.addProcBranch(name='Branch1') |
|
317 | 320 | |
|
318 | 321 | procBranchObj1 = projectObj.addProcBranch(name='Branch2') |
|
319 | 322 | upObj1 = procBranchObj.addUP(name='UP1', type='Voltage') |
|
320 | 323 | upObj2 = procBranchObj.addUP(name='UP2', type='Voltage') |
|
321 | 324 | |
|
322 | 325 | opObj11 = upObj1.addOperation(name='removeDC', priority=1) |
|
323 | 326 | opObj11.addParameter(name='type', value='1') |
|
324 | 327 | |
|
325 | 328 | |
|
326 | 329 | opObj12 = upObj1.addOperation(name='decodification', priority=2) |
|
327 | 330 | opObj12.addParameter(name='ncode', value='2') |
|
328 | 331 | opObj12.addParameter(name='nbauds', value='8') |
|
329 | 332 | opObj12.addParameter(name='code1', value='001110011') |
|
330 | 333 | opObj12.addParameter(name='code2', value='001110011') |
|
331 | 334 | |
|
332 | 335 | print "Escribiendo el XML" |
|
333 | 336 | |
|
334 | 337 | projectObj.writeXml(filename) |
|
335 | 338 | |
|
336 | 339 | print "Leyendo el XML" |
|
337 | 340 | projectObj.readXml(filename) |
|
338 | 341 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now