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