@@ -1,1272 +1,1272 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on September , 2012 |
|
3 | 3 | @author: |
|
4 | 4 | ''' |
|
5 | 5 | |
|
6 | 6 | import sys |
|
7 | 7 | import ast |
|
8 | 8 | import datetime |
|
9 | 9 | import traceback |
|
10 | 10 | import schainpy |
|
11 | 11 | import schainpy.admin |
|
12 | 12 | |
|
13 | 13 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring |
|
14 | 14 | from xml.dom import minidom |
|
15 | 15 | |
|
16 | 16 | from schainpy.model import * |
|
17 | 17 | from time import sleep |
|
18 | 18 | |
|
19 | 19 | def prettify(elem): |
|
20 | 20 | """Return a pretty-printed XML string for the Element. |
|
21 | 21 | """ |
|
22 | 22 | rough_string = tostring(elem, 'utf-8') |
|
23 | 23 | reparsed = minidom.parseString(rough_string) |
|
24 | 24 | return reparsed.toprettyxml(indent=" ") |
|
25 | 25 | |
|
26 | 26 | class ParameterConf(): |
|
27 | 27 | |
|
28 | 28 | id = None |
|
29 | 29 | name = None |
|
30 | 30 | value = None |
|
31 | 31 | format = None |
|
32 | 32 | |
|
33 | 33 | __formated_value = None |
|
34 | 34 | |
|
35 | 35 | ELEMENTNAME = 'Parameter' |
|
36 | 36 | |
|
37 | 37 | def __init__(self): |
|
38 | 38 | |
|
39 | 39 | self.format = 'str' |
|
40 | 40 | |
|
41 | 41 | def getElementName(self): |
|
42 | 42 | |
|
43 | 43 | return self.ELEMENTNAME |
|
44 | 44 | |
|
45 | 45 | def getValue(self): |
|
46 | 46 | |
|
47 | 47 | value = self.value |
|
48 | 48 | format = self.format |
|
49 | 49 | |
|
50 | 50 | if self.__formated_value != None: |
|
51 | 51 | |
|
52 | 52 | return self.__formated_value |
|
53 | 53 | |
|
54 | 54 | if format == 'str': |
|
55 | 55 | self.__formated_value = str(value) |
|
56 | 56 | return self.__formated_value |
|
57 | 57 | |
|
58 | 58 | if value == '': |
|
59 | 59 | raise ValueError, "%s: This parameter value is empty" %self.name |
|
60 | 60 | |
|
61 | 61 | if format == 'list': |
|
62 | 62 | strList = value.split(',') |
|
63 | 63 | |
|
64 | 64 | self.__formated_value = strList |
|
65 | 65 | |
|
66 | 66 | return self.__formated_value |
|
67 | 67 | |
|
68 | 68 | if format == 'intlist': |
|
69 | 69 | """ |
|
70 | 70 | Example: |
|
71 | 71 | value = (0,1,2) |
|
72 | 72 | """ |
|
73 | 73 | |
|
74 | 74 | new_value = ast.literal_eval(value) |
|
75 | 75 | |
|
76 | 76 | if type(new_value) not in (tuple, list): |
|
77 | 77 | new_value = [int(new_value)] |
|
78 | 78 | |
|
79 | 79 | self.__formated_value = new_value |
|
80 | 80 | |
|
81 | 81 | return self.__formated_value |
|
82 | 82 | |
|
83 | 83 | if format == 'floatlist': |
|
84 | 84 | """ |
|
85 | 85 | Example: |
|
86 | 86 | value = (0.5, 1.4, 2.7) |
|
87 | 87 | """ |
|
88 | 88 | |
|
89 | 89 | new_value = ast.literal_eval(value) |
|
90 | 90 | |
|
91 | 91 | if type(new_value) not in (tuple, list): |
|
92 | 92 | new_value = [float(new_value)] |
|
93 | 93 | |
|
94 |
self.__formated_value = |
|
|
94 | self.__formated_value = new_value | |
|
95 | 95 | |
|
96 | 96 | return self.__formated_value |
|
97 | 97 | |
|
98 | 98 | if format == 'date': |
|
99 | 99 | strList = value.split('/') |
|
100 | 100 | intList = [int(x) for x in strList] |
|
101 | 101 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
102 | 102 | |
|
103 | 103 | self.__formated_value = date |
|
104 | 104 | |
|
105 | 105 | return self.__formated_value |
|
106 | 106 | |
|
107 | 107 | if format == 'time': |
|
108 | 108 | strList = value.split(':') |
|
109 | 109 | intList = [int(x) for x in strList] |
|
110 | 110 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
111 | 111 | |
|
112 | 112 | self.__formated_value = time |
|
113 | 113 | |
|
114 | 114 | return self.__formated_value |
|
115 | 115 | |
|
116 | 116 | if format == 'pairslist': |
|
117 | 117 | """ |
|
118 | 118 | Example: |
|
119 | 119 | value = (0,1),(1,2) |
|
120 | 120 | """ |
|
121 | 121 | |
|
122 | 122 | new_value = ast.literal_eval(value) |
|
123 | 123 | |
|
124 | 124 | if type(new_value) not in (tuple, list): |
|
125 | 125 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
126 | 126 | |
|
127 | 127 | if type(new_value[0]) not in (tuple, list): |
|
128 | 128 | if len(new_value) != 2: |
|
129 | 129 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
130 | 130 | new_value = [new_value] |
|
131 | 131 | |
|
132 | 132 | for thisPair in new_value: |
|
133 | 133 | if len(thisPair) != 2: |
|
134 | 134 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
135 | 135 | |
|
136 | 136 | self.__formated_value = new_value |
|
137 | 137 | |
|
138 | 138 | return self.__formated_value |
|
139 | 139 | |
|
140 | 140 | if format == 'multilist': |
|
141 | 141 | """ |
|
142 | 142 | Example: |
|
143 | 143 | value = (0,1,2),(3,4,5) |
|
144 | 144 | """ |
|
145 | 145 | multiList = ast.literal_eval(value) |
|
146 | 146 | |
|
147 | 147 | if type(multiList[0]) == int: |
|
148 | 148 | multiList = ast.literal_eval("(" + value + ")") |
|
149 | 149 | |
|
150 | 150 | self.__formated_value = multiList |
|
151 | 151 | |
|
152 | 152 | return self.__formated_value |
|
153 | 153 | |
|
154 | 154 | if format == 'bool': |
|
155 | 155 | value = int(value) |
|
156 | 156 | |
|
157 | 157 | if format == 'int': |
|
158 | 158 | value = float(value) |
|
159 | 159 | |
|
160 | 160 | format_func = eval(format) |
|
161 | 161 | |
|
162 | 162 | self.__formated_value = format_func(value) |
|
163 | 163 | |
|
164 | 164 | return self.__formated_value |
|
165 | 165 | |
|
166 | 166 | def updateId(self, new_id): |
|
167 | 167 | |
|
168 | 168 | self.id = str(new_id) |
|
169 | 169 | |
|
170 | 170 | def setup(self, id, name, value, format='str'): |
|
171 | 171 | |
|
172 | 172 | self.id = str(id) |
|
173 | 173 | self.name = name |
|
174 | 174 | self.value = str(value) |
|
175 | 175 | self.format = str.lower(format) |
|
176 | 176 | |
|
177 | 177 | self.getValue() |
|
178 | 178 | |
|
179 | 179 | return 1 |
|
180 | 180 | |
|
181 | 181 | def update(self, name, value, format='str'): |
|
182 | 182 | |
|
183 | 183 | self.name = name |
|
184 | 184 | self.value = str(value) |
|
185 | 185 | self.format = format |
|
186 | 186 | |
|
187 | 187 | def makeXml(self, opElement): |
|
188 | 188 | |
|
189 | 189 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
190 | 190 | parmElement.set('id', str(self.id)) |
|
191 | 191 | parmElement.set('name', self.name) |
|
192 | 192 | parmElement.set('value', self.value) |
|
193 | 193 | parmElement.set('format', self.format) |
|
194 | 194 | |
|
195 | 195 | def readXml(self, parmElement): |
|
196 | 196 | |
|
197 | 197 | self.id = parmElement.get('id') |
|
198 | 198 | self.name = parmElement.get('name') |
|
199 | 199 | self.value = parmElement.get('value') |
|
200 | 200 | self.format = str.lower(parmElement.get('format')) |
|
201 | 201 | |
|
202 | 202 | #Compatible with old signal chain version |
|
203 | 203 | if self.format == 'int' and self.name == 'idfigure': |
|
204 | 204 | self.name = 'id' |
|
205 | 205 | |
|
206 | 206 | def printattr(self): |
|
207 | 207 | |
|
208 | 208 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
209 | 209 | |
|
210 | 210 | class OperationConf(): |
|
211 | 211 | |
|
212 | 212 | id = None |
|
213 | 213 | name = None |
|
214 | 214 | priority = None |
|
215 | 215 | type = None |
|
216 | 216 | |
|
217 | 217 | parmConfObjList = [] |
|
218 | 218 | |
|
219 | 219 | ELEMENTNAME = 'Operation' |
|
220 | 220 | |
|
221 | 221 | def __init__(self): |
|
222 | 222 | |
|
223 | 223 | self.id = '0' |
|
224 | 224 | self.name = None |
|
225 | 225 | self.priority = None |
|
226 | 226 | self.type = 'self' |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | def __getNewId(self): |
|
230 | 230 | |
|
231 | 231 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
232 | 232 | |
|
233 | 233 | def updateId(self, new_id): |
|
234 | 234 | |
|
235 | 235 | self.id = str(new_id) |
|
236 | 236 | |
|
237 | 237 | n = 1 |
|
238 | 238 | for parmObj in self.parmConfObjList: |
|
239 | 239 | |
|
240 | 240 | idParm = str(int(new_id)*10 + n) |
|
241 | 241 | parmObj.updateId(idParm) |
|
242 | 242 | |
|
243 | 243 | n += 1 |
|
244 | 244 | |
|
245 | 245 | def getElementName(self): |
|
246 | 246 | |
|
247 | 247 | return self.ELEMENTNAME |
|
248 | 248 | |
|
249 | 249 | def getParameterObjList(self): |
|
250 | 250 | |
|
251 | 251 | return self.parmConfObjList |
|
252 | 252 | |
|
253 | 253 | def getParameterObj(self, parameterName): |
|
254 | 254 | |
|
255 | 255 | for parmConfObj in self.parmConfObjList: |
|
256 | 256 | |
|
257 | 257 | if parmConfObj.name != parameterName: |
|
258 | 258 | continue |
|
259 | 259 | |
|
260 | 260 | return parmConfObj |
|
261 | 261 | |
|
262 | 262 | return None |
|
263 | 263 | |
|
264 | 264 | def getParameterObjfromValue(self, parameterValue): |
|
265 | 265 | |
|
266 | 266 | for parmConfObj in self.parmConfObjList: |
|
267 | 267 | |
|
268 | 268 | if parmConfObj.getValue() != parameterValue: |
|
269 | 269 | continue |
|
270 | 270 | |
|
271 | 271 | return parmConfObj.getValue() |
|
272 | 272 | |
|
273 | 273 | return None |
|
274 | 274 | |
|
275 | 275 | def getParameterValue(self, parameterName): |
|
276 | 276 | |
|
277 | 277 | parameterObj = self.getParameterObj(parameterName) |
|
278 | 278 | |
|
279 | 279 | # if not parameterObj: |
|
280 | 280 | # return None |
|
281 | 281 | |
|
282 | 282 | value = parameterObj.getValue() |
|
283 | 283 | |
|
284 | 284 | return value |
|
285 | 285 | |
|
286 | 286 | def setup(self, id, name, priority, type): |
|
287 | 287 | |
|
288 | 288 | self.id = str(id) |
|
289 | 289 | self.name = name |
|
290 | 290 | self.type = type |
|
291 | 291 | self.priority = priority |
|
292 | 292 | |
|
293 | 293 | self.parmConfObjList = [] |
|
294 | 294 | |
|
295 | 295 | def removeParameters(self): |
|
296 | 296 | |
|
297 | 297 | for obj in self.parmConfObjList: |
|
298 | 298 | del obj |
|
299 | 299 | |
|
300 | 300 | self.parmConfObjList = [] |
|
301 | 301 | |
|
302 | 302 | def addParameter(self, name, value, format='str'): |
|
303 | 303 | |
|
304 | 304 | id = self.__getNewId() |
|
305 | 305 | |
|
306 | 306 | parmConfObj = ParameterConf() |
|
307 | 307 | if not parmConfObj.setup(id, name, value, format): |
|
308 | 308 | return None |
|
309 | 309 | |
|
310 | 310 | self.parmConfObjList.append(parmConfObj) |
|
311 | 311 | |
|
312 | 312 | return parmConfObj |
|
313 | 313 | |
|
314 | 314 | def changeParameter(self, name, value, format='str'): |
|
315 | 315 | |
|
316 | 316 | parmConfObj = self.getParameterObj(name) |
|
317 | 317 | parmConfObj.update(name, value, format) |
|
318 | 318 | |
|
319 | 319 | return parmConfObj |
|
320 | 320 | |
|
321 | 321 | def makeXml(self, procUnitElement): |
|
322 | 322 | |
|
323 | 323 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
324 | 324 | opElement.set('id', str(self.id)) |
|
325 | 325 | opElement.set('name', self.name) |
|
326 | 326 | opElement.set('type', self.type) |
|
327 | 327 | opElement.set('priority', str(self.priority)) |
|
328 | 328 | |
|
329 | 329 | for parmConfObj in self.parmConfObjList: |
|
330 | 330 | parmConfObj.makeXml(opElement) |
|
331 | 331 | |
|
332 | 332 | def readXml(self, opElement): |
|
333 | 333 | |
|
334 | 334 | self.id = opElement.get('id') |
|
335 | 335 | self.name = opElement.get('name') |
|
336 | 336 | self.type = opElement.get('type') |
|
337 | 337 | self.priority = opElement.get('priority') |
|
338 | 338 | |
|
339 | 339 | #Compatible with old signal chain version |
|
340 | 340 | #Use of 'run' method instead 'init' |
|
341 | 341 | if self.type == 'self' and self.name == 'init': |
|
342 | 342 | self.name = 'run' |
|
343 | 343 | |
|
344 | 344 | self.parmConfObjList = [] |
|
345 | 345 | |
|
346 | 346 | parmElementList = opElement.getiterator(ParameterConf().getElementName()) |
|
347 | 347 | |
|
348 | 348 | for parmElement in parmElementList: |
|
349 | 349 | parmConfObj = ParameterConf() |
|
350 | 350 | parmConfObj.readXml(parmElement) |
|
351 | 351 | |
|
352 | 352 | #Compatible with old signal chain version |
|
353 | 353 | #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
354 | 354 | if self.type != 'self' and self.name == 'Plot': |
|
355 | 355 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
356 | 356 | self.name = parmConfObj.value |
|
357 | 357 | continue |
|
358 | 358 | |
|
359 | 359 | self.parmConfObjList.append(parmConfObj) |
|
360 | 360 | |
|
361 | 361 | def printattr(self): |
|
362 | 362 | |
|
363 | 363 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
364 | 364 | self.id, |
|
365 | 365 | self.name, |
|
366 | 366 | self.type, |
|
367 | 367 | self.priority) |
|
368 | 368 | |
|
369 | 369 | for parmConfObj in self.parmConfObjList: |
|
370 | 370 | parmConfObj.printattr() |
|
371 | 371 | |
|
372 | 372 | def createObject(self, plotter_queue=None): |
|
373 | 373 | |
|
374 | 374 | if self.type == 'self': |
|
375 | 375 | raise ValueError, "This operation type cannot be created" |
|
376 | 376 | |
|
377 | 377 | if self.type == 'plotter': |
|
378 | 378 | #Plotter(plotter_name) |
|
379 | 379 | if not plotter_queue: |
|
380 | 380 | raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)" |
|
381 | 381 | |
|
382 | 382 | opObj = Plotter(self.name, plotter_queue) |
|
383 | 383 | |
|
384 | 384 | if self.type == 'external' or self.type == 'other': |
|
385 | 385 | className = eval(self.name) |
|
386 | 386 | opObj = className() |
|
387 | 387 | |
|
388 | 388 | return opObj |
|
389 | 389 | |
|
390 | 390 | class ProcUnitConf(): |
|
391 | 391 | |
|
392 | 392 | id = None |
|
393 | 393 | name = None |
|
394 | 394 | datatype = None |
|
395 | 395 | inputId = None |
|
396 | 396 | parentId = None |
|
397 | 397 | |
|
398 | 398 | opConfObjList = [] |
|
399 | 399 | |
|
400 | 400 | procUnitObj = None |
|
401 | 401 | opObjList = [] |
|
402 | 402 | |
|
403 | 403 | ELEMENTNAME = 'ProcUnit' |
|
404 | 404 | |
|
405 | 405 | def __init__(self): |
|
406 | 406 | |
|
407 | 407 | self.id = None |
|
408 | 408 | self.datatype = None |
|
409 | 409 | self.name = None |
|
410 | 410 | self.inputId = None |
|
411 | 411 | |
|
412 | 412 | self.opConfObjList = [] |
|
413 | 413 | |
|
414 | 414 | self.procUnitObj = None |
|
415 | 415 | self.opObjDict = {} |
|
416 | 416 | |
|
417 | 417 | def __getPriority(self): |
|
418 | 418 | |
|
419 | 419 | return len(self.opConfObjList)+1 |
|
420 | 420 | |
|
421 | 421 | def __getNewId(self): |
|
422 | 422 | |
|
423 | 423 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
424 | 424 | |
|
425 | 425 | def getElementName(self): |
|
426 | 426 | |
|
427 | 427 | return self.ELEMENTNAME |
|
428 | 428 | |
|
429 | 429 | def getId(self): |
|
430 | 430 | |
|
431 | 431 | return self.id |
|
432 | 432 | |
|
433 | 433 | def updateId(self, new_id, parentId=parentId): |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
437 | 437 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
438 | 438 | |
|
439 | 439 | #If this proc unit has not inputs |
|
440 | 440 | if self.inputId == '0': |
|
441 | 441 | new_inputId = 0 |
|
442 | 442 | |
|
443 | 443 | n = 1 |
|
444 | 444 | for opConfObj in self.opConfObjList: |
|
445 | 445 | |
|
446 | 446 | idOp = str(int(new_id)*10 + n) |
|
447 | 447 | opConfObj.updateId(idOp) |
|
448 | 448 | |
|
449 | 449 | n += 1 |
|
450 | 450 | |
|
451 | 451 | self.parentId = str(parentId) |
|
452 | 452 | self.id = str(new_id) |
|
453 | 453 | self.inputId = str(new_inputId) |
|
454 | 454 | |
|
455 | 455 | |
|
456 | 456 | def getInputId(self): |
|
457 | 457 | |
|
458 | 458 | return self.inputId |
|
459 | 459 | |
|
460 | 460 | def getOperationObjList(self): |
|
461 | 461 | |
|
462 | 462 | return self.opConfObjList |
|
463 | 463 | |
|
464 | 464 | def getOperationObj(self, name=None): |
|
465 | 465 | |
|
466 | 466 | for opConfObj in self.opConfObjList: |
|
467 | 467 | |
|
468 | 468 | if opConfObj.name != name: |
|
469 | 469 | continue |
|
470 | 470 | |
|
471 | 471 | return opConfObj |
|
472 | 472 | |
|
473 | 473 | return None |
|
474 | 474 | |
|
475 | 475 | def getOpObjfromParamValue(self, value=None): |
|
476 | 476 | |
|
477 | 477 | for opConfObj in self.opConfObjList: |
|
478 | 478 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
479 | 479 | continue |
|
480 | 480 | return opConfObj |
|
481 | 481 | return None |
|
482 | 482 | |
|
483 | 483 | def getProcUnitObj(self): |
|
484 | 484 | |
|
485 | 485 | return self.procUnitObj |
|
486 | 486 | |
|
487 | 487 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
488 | 488 | |
|
489 | 489 | #Compatible with old signal chain version |
|
490 | 490 | if datatype==None and name==None: |
|
491 | 491 | raise ValueError, "datatype or name should be defined" |
|
492 | 492 | |
|
493 | 493 | if name==None: |
|
494 | 494 | if 'Proc' in datatype: |
|
495 | 495 | name = datatype |
|
496 | 496 | else: |
|
497 | 497 | name = '%sProc' %(datatype) |
|
498 | 498 | |
|
499 | 499 | if datatype==None: |
|
500 | 500 | datatype = name.replace('Proc','') |
|
501 | 501 | |
|
502 | 502 | self.id = str(id) |
|
503 | 503 | self.name = name |
|
504 | 504 | self.datatype = datatype |
|
505 | 505 | self.inputId = inputId |
|
506 | 506 | self.parentId = parentId |
|
507 | 507 | |
|
508 | 508 | self.opConfObjList = [] |
|
509 | 509 | |
|
510 | 510 | self.addOperation(name='run', optype='self') |
|
511 | 511 | |
|
512 | 512 | def removeOperations(self): |
|
513 | 513 | |
|
514 | 514 | for obj in self.opConfObjList: |
|
515 | 515 | del obj |
|
516 | 516 | |
|
517 | 517 | self.opConfObjList = [] |
|
518 | 518 | self.addOperation(name='run') |
|
519 | 519 | |
|
520 | 520 | def addParameter(self, **kwargs): |
|
521 | 521 | ''' |
|
522 | 522 | Add parameters to "run" operation |
|
523 | 523 | ''' |
|
524 | 524 | opObj = self.opConfObjList[0] |
|
525 | 525 | |
|
526 | 526 | opObj.addParameter(**kwargs) |
|
527 | 527 | |
|
528 | 528 | return opObj |
|
529 | 529 | |
|
530 | 530 | def addOperation(self, name, optype='self'): |
|
531 | 531 | |
|
532 | 532 | id = self.__getNewId() |
|
533 | 533 | priority = self.__getPriority() |
|
534 | 534 | |
|
535 | 535 | opConfObj = OperationConf() |
|
536 | 536 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
537 | 537 | |
|
538 | 538 | self.opConfObjList.append(opConfObj) |
|
539 | 539 | |
|
540 | 540 | return opConfObj |
|
541 | 541 | |
|
542 | 542 | def makeXml(self, projectElement): |
|
543 | 543 | |
|
544 | 544 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
545 | 545 | procUnitElement.set('id', str(self.id)) |
|
546 | 546 | procUnitElement.set('name', self.name) |
|
547 | 547 | procUnitElement.set('datatype', self.datatype) |
|
548 | 548 | procUnitElement.set('inputId', str(self.inputId)) |
|
549 | 549 | |
|
550 | 550 | for opConfObj in self.opConfObjList: |
|
551 | 551 | opConfObj.makeXml(procUnitElement) |
|
552 | 552 | |
|
553 | 553 | def readXml(self, upElement): |
|
554 | 554 | |
|
555 | 555 | self.id = upElement.get('id') |
|
556 | 556 | self.name = upElement.get('name') |
|
557 | 557 | self.datatype = upElement.get('datatype') |
|
558 | 558 | self.inputId = upElement.get('inputId') |
|
559 | 559 | |
|
560 | 560 | if self.ELEMENTNAME == "ReadUnit": |
|
561 | 561 | self.datatype = self.datatype.replace("Reader", "") |
|
562 | 562 | |
|
563 | 563 | if self.ELEMENTNAME == "ProcUnit": |
|
564 | 564 | self.datatype = self.datatype.replace("Proc", "") |
|
565 | 565 | |
|
566 | 566 | if self.inputId == 'None': |
|
567 | 567 | self.inputId = '0' |
|
568 | 568 | |
|
569 | 569 | self.opConfObjList = [] |
|
570 | 570 | |
|
571 | 571 | opElementList = upElement.getiterator(OperationConf().getElementName()) |
|
572 | 572 | |
|
573 | 573 | for opElement in opElementList: |
|
574 | 574 | opConfObj = OperationConf() |
|
575 | 575 | opConfObj.readXml(opElement) |
|
576 | 576 | self.opConfObjList.append(opConfObj) |
|
577 | 577 | |
|
578 | 578 | def printattr(self): |
|
579 | 579 | |
|
580 | 580 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
581 | 581 | self.id, |
|
582 | 582 | self.name, |
|
583 | 583 | self.datatype, |
|
584 | 584 | self.inputId) |
|
585 | 585 | |
|
586 | 586 | for opConfObj in self.opConfObjList: |
|
587 | 587 | opConfObj.printattr() |
|
588 | 588 | |
|
589 | 589 | def createObjects(self, plotter_queue=None): |
|
590 | 590 | |
|
591 | 591 | className = eval(self.name) |
|
592 | 592 | procUnitObj = className() |
|
593 | 593 | |
|
594 | 594 | for opConfObj in self.opConfObjList: |
|
595 | 595 | |
|
596 | 596 | if opConfObj.type == 'self': |
|
597 | 597 | continue |
|
598 | 598 | |
|
599 | 599 | opObj = opConfObj.createObject(plotter_queue) |
|
600 | 600 | |
|
601 | 601 | self.opObjDict[opConfObj.id] = opObj |
|
602 | 602 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
603 | 603 | |
|
604 | 604 | self.procUnitObj = procUnitObj |
|
605 | 605 | |
|
606 | 606 | return procUnitObj |
|
607 | 607 | |
|
608 | 608 | def run(self): |
|
609 | 609 | |
|
610 | 610 | is_ok = False |
|
611 | 611 | |
|
612 | 612 | for opConfObj in self.opConfObjList: |
|
613 | 613 | |
|
614 | 614 | kwargs = {} |
|
615 | 615 | for parmConfObj in opConfObj.getParameterObjList(): |
|
616 | 616 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
617 | 617 | continue |
|
618 | 618 | |
|
619 | 619 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
620 | 620 | |
|
621 | 621 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
622 | 622 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
623 | 623 | opName = opConfObj.name, |
|
624 | 624 | opId = opConfObj.id, |
|
625 | 625 | **kwargs) |
|
626 | 626 | is_ok = is_ok or sts |
|
627 | 627 | |
|
628 | 628 | return is_ok |
|
629 | 629 | |
|
630 | 630 | def close(self): |
|
631 | 631 | |
|
632 | 632 | for opConfObj in self.opConfObjList: |
|
633 | 633 | if opConfObj.type == 'self': |
|
634 | 634 | continue |
|
635 | 635 | |
|
636 | 636 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
637 | 637 | opObj.close() |
|
638 | 638 | |
|
639 | 639 | self.procUnitObj.close() |
|
640 | 640 | |
|
641 | 641 | return |
|
642 | 642 | |
|
643 | 643 | class ReadUnitConf(ProcUnitConf): |
|
644 | 644 | |
|
645 | 645 | path = None |
|
646 | 646 | startDate = None |
|
647 | 647 | endDate = None |
|
648 | 648 | startTime = None |
|
649 | 649 | endTime = None |
|
650 | 650 | |
|
651 | 651 | ELEMENTNAME = 'ReadUnit' |
|
652 | 652 | |
|
653 | 653 | def __init__(self): |
|
654 | 654 | |
|
655 | 655 | self.id = None |
|
656 | 656 | self.datatype = None |
|
657 | 657 | self.name = None |
|
658 | 658 | self.inputId = None |
|
659 | 659 | |
|
660 | 660 | self.parentId = None |
|
661 | 661 | |
|
662 | 662 | self.opConfObjList = [] |
|
663 | 663 | self.opObjList = [] |
|
664 | 664 | |
|
665 | 665 | def getElementName(self): |
|
666 | 666 | |
|
667 | 667 | return self.ELEMENTNAME |
|
668 | 668 | |
|
669 | 669 | def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs): |
|
670 | 670 | |
|
671 | 671 | #Compatible with old signal chain version |
|
672 | 672 | if datatype==None and name==None: |
|
673 | 673 | raise ValueError, "datatype or name should be defined" |
|
674 | 674 | |
|
675 | 675 | if name==None: |
|
676 | 676 | if 'Reader' in datatype: |
|
677 | 677 | name = datatype |
|
678 | 678 | else: |
|
679 | 679 | name = '%sReader' %(datatype) |
|
680 | 680 | |
|
681 | 681 | if datatype==None: |
|
682 | 682 | datatype = name.replace('Reader','') |
|
683 | 683 | |
|
684 | 684 | self.id = id |
|
685 | 685 | self.name = name |
|
686 | 686 | self.datatype = datatype |
|
687 | 687 | |
|
688 | 688 | self.path = os.path.abspath(path) |
|
689 | 689 | self.startDate = startDate |
|
690 | 690 | self.endDate = endDate |
|
691 | 691 | self.startTime = startTime |
|
692 | 692 | self.endTime = endTime |
|
693 | 693 | |
|
694 | 694 | self.inputId = '0' |
|
695 | 695 | self.parentId = parentId |
|
696 | 696 | |
|
697 | 697 | self.addRunOperation(**kwargs) |
|
698 | 698 | |
|
699 | 699 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
700 | 700 | |
|
701 | 701 | #Compatible with old signal chain version |
|
702 | 702 | if datatype==None and name==None: |
|
703 | 703 | raise ValueError, "datatype or name should be defined" |
|
704 | 704 | |
|
705 | 705 | if name==None: |
|
706 | 706 | if 'Reader' in datatype: |
|
707 | 707 | name = datatype |
|
708 | 708 | else: |
|
709 | 709 | name = '%sReader' %(datatype) |
|
710 | 710 | |
|
711 | 711 | if datatype==None: |
|
712 | 712 | datatype = name.replace('Reader','') |
|
713 | 713 | |
|
714 | 714 | self.datatype = datatype |
|
715 | 715 | self.name = name |
|
716 | 716 | self.path = path |
|
717 | 717 | self.startDate = startDate |
|
718 | 718 | self.endDate = endDate |
|
719 | 719 | self.startTime = startTime |
|
720 | 720 | self.endTime = endTime |
|
721 | 721 | |
|
722 | 722 | self.inputId = '0' |
|
723 | 723 | self.parentId = parentId |
|
724 | 724 | |
|
725 | 725 | self.updateRunOperation(**kwargs) |
|
726 | 726 | |
|
727 | 727 | def removeOperations(self): |
|
728 | 728 | |
|
729 | 729 | for obj in self.opConfObjList: |
|
730 | 730 | del obj |
|
731 | 731 | |
|
732 | 732 | self.opConfObjList = [] |
|
733 | 733 | |
|
734 | 734 | def addRunOperation(self, **kwargs): |
|
735 | 735 | |
|
736 | 736 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
737 | 737 | |
|
738 | 738 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
739 | 739 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
740 | 740 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
741 | 741 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
742 | 742 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
743 | 743 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
744 | 744 | |
|
745 | 745 | for key, value in kwargs.items(): |
|
746 | 746 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
747 | 747 | |
|
748 | 748 | return opObj |
|
749 | 749 | |
|
750 | 750 | def updateRunOperation(self, **kwargs): |
|
751 | 751 | |
|
752 | 752 | opObj = self.getOperationObj(name = 'run') |
|
753 | 753 | opObj.removeParameters() |
|
754 | 754 | |
|
755 | 755 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
756 | 756 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
757 | 757 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
758 | 758 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
759 | 759 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
760 | 760 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
761 | 761 | |
|
762 | 762 | for key, value in kwargs.items(): |
|
763 | 763 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
764 | 764 | |
|
765 | 765 | return opObj |
|
766 | 766 | |
|
767 | 767 | # def makeXml(self, projectElement): |
|
768 | 768 | # |
|
769 | 769 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
770 | 770 | # procUnitElement.set('id', str(self.id)) |
|
771 | 771 | # procUnitElement.set('name', self.name) |
|
772 | 772 | # procUnitElement.set('datatype', self.datatype) |
|
773 | 773 | # procUnitElement.set('inputId', str(self.inputId)) |
|
774 | 774 | # |
|
775 | 775 | # for opConfObj in self.opConfObjList: |
|
776 | 776 | # opConfObj.makeXml(procUnitElement) |
|
777 | 777 | |
|
778 | 778 | def readXml(self, upElement): |
|
779 | 779 | |
|
780 | 780 | self.id = upElement.get('id') |
|
781 | 781 | self.name = upElement.get('name') |
|
782 | 782 | self.datatype = upElement.get('datatype') |
|
783 | 783 | self.inputId = upElement.get('inputId') |
|
784 | 784 | |
|
785 | 785 | if self.ELEMENTNAME == "ReadUnit": |
|
786 | 786 | self.datatype = self.datatype.replace("Reader", "") |
|
787 | 787 | |
|
788 | 788 | if self.inputId == 'None': |
|
789 | 789 | self.inputId = '0' |
|
790 | 790 | |
|
791 | 791 | self.opConfObjList = [] |
|
792 | 792 | |
|
793 | 793 | opElementList = upElement.getiterator(OperationConf().getElementName()) |
|
794 | 794 | |
|
795 | 795 | for opElement in opElementList: |
|
796 | 796 | opConfObj = OperationConf() |
|
797 | 797 | opConfObj.readXml(opElement) |
|
798 | 798 | self.opConfObjList.append(opConfObj) |
|
799 | 799 | |
|
800 | 800 | if opConfObj.name == 'run': |
|
801 | 801 | self.path = opConfObj.getParameterValue('path') |
|
802 | 802 | self.startDate = opConfObj.getParameterValue('startDate') |
|
803 | 803 | self.endDate = opConfObj.getParameterValue('endDate') |
|
804 | 804 | self.startTime = opConfObj.getParameterValue('startTime') |
|
805 | 805 | self.endTime = opConfObj.getParameterValue('endTime') |
|
806 | 806 | |
|
807 | 807 | class Project(): |
|
808 | 808 | |
|
809 | 809 | id = None |
|
810 | 810 | name = None |
|
811 | 811 | description = None |
|
812 | 812 | filename = None |
|
813 | 813 | |
|
814 | 814 | procUnitConfObjDict = None |
|
815 | 815 | |
|
816 | 816 | ELEMENTNAME = 'Project' |
|
817 | 817 | |
|
818 | 818 | plotterQueue = None |
|
819 | 819 | |
|
820 | 820 | def __init__(self, plotter_queue=None): |
|
821 | 821 | |
|
822 | 822 | self.id = None |
|
823 | 823 | self.name = None |
|
824 | 824 | self.description = None |
|
825 | 825 | |
|
826 | 826 | self.plotterQueue = plotter_queue |
|
827 | 827 | |
|
828 | 828 | self.procUnitConfObjDict = {} |
|
829 | 829 | |
|
830 | 830 | def __getNewId(self): |
|
831 | 831 | |
|
832 | 832 | id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1 |
|
833 | 833 | |
|
834 | 834 | return str(id) |
|
835 | 835 | |
|
836 | 836 | def getElementName(self): |
|
837 | 837 | |
|
838 | 838 | return self.ELEMENTNAME |
|
839 | 839 | |
|
840 | 840 | def getId(self): |
|
841 | 841 | |
|
842 | 842 | return self.id |
|
843 | 843 | |
|
844 | 844 | def updateId(self, new_id): |
|
845 | 845 | |
|
846 | 846 | self.id = str(new_id) |
|
847 | 847 | |
|
848 | 848 | keyList = self.procUnitConfObjDict.keys() |
|
849 | 849 | keyList.sort() |
|
850 | 850 | |
|
851 | 851 | n = 1 |
|
852 | 852 | newProcUnitConfObjDict = {} |
|
853 | 853 | |
|
854 | 854 | for procKey in keyList: |
|
855 | 855 | |
|
856 | 856 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
857 | 857 | idProcUnit = str(int(self.id)*10 + n) |
|
858 | 858 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
859 | 859 | |
|
860 | 860 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
861 | 861 | n += 1 |
|
862 | 862 | |
|
863 | 863 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
864 | 864 | |
|
865 | 865 | def setup(self, id, name, description): |
|
866 | 866 | |
|
867 | 867 | self.id = str(id) |
|
868 | 868 | self.name = name |
|
869 | 869 | self.description = description |
|
870 | 870 | |
|
871 | 871 | def update(self, name, description): |
|
872 | 872 | |
|
873 | 873 | self.name = name |
|
874 | 874 | self.description = description |
|
875 | 875 | |
|
876 | 876 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
877 | 877 | |
|
878 | 878 | if id is None: |
|
879 | 879 | idReadUnit = self.__getNewId() |
|
880 | 880 | else: |
|
881 | 881 | idReadUnit = str(id) |
|
882 | 882 | |
|
883 | 883 | readUnitConfObj = ReadUnitConf() |
|
884 | 884 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
885 | 885 | |
|
886 | 886 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
887 | 887 | |
|
888 | 888 | return readUnitConfObj |
|
889 | 889 | |
|
890 | 890 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
891 | 891 | |
|
892 | 892 | idProcUnit = self.__getNewId() |
|
893 | 893 | |
|
894 | 894 | procUnitConfObj = ProcUnitConf() |
|
895 | 895 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
896 | 896 | |
|
897 | 897 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
898 | 898 | |
|
899 | 899 | return procUnitConfObj |
|
900 | 900 | |
|
901 | 901 | def removeProcUnit(self, id): |
|
902 | 902 | |
|
903 | 903 | if id in self.procUnitConfObjDict.keys(): |
|
904 | 904 | self.procUnitConfObjDict.pop(id) |
|
905 | 905 | |
|
906 | 906 | def getReadUnitId(self): |
|
907 | 907 | |
|
908 | 908 | readUnitConfObj = self.getReadUnitObj() |
|
909 | 909 | |
|
910 | 910 | return readUnitConfObj.id |
|
911 | 911 | |
|
912 | 912 | def getReadUnitObj(self): |
|
913 | 913 | |
|
914 | 914 | for obj in self.procUnitConfObjDict.values(): |
|
915 | 915 | if obj.getElementName() == "ReadUnit": |
|
916 | 916 | return obj |
|
917 | 917 | |
|
918 | 918 | return None |
|
919 | 919 | |
|
920 | 920 | def getProcUnitObj(self, id=None, name=None): |
|
921 | 921 | |
|
922 | 922 | if id != None: |
|
923 | 923 | return self.procUnitConfObjDict[id] |
|
924 | 924 | |
|
925 | 925 | if name != None: |
|
926 | 926 | return self.getProcUnitObjByName(name) |
|
927 | 927 | |
|
928 | 928 | return None |
|
929 | 929 | |
|
930 | 930 | def getProcUnitObjByName(self, name): |
|
931 | 931 | |
|
932 | 932 | for obj in self.procUnitConfObjDict.values(): |
|
933 | 933 | if obj.name == name: |
|
934 | 934 | return obj |
|
935 | 935 | |
|
936 | 936 | return None |
|
937 | 937 | |
|
938 | 938 | def procUnitItems(self): |
|
939 | 939 | |
|
940 | 940 | return self.procUnitConfObjDict.items() |
|
941 | 941 | |
|
942 | 942 | def makeXml(self): |
|
943 | 943 | |
|
944 | 944 | projectElement = Element('Project') |
|
945 | 945 | projectElement.set('id', str(self.id)) |
|
946 | 946 | projectElement.set('name', self.name) |
|
947 | 947 | projectElement.set('description', self.description) |
|
948 | 948 | |
|
949 | 949 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
950 | 950 | procUnitConfObj.makeXml(projectElement) |
|
951 | 951 | |
|
952 | 952 | self.projectElement = projectElement |
|
953 | 953 | |
|
954 | 954 | def writeXml(self, filename=None): |
|
955 | 955 | |
|
956 | 956 | if filename == None: |
|
957 | 957 | if self.filename: |
|
958 | 958 | filename = self.filename |
|
959 | 959 | else: |
|
960 | 960 | filename = "schain.xml" |
|
961 | 961 | |
|
962 | 962 | if not filename: |
|
963 | 963 | print "filename has not been defined. Use setFilename(filename) for do it." |
|
964 | 964 | return 0 |
|
965 | 965 | |
|
966 | 966 | abs_file = os.path.abspath(filename) |
|
967 | 967 | |
|
968 | 968 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
969 | 969 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
970 | 970 | return 0 |
|
971 | 971 | |
|
972 | 972 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
973 | 973 | print "File %s already exists and it could not be overwriten" %abs_file |
|
974 | 974 | return 0 |
|
975 | 975 | |
|
976 | 976 | self.makeXml() |
|
977 | 977 | |
|
978 | 978 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
979 | 979 | |
|
980 | 980 | self.filename = abs_file |
|
981 | 981 | |
|
982 | 982 | return 1 |
|
983 | 983 | |
|
984 | 984 | def readXml(self, filename = None): |
|
985 | 985 | |
|
986 | 986 | abs_file = os.path.abspath(filename) |
|
987 | 987 | |
|
988 | 988 | if not os.path.isfile(abs_file): |
|
989 | 989 | print "%s does not exist" %abs_file |
|
990 | 990 | return 0 |
|
991 | 991 | |
|
992 | 992 | self.projectElement = None |
|
993 | 993 | self.procUnitConfObjDict = {} |
|
994 | 994 | |
|
995 | 995 | try: |
|
996 | 996 | self.projectElement = ElementTree().parse(abs_file) |
|
997 | 997 | except: |
|
998 | 998 | print "Error reading %s, verify file format" %filename |
|
999 | 999 | return 0 |
|
1000 | 1000 | |
|
1001 | 1001 | self.project = self.projectElement.tag |
|
1002 | 1002 | |
|
1003 | 1003 | self.id = self.projectElement.get('id') |
|
1004 | 1004 | self.name = self.projectElement.get('name') |
|
1005 | 1005 | self.description = self.projectElement.get('description') |
|
1006 | 1006 | |
|
1007 | 1007 | readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName()) |
|
1008 | 1008 | |
|
1009 | 1009 | for readUnitElement in readUnitElementList: |
|
1010 | 1010 | readUnitConfObj = ReadUnitConf() |
|
1011 | 1011 | readUnitConfObj.readXml(readUnitElement) |
|
1012 | 1012 | |
|
1013 | 1013 | if readUnitConfObj.parentId == None: |
|
1014 | 1014 | readUnitConfObj.parentId = self.id |
|
1015 | 1015 | |
|
1016 | 1016 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1017 | 1017 | |
|
1018 | 1018 | procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName()) |
|
1019 | 1019 | |
|
1020 | 1020 | for procUnitElement in procUnitElementList: |
|
1021 | 1021 | procUnitConfObj = ProcUnitConf() |
|
1022 | 1022 | procUnitConfObj.readXml(procUnitElement) |
|
1023 | 1023 | |
|
1024 | 1024 | if procUnitConfObj.parentId == None: |
|
1025 | 1025 | procUnitConfObj.parentId = self.id |
|
1026 | 1026 | |
|
1027 | 1027 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1028 | 1028 | |
|
1029 | 1029 | self.filename = abs_file |
|
1030 | 1030 | |
|
1031 | 1031 | return 1 |
|
1032 | 1032 | |
|
1033 | 1033 | def printattr(self): |
|
1034 | 1034 | |
|
1035 | 1035 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1036 | 1036 | self.name, |
|
1037 | 1037 | self.description) |
|
1038 | 1038 | |
|
1039 | 1039 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1040 | 1040 | procUnitConfObj.printattr() |
|
1041 | 1041 | |
|
1042 | 1042 | def createObjects(self): |
|
1043 | 1043 | |
|
1044 | 1044 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1045 | 1045 | procUnitConfObj.createObjects(self.plotterQueue) |
|
1046 | 1046 | |
|
1047 | 1047 | def __connect(self, objIN, thisObj): |
|
1048 | 1048 | |
|
1049 | 1049 | thisObj.setInput(objIN.getOutputObj()) |
|
1050 | 1050 | |
|
1051 | 1051 | def connectObjects(self): |
|
1052 | 1052 | |
|
1053 | 1053 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1054 | 1054 | |
|
1055 | 1055 | inputId = thisPUConfObj.getInputId() |
|
1056 | 1056 | |
|
1057 | 1057 | if int(inputId) == 0: |
|
1058 | 1058 | continue |
|
1059 | 1059 | |
|
1060 | 1060 | #Get input object |
|
1061 | 1061 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1062 | 1062 | puObjIN = puConfINObj.getProcUnitObj() |
|
1063 | 1063 | |
|
1064 | 1064 | #Get current object |
|
1065 | 1065 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1066 | 1066 | |
|
1067 | 1067 | self.__connect(puObjIN, thisPUObj) |
|
1068 | 1068 | |
|
1069 | 1069 | def __handleError(self, procUnitConfObj, send_email=True): |
|
1070 | 1070 | |
|
1071 | 1071 | import socket |
|
1072 | 1072 | |
|
1073 | 1073 | err = traceback.format_exception(sys.exc_info()[0], |
|
1074 | 1074 | sys.exc_info()[1], |
|
1075 | 1075 | sys.exc_info()[2]) |
|
1076 | 1076 | |
|
1077 | 1077 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1078 | 1078 | print "***** %s" %err[-1] |
|
1079 | 1079 | |
|
1080 | 1080 | message = "".join(err) |
|
1081 | 1081 | |
|
1082 | 1082 | sys.stderr.write(message) |
|
1083 | 1083 | |
|
1084 | 1084 | if not send_email: |
|
1085 | 1085 | return |
|
1086 | 1086 | |
|
1087 | 1087 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1088 | 1088 | |
|
1089 | 1089 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1090 | 1090 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1091 | 1091 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1092 | 1092 | subtitle += "Configuration file: %s\n" %self.filename |
|
1093 | 1093 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1094 | 1094 | |
|
1095 | 1095 | readUnitConfObj = self.getReadUnitObj() |
|
1096 | 1096 | if readUnitConfObj: |
|
1097 | 1097 | subtitle += "\nInput parameters:\n" |
|
1098 | 1098 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1099 | 1099 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1100 | 1100 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1101 | 1101 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1102 | 1102 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1103 | 1103 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1104 | 1104 | |
|
1105 | 1105 | adminObj = schainpy.admin.SchainNotify() |
|
1106 | 1106 | adminObj.sendAlert(message=message, |
|
1107 | 1107 | subject=subject, |
|
1108 | 1108 | subtitle=subtitle, |
|
1109 | 1109 | filename=self.filename) |
|
1110 | 1110 | |
|
1111 | 1111 | def isPaused(self): |
|
1112 | 1112 | return 0 |
|
1113 | 1113 | |
|
1114 | 1114 | def isStopped(self): |
|
1115 | 1115 | return 0 |
|
1116 | 1116 | |
|
1117 | 1117 | def runController(self): |
|
1118 | 1118 | """ |
|
1119 | 1119 | returns 0 when this process has been stopped, 1 otherwise |
|
1120 | 1120 | """ |
|
1121 | 1121 | |
|
1122 | 1122 | if self.isPaused(): |
|
1123 | 1123 | print "Process suspended" |
|
1124 | 1124 | |
|
1125 | 1125 | while True: |
|
1126 | 1126 | sleep(0.1) |
|
1127 | 1127 | |
|
1128 | 1128 | if not self.isPaused(): |
|
1129 | 1129 | break |
|
1130 | 1130 | |
|
1131 | 1131 | if self.isStopped(): |
|
1132 | 1132 | break |
|
1133 | 1133 | |
|
1134 | 1134 | print "Process reinitialized" |
|
1135 | 1135 | |
|
1136 | 1136 | if self.isStopped(): |
|
1137 | 1137 | print "Process stopped" |
|
1138 | 1138 | return 0 |
|
1139 | 1139 | |
|
1140 | 1140 | return 1 |
|
1141 | 1141 | |
|
1142 | 1142 | def setFilename(self, filename): |
|
1143 | 1143 | |
|
1144 | 1144 | self.filename = filename |
|
1145 | 1145 | |
|
1146 | 1146 | def setPlotterQueue(self, plotter_queue): |
|
1147 | 1147 | |
|
1148 | 1148 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1149 | 1149 | |
|
1150 | 1150 | def getPlotterQueue(self): |
|
1151 | 1151 | |
|
1152 | 1152 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1153 | 1153 | |
|
1154 | 1154 | def useExternalPlotter(self): |
|
1155 | 1155 | |
|
1156 | 1156 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1157 | 1157 | |
|
1158 | 1158 | def run(self): |
|
1159 | 1159 | |
|
1160 | 1160 | |
|
1161 | 1161 | print "*"*60 |
|
1162 | 1162 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1163 | 1163 | print "*"*60 |
|
1164 | 1164 | |
|
1165 | 1165 | |
|
1166 | 1166 | keyList = self.procUnitConfObjDict.keys() |
|
1167 | 1167 | keyList.sort() |
|
1168 | 1168 | |
|
1169 | 1169 | while(True): |
|
1170 | 1170 | |
|
1171 | 1171 | is_ok = False |
|
1172 | 1172 | |
|
1173 | 1173 | for procKey in keyList: |
|
1174 | 1174 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1175 | 1175 | |
|
1176 | 1176 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1177 | 1177 | |
|
1178 | 1178 | try: |
|
1179 | 1179 | sts = procUnitConfObj.run() |
|
1180 | 1180 | is_ok = is_ok or sts |
|
1181 | 1181 | except KeyboardInterrupt: |
|
1182 | 1182 | is_ok = False |
|
1183 | 1183 | break |
|
1184 | 1184 | except ValueError, e: |
|
1185 | 1185 | sleep(0.5) |
|
1186 |
self.__handleError(procUnitConfObj, send_email= |
|
|
1186 | self.__handleError(procUnitConfObj, send_email=True) | |
|
1187 | 1187 | is_ok = False |
|
1188 | 1188 | break |
|
1189 | 1189 | except: |
|
1190 | 1190 | sleep(0.5) |
|
1191 | 1191 | self.__handleError(procUnitConfObj) |
|
1192 | 1192 | is_ok = False |
|
1193 | 1193 | break |
|
1194 | 1194 | |
|
1195 | 1195 | #If every process unit finished so end process |
|
1196 | 1196 | if not(is_ok): |
|
1197 | 1197 | # print "Every process unit have finished" |
|
1198 | 1198 | break |
|
1199 | 1199 | |
|
1200 | 1200 | if not self.runController(): |
|
1201 | 1201 | break |
|
1202 | 1202 | |
|
1203 | 1203 | #Closing every process |
|
1204 | 1204 | for procKey in keyList: |
|
1205 | 1205 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1206 | 1206 | procUnitConfObj.close() |
|
1207 | 1207 | |
|
1208 | 1208 | print "Process finished" |
|
1209 | 1209 | |
|
1210 | 1210 | def start(self): |
|
1211 | 1211 | |
|
1212 | 1212 | self.writeXml() |
|
1213 | 1213 | |
|
1214 | 1214 | self.createObjects() |
|
1215 | 1215 | self.connectObjects() |
|
1216 | 1216 | self.run() |
|
1217 | 1217 | |
|
1218 | 1218 | if __name__ == '__main__': |
|
1219 | 1219 | |
|
1220 | 1220 | desc = "Segundo Test" |
|
1221 | 1221 | filename = "schain.xml" |
|
1222 | 1222 | |
|
1223 | 1223 | controllerObj = Project() |
|
1224 | 1224 | |
|
1225 | 1225 | controllerObj.setup(id = '191', name='test01', description=desc) |
|
1226 | 1226 | |
|
1227 | 1227 | readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage', |
|
1228 | 1228 | path='data/rawdata/', |
|
1229 | 1229 | startDate='2011/01/01', |
|
1230 | 1230 | endDate='2012/12/31', |
|
1231 | 1231 | startTime='00:00:00', |
|
1232 | 1232 | endTime='23:59:59', |
|
1233 | 1233 | online=1, |
|
1234 | 1234 | walk=1) |
|
1235 | 1235 | |
|
1236 | 1236 | procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId()) |
|
1237 | 1237 | |
|
1238 | 1238 | opObj10 = procUnitConfObj0.addOperation(name='selectChannels') |
|
1239 | 1239 | opObj10.addParameter(name='channelList', value='3,4,5', format='intlist') |
|
1240 | 1240 | |
|
1241 | 1241 | opObj10 = procUnitConfObj0.addOperation(name='selectHeights') |
|
1242 | 1242 | opObj10.addParameter(name='minHei', value='90', format='float') |
|
1243 | 1243 | opObj10.addParameter(name='maxHei', value='180', format='float') |
|
1244 | 1244 | |
|
1245 | 1245 | opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external') |
|
1246 | 1246 | opObj12.addParameter(name='n', value='10', format='int') |
|
1247 | 1247 | |
|
1248 | 1248 | procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId()) |
|
1249 | 1249 | procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int') |
|
1250 | 1250 | # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='') |
|
1251 | 1251 | |
|
1252 | 1252 | |
|
1253 | 1253 | opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') |
|
1254 | 1254 | opObj11.addParameter(name='idfigure', value='1', format='int') |
|
1255 | 1255 | opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str') |
|
1256 | 1256 | opObj11.addParameter(name='zmin', value='40', format='int') |
|
1257 | 1257 | opObj11.addParameter(name='zmax', value='90', format='int') |
|
1258 | 1258 | opObj11.addParameter(name='showprofile', value='1', format='int') |
|
1259 | 1259 | |
|
1260 | 1260 | print "Escribiendo el archivo XML" |
|
1261 | 1261 | |
|
1262 | 1262 | controllerObj.writeXml(filename) |
|
1263 | 1263 | |
|
1264 | 1264 | print "Leyendo el archivo XML" |
|
1265 | 1265 | controllerObj.readXml(filename) |
|
1266 | 1266 | #controllerObj.printattr() |
|
1267 | 1267 | |
|
1268 | 1268 | controllerObj.createObjects() |
|
1269 | 1269 | controllerObj.connectObjects() |
|
1270 | 1270 | controllerObj.run() |
|
1271 | 1271 | |
|
1272 | 1272 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now