##// END OF EJS Templates
External plotter simplified.
Miguel Valdez -
r716:4b81c4b32e79
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

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