@@ -0,0 +1,68 | |||
|
1 | ''' | |
|
2 | Created on Jul 9, 2014 | |
|
3 | ||
|
4 | @author: roj-idl71 | |
|
5 | ''' | |
|
6 | import os | |
|
7 | import datetime | |
|
8 | import numpy | |
|
9 | ||
|
10 | from figure import Figure | |
|
11 | ||
|
12 | class Plotter(Figure): | |
|
13 | ||
|
14 | isConfig = None | |
|
15 | name = None | |
|
16 | plotterQueue = None | |
|
17 | ||
|
18 | def __init__(self, plotter_name, plotter_queue=None): | |
|
19 | ||
|
20 | self.isConfig = False | |
|
21 | self.name = plotter_name | |
|
22 | self.plotterQueue = plotter_queue | |
|
23 | ||
|
24 | def getSubplots(self): | |
|
25 | ||
|
26 | nrow = self.nplots | |
|
27 | ncol = 1 | |
|
28 | return nrow, ncol | |
|
29 | ||
|
30 | def setup(self, **kwargs): | |
|
31 | ||
|
32 | # self.nplots = nplots | |
|
33 | # | |
|
34 | # self.createFigure(id=id, | |
|
35 | # wintitle=wintitle, | |
|
36 | # show=show) | |
|
37 | # | |
|
38 | # nrow,ncol = self.getSubplots() | |
|
39 | # colspan = 3 | |
|
40 | # rowspan = 1 | |
|
41 | # | |
|
42 | # for i in range(nplots): | |
|
43 | # self.addAxes(nrow, ncol, i, 0, colspan, rowspan) | |
|
44 | ||
|
45 | ||
|
46 | ||
|
47 | print "Initializing ..." | |
|
48 | ||
|
49 | ||
|
50 | def run(self, dataOut, **kwargs): | |
|
51 | ||
|
52 | """ | |
|
53 | ||
|
54 | Input: | |
|
55 | dataOut : | |
|
56 | id : | |
|
57 | wintitle : | |
|
58 | channelList : | |
|
59 | show : | |
|
60 | """ | |
|
61 | ||
|
62 | if not self.isConfig: | |
|
63 | self.setup(**kwargs) | |
|
64 | self.isConfig=True | |
|
65 | ||
|
66 | print "Putting data on %s queue:" %self.name | |
|
67 | print kwargs | |
|
68 |
@@ -1,7 +1,7 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Feb 7, 2012 |
|
3 | 3 | |
|
4 | 4 | @author $Author$ |
|
5 | 5 | @version $Id$ |
|
6 | 6 | ''' |
|
7 |
__version__ = "2.1.4. |
|
|
7 | __version__ = "2.1.4.2" No newline at end of file |
@@ -1,1234 +1,1252 | |||
|
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 | def createObject(self): | |
|
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 | if self.type == 'plotter': | |
|
386 | #Plotter(plotter_name) | |
|
387 | opObj = Plotter(self.name, plotter_queue) | |
|
388 | ||
|
385 | 389 | if self.type == 'external' or self.type == 'other': |
|
386 | 390 | className = eval(self.name) |
|
387 | 391 | opObj = className() |
|
388 | 392 | |
|
389 | 393 | return opObj |
|
390 | 394 | |
|
391 | 395 | class ProcUnitConf(): |
|
392 | 396 | |
|
393 | 397 | id = None |
|
394 | 398 | name = None |
|
395 | 399 | datatype = None |
|
396 | 400 | inputId = None |
|
397 | 401 | parentId = None |
|
398 | 402 | |
|
399 | 403 | opConfObjList = [] |
|
400 | 404 | |
|
401 | 405 | procUnitObj = None |
|
402 | 406 | opObjList = [] |
|
403 | 407 | |
|
404 | 408 | ELEMENTNAME = 'ProcUnit' |
|
405 | 409 | |
|
406 | 410 | def __init__(self): |
|
407 | 411 | |
|
408 | 412 | self.id = None |
|
409 | 413 | self.datatype = None |
|
410 | 414 | self.name = None |
|
411 | 415 | self.inputId = None |
|
412 | 416 | |
|
413 | 417 | self.opConfObjList = [] |
|
414 | 418 | |
|
415 | 419 | self.procUnitObj = None |
|
416 | 420 | self.opObjDict = {} |
|
417 | 421 | |
|
418 | 422 | def __getPriority(self): |
|
419 | 423 | |
|
420 | 424 | return len(self.opConfObjList)+1 |
|
421 | 425 | |
|
422 | 426 | def __getNewId(self): |
|
423 | 427 | |
|
424 | 428 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
425 | 429 | |
|
426 | 430 | def getElementName(self): |
|
427 | 431 | |
|
428 | 432 | return self.ELEMENTNAME |
|
429 | 433 | |
|
430 | 434 | def getId(self): |
|
431 | 435 | |
|
432 | 436 | return self.id |
|
433 | 437 | |
|
434 | 438 | def updateId(self, new_id, parentId=parentId): |
|
435 | 439 | |
|
436 | 440 | |
|
437 | 441 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
438 | 442 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
439 | 443 | |
|
440 | 444 | #If this proc unit has not inputs |
|
441 | 445 | if self.inputId == '0': |
|
442 | 446 | new_inputId = 0 |
|
443 | 447 | |
|
444 | 448 | n = 1 |
|
445 | 449 | for opConfObj in self.opConfObjList: |
|
446 | 450 | |
|
447 | 451 | idOp = str(int(new_id)*10 + n) |
|
448 | 452 | opConfObj.updateId(idOp) |
|
449 | 453 | |
|
450 | 454 | n += 1 |
|
451 | 455 | |
|
452 | 456 | self.parentId = str(parentId) |
|
453 | 457 | self.id = str(new_id) |
|
454 | 458 | self.inputId = str(new_inputId) |
|
455 | 459 | |
|
456 | 460 | |
|
457 | 461 | def getInputId(self): |
|
458 | 462 | |
|
459 | 463 | return self.inputId |
|
460 | 464 | |
|
461 | 465 | def getOperationObjList(self): |
|
462 | 466 | |
|
463 | 467 | return self.opConfObjList |
|
464 | 468 | |
|
465 | 469 | def getOperationObj(self, name=None): |
|
466 | 470 | |
|
467 | 471 | for opConfObj in self.opConfObjList: |
|
468 | 472 | |
|
469 | 473 | if opConfObj.name != name: |
|
470 | 474 | continue |
|
471 | 475 | |
|
472 | 476 | return opConfObj |
|
473 | 477 | |
|
474 | 478 | return None |
|
475 | 479 | |
|
476 | 480 | def getOpObjfromParamValue(self, value=None): |
|
477 | 481 | |
|
478 | 482 | for opConfObj in self.opConfObjList: |
|
479 | 483 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
480 | 484 | continue |
|
481 | 485 | return opConfObj |
|
482 | 486 | return None |
|
483 | 487 | |
|
484 | 488 | def getProcUnitObj(self): |
|
485 | 489 | |
|
486 | 490 | return self.procUnitObj |
|
487 | 491 | |
|
488 | 492 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
489 | 493 | |
|
490 | 494 | #Compatible with old signal chain version |
|
491 | 495 | if datatype==None and name==None: |
|
492 | 496 | raise ValueError, "datatype or name should be defined" |
|
493 | 497 | |
|
494 | 498 | if name==None: |
|
495 | 499 | if 'Proc' in datatype: |
|
496 | 500 | name = datatype |
|
497 | 501 | else: |
|
498 | 502 | name = '%sProc' %(datatype) |
|
499 | 503 | |
|
500 | 504 | if datatype==None: |
|
501 | 505 | datatype = name.replace('Proc','') |
|
502 | 506 | |
|
503 | 507 | self.id = str(id) |
|
504 | 508 | self.name = name |
|
505 | 509 | self.datatype = datatype |
|
506 | 510 | self.inputId = inputId |
|
507 | 511 | self.parentId = parentId |
|
508 | 512 | |
|
509 | 513 | self.opConfObjList = [] |
|
510 | 514 | |
|
511 | 515 | self.addOperation(name='run', optype='self') |
|
512 | 516 | |
|
513 | 517 | def removeOperations(self): |
|
514 | 518 | |
|
515 | 519 | for obj in self.opConfObjList: |
|
516 | 520 | del obj |
|
517 | 521 | |
|
518 | 522 | self.opConfObjList = [] |
|
519 | 523 | self.addOperation(name='run') |
|
520 | 524 | |
|
521 | 525 | def addParameter(self, **kwargs): |
|
522 | 526 | ''' |
|
523 | 527 | Add parameters to "run" operation |
|
524 | 528 | ''' |
|
525 | 529 | opObj = self.opConfObjList[0] |
|
526 | 530 | |
|
527 | 531 | opObj.addParameter(**kwargs) |
|
528 | 532 | |
|
529 | 533 | return opObj |
|
530 | 534 | |
|
531 | 535 | def addOperation(self, name, optype='self'): |
|
532 | 536 | |
|
533 | 537 | id = self.__getNewId() |
|
534 | 538 | priority = self.__getPriority() |
|
535 | 539 | |
|
536 | 540 | opConfObj = OperationConf() |
|
537 | 541 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
538 | 542 | |
|
539 | 543 | self.opConfObjList.append(opConfObj) |
|
540 | 544 | |
|
541 | 545 | return opConfObj |
|
542 | 546 | |
|
543 | 547 | def makeXml(self, projectElement): |
|
544 | 548 | |
|
545 | 549 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
546 | 550 | procUnitElement.set('id', str(self.id)) |
|
547 | 551 | procUnitElement.set('name', self.name) |
|
548 | 552 | procUnitElement.set('datatype', self.datatype) |
|
549 | 553 | procUnitElement.set('inputId', str(self.inputId)) |
|
550 | 554 | |
|
551 | 555 | for opConfObj in self.opConfObjList: |
|
552 | 556 | opConfObj.makeXml(procUnitElement) |
|
553 | 557 | |
|
554 | 558 | def readXml(self, upElement): |
|
555 | 559 | |
|
556 | 560 | self.id = upElement.get('id') |
|
557 | 561 | self.name = upElement.get('name') |
|
558 | 562 | self.datatype = upElement.get('datatype') |
|
559 | 563 | self.inputId = upElement.get('inputId') |
|
560 | 564 | |
|
561 | 565 | if self.ELEMENTNAME == "ReadUnit": |
|
562 | 566 | self.datatype = self.datatype.replace("Reader", "") |
|
563 | 567 | |
|
564 | 568 | if self.ELEMENTNAME == "ProcUnit": |
|
565 | 569 | self.datatype = self.datatype.replace("Proc", "") |
|
566 | 570 | |
|
567 | 571 | if self.inputId == 'None': |
|
568 | 572 | self.inputId = '0' |
|
569 | 573 | |
|
570 | 574 | self.opConfObjList = [] |
|
571 | 575 | |
|
572 | 576 | opElementList = upElement.getiterator(OperationConf().getElementName()) |
|
573 | 577 | |
|
574 | 578 | for opElement in opElementList: |
|
575 | 579 | opConfObj = OperationConf() |
|
576 | 580 | opConfObj.readXml(opElement) |
|
577 | 581 | self.opConfObjList.append(opConfObj) |
|
578 | 582 | |
|
579 | 583 | def printattr(self): |
|
580 | 584 | |
|
581 | 585 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
582 | 586 | self.id, |
|
583 | 587 | self.name, |
|
584 | 588 | self.datatype, |
|
585 | 589 | self.inputId) |
|
586 | 590 | |
|
587 | 591 | for opConfObj in self.opConfObjList: |
|
588 | 592 | opConfObj.printattr() |
|
589 | 593 | |
|
590 | def createObjects(self): | |
|
594 | def createObjects(self, plotter_queue=None): | |
|
591 | 595 | |
|
592 | 596 | className = eval(self.name) |
|
593 | 597 | procUnitObj = className() |
|
594 | 598 | |
|
595 | 599 | for opConfObj in self.opConfObjList: |
|
596 | 600 | |
|
597 | 601 | if opConfObj.type == 'self': |
|
598 | 602 | continue |
|
599 | 603 | |
|
600 | opObj = opConfObj.createObject() | |
|
604 | opObj = opConfObj.createObject(plotter_queue) | |
|
601 | 605 | |
|
602 | 606 | self.opObjDict[opConfObj.id] = opObj |
|
603 | 607 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
604 | 608 | |
|
605 | 609 | self.procUnitObj = procUnitObj |
|
606 | 610 | |
|
607 | 611 | return procUnitObj |
|
608 | 612 | |
|
609 | 613 | def run(self): |
|
610 | 614 | |
|
611 | 615 | is_ok = False |
|
612 | 616 | |
|
613 | 617 | for opConfObj in self.opConfObjList: |
|
614 | 618 | |
|
615 | 619 | kwargs = {} |
|
616 | 620 | for parmConfObj in opConfObj.getParameterObjList(): |
|
617 | 621 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
618 | 622 | continue |
|
619 | 623 | |
|
620 | 624 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
621 | 625 | |
|
622 | 626 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
623 | 627 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
624 | 628 | opName = opConfObj.name, |
|
625 | 629 | opId = opConfObj.id, |
|
626 | 630 | **kwargs) |
|
627 | 631 | is_ok = is_ok or sts |
|
628 | 632 | |
|
629 | 633 | return is_ok |
|
630 | 634 | |
|
631 | 635 | def close(self): |
|
632 | 636 | |
|
633 | 637 | for opConfObj in self.opConfObjList: |
|
634 | 638 | if opConfObj.type == 'self': |
|
635 | 639 | continue |
|
636 | 640 | |
|
637 | 641 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
638 | 642 | opObj.close() |
|
639 | 643 | |
|
640 | 644 | self.procUnitObj.close() |
|
641 | 645 | |
|
642 | 646 | return |
|
643 | 647 | |
|
644 | 648 | class ReadUnitConf(ProcUnitConf): |
|
645 | 649 | |
|
646 | 650 | path = None |
|
647 | 651 | startDate = None |
|
648 | 652 | endDate = None |
|
649 | 653 | startTime = None |
|
650 | 654 | endTime = None |
|
651 | 655 | |
|
652 | 656 | ELEMENTNAME = 'ReadUnit' |
|
653 | 657 | |
|
654 | 658 | def __init__(self): |
|
655 | 659 | |
|
656 | 660 | self.id = None |
|
657 | 661 | self.datatype = None |
|
658 | 662 | self.name = None |
|
659 | 663 | self.inputId = None |
|
660 | 664 | |
|
661 | 665 | self.parentId = None |
|
662 | 666 | |
|
663 | 667 | self.opConfObjList = [] |
|
664 | 668 | self.opObjList = [] |
|
665 | 669 | |
|
666 | 670 | def getElementName(self): |
|
667 | 671 | |
|
668 | 672 | return self.ELEMENTNAME |
|
669 | 673 | |
|
670 | 674 | def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs): |
|
671 | 675 | |
|
672 | 676 | #Compatible with old signal chain version |
|
673 | 677 | if datatype==None and name==None: |
|
674 | 678 | raise ValueError, "datatype or name should be defined" |
|
675 | 679 | |
|
676 | 680 | if name==None: |
|
677 | 681 | if 'Reader' in datatype: |
|
678 | 682 | name = datatype |
|
679 | 683 | else: |
|
680 | 684 | name = '%sReader' %(datatype) |
|
681 | 685 | |
|
682 | 686 | if datatype==None: |
|
683 | 687 | datatype = name.replace('Reader','') |
|
684 | 688 | |
|
685 | 689 | self.id = id |
|
686 | 690 | self.name = name |
|
687 | 691 | self.datatype = datatype |
|
688 | 692 | |
|
689 | 693 | self.path = os.path.abspath(path) |
|
690 | 694 | self.startDate = startDate |
|
691 | 695 | self.endDate = endDate |
|
692 | 696 | self.startTime = startTime |
|
693 | 697 | self.endTime = endTime |
|
694 | 698 | |
|
695 | 699 | self.inputId = '0' |
|
696 | 700 | self.parentId = parentId |
|
697 | 701 | |
|
698 | 702 | self.addRunOperation(**kwargs) |
|
699 | 703 | |
|
700 | 704 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
701 | 705 | |
|
702 | 706 | #Compatible with old signal chain version |
|
703 | 707 | if datatype==None and name==None: |
|
704 | 708 | raise ValueError, "datatype or name should be defined" |
|
705 | 709 | |
|
706 | 710 | if name==None: |
|
707 | 711 | if 'Reader' in datatype: |
|
708 | 712 | name = datatype |
|
709 | 713 | else: |
|
710 | 714 | name = '%sReader' %(datatype) |
|
711 | 715 | |
|
712 | 716 | if datatype==None: |
|
713 | 717 | datatype = name.replace('Reader','') |
|
714 | 718 | |
|
715 | 719 | self.datatype = datatype |
|
716 | 720 | self.name = name |
|
717 | 721 | self.path = path |
|
718 | 722 | self.startDate = startDate |
|
719 | 723 | self.endDate = endDate |
|
720 | 724 | self.startTime = startTime |
|
721 | 725 | self.endTime = endTime |
|
722 | 726 | |
|
723 | 727 | self.inputId = '0' |
|
724 | 728 | self.parentId = parentId |
|
725 | 729 | |
|
726 | 730 | self.updateRunOperation(**kwargs) |
|
727 | 731 | |
|
728 | 732 | def removeOperations(self): |
|
729 | 733 | |
|
730 | 734 | for obj in self.opConfObjList: |
|
731 | 735 | del obj |
|
732 | 736 | |
|
733 | 737 | self.opConfObjList = [] |
|
734 | 738 | |
|
735 | 739 | def addRunOperation(self, **kwargs): |
|
736 | 740 | |
|
737 | 741 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
738 | 742 | |
|
739 | 743 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
740 | 744 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
741 | 745 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
742 | 746 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
743 | 747 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
744 | 748 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
745 | 749 | |
|
746 | 750 | for key, value in kwargs.items(): |
|
747 | 751 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
748 | 752 | |
|
749 | 753 | return opObj |
|
750 | 754 | |
|
751 | 755 | def updateRunOperation(self, **kwargs): |
|
752 | 756 | |
|
753 | 757 | opObj = self.getOperationObj(name = 'run') |
|
754 | 758 | opObj.removeParameters() |
|
755 | 759 | |
|
756 | 760 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
757 | 761 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
758 | 762 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
759 | 763 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
760 | 764 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
761 | 765 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
762 | 766 | |
|
763 | 767 | for key, value in kwargs.items(): |
|
764 | 768 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
765 | 769 | |
|
766 | 770 | return opObj |
|
767 | 771 | |
|
768 | 772 | # def makeXml(self, projectElement): |
|
769 | 773 | # |
|
770 | 774 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
771 | 775 | # procUnitElement.set('id', str(self.id)) |
|
772 | 776 | # procUnitElement.set('name', self.name) |
|
773 | 777 | # procUnitElement.set('datatype', self.datatype) |
|
774 | 778 | # procUnitElement.set('inputId', str(self.inputId)) |
|
775 | 779 | # |
|
776 | 780 | # for opConfObj in self.opConfObjList: |
|
777 | 781 | # opConfObj.makeXml(procUnitElement) |
|
778 | 782 | |
|
779 | 783 | def readXml(self, upElement): |
|
780 | 784 | |
|
781 | 785 | self.id = upElement.get('id') |
|
782 | 786 | self.name = upElement.get('name') |
|
783 | 787 | self.datatype = upElement.get('datatype') |
|
784 | 788 | self.inputId = upElement.get('inputId') |
|
785 | 789 | |
|
786 | 790 | if self.ELEMENTNAME == "ReadUnit": |
|
787 | 791 | self.datatype = self.datatype.replace("Reader", "") |
|
788 | 792 | |
|
789 | 793 | if self.inputId == 'None': |
|
790 | 794 | self.inputId = '0' |
|
791 | 795 | |
|
792 | 796 | self.opConfObjList = [] |
|
793 | 797 | |
|
794 | 798 | opElementList = upElement.getiterator(OperationConf().getElementName()) |
|
795 | 799 | |
|
796 | 800 | for opElement in opElementList: |
|
797 | 801 | opConfObj = OperationConf() |
|
798 | 802 | opConfObj.readXml(opElement) |
|
799 | 803 | self.opConfObjList.append(opConfObj) |
|
800 | 804 | |
|
801 | 805 | if opConfObj.name == 'run': |
|
802 | 806 | self.path = opConfObj.getParameterValue('path') |
|
803 | 807 | self.startDate = opConfObj.getParameterValue('startDate') |
|
804 | 808 | self.endDate = opConfObj.getParameterValue('endDate') |
|
805 | 809 | self.startTime = opConfObj.getParameterValue('startTime') |
|
806 | 810 | self.endTime = opConfObj.getParameterValue('endTime') |
|
807 | 811 | |
|
808 | 812 | class Project(): |
|
809 | 813 | |
|
810 | 814 | id = None |
|
811 | 815 | name = None |
|
812 | 816 | description = None |
|
813 | 817 | filename = None |
|
814 | 818 | |
|
815 | 819 | procUnitConfObjDict = None |
|
816 | 820 | |
|
817 | 821 | ELEMENTNAME = 'Project' |
|
818 | 822 | |
|
819 | def __init__(self): | |
|
823 | __plotterQueue = None | |
|
824 | ||
|
825 | def __init__(self, filename="./schain.xml", plotter_queue=None): | |
|
820 | 826 | |
|
821 | 827 | self.id = None |
|
822 | 828 | self.name = None |
|
823 | 829 | self.description = None |
|
824 | 830 | |
|
831 | self.filename = filename | |
|
832 | self.__plotterQueue = plotter_queue | |
|
833 | ||
|
825 | 834 | self.procUnitConfObjDict = {} |
|
826 | 835 | |
|
827 | 836 | def __getNewId(self): |
|
828 | 837 | |
|
829 | 838 | id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1 |
|
830 | 839 | |
|
831 | 840 | return str(id) |
|
832 | 841 | |
|
833 | 842 | def getElementName(self): |
|
834 | 843 | |
|
835 | 844 | return self.ELEMENTNAME |
|
836 | 845 | |
|
837 | 846 | def getId(self): |
|
838 | 847 | |
|
839 | 848 | return self.id |
|
840 | 849 | |
|
841 | 850 | def updateId(self, new_id): |
|
842 | 851 | |
|
843 | 852 | self.id = str(new_id) |
|
844 | 853 | |
|
845 | 854 | keyList = self.procUnitConfObjDict.keys() |
|
846 | 855 | keyList.sort() |
|
847 | 856 | |
|
848 | 857 | n = 1 |
|
849 | 858 | newProcUnitConfObjDict = {} |
|
850 | 859 | |
|
851 | 860 | for procKey in keyList: |
|
852 | 861 | |
|
853 | 862 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
854 | 863 | idProcUnit = str(int(self.id)*10 + n) |
|
855 | 864 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
856 | 865 | |
|
857 | 866 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
858 | 867 | n += 1 |
|
859 | 868 | |
|
860 | 869 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
861 | 870 | |
|
862 | 871 | def setup(self, id, name, description): |
|
863 | 872 | |
|
864 | 873 | self.id = str(id) |
|
865 | 874 | self.name = name |
|
866 | 875 | self.description = description |
|
867 | 876 | |
|
868 | 877 | def update(self, name, description): |
|
869 | 878 | |
|
870 | 879 | self.name = name |
|
871 | 880 | self.description = description |
|
872 | 881 | |
|
873 | 882 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
874 | 883 | |
|
875 | 884 | if id is None: |
|
876 | 885 | idReadUnit = self.__getNewId() |
|
877 | 886 | else: |
|
878 | 887 | idReadUnit = str(id) |
|
879 | 888 | |
|
880 | 889 | readUnitConfObj = ReadUnitConf() |
|
881 | 890 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
882 | 891 | |
|
883 | 892 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
884 | 893 | |
|
885 | 894 | return readUnitConfObj |
|
886 | 895 | |
|
887 | 896 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
888 | 897 | |
|
889 | 898 | idProcUnit = self.__getNewId() |
|
890 | 899 | |
|
891 | 900 | procUnitConfObj = ProcUnitConf() |
|
892 | 901 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
893 | 902 | |
|
894 | 903 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
895 | 904 | |
|
896 | 905 | return procUnitConfObj |
|
897 | 906 | |
|
898 | 907 | def removeProcUnit(self, id): |
|
899 | 908 | |
|
900 | 909 | if id in self.procUnitConfObjDict.keys(): |
|
901 | 910 | self.procUnitConfObjDict.pop(id) |
|
902 | 911 | |
|
903 | 912 | def getReadUnitId(self): |
|
904 | 913 | |
|
905 | 914 | readUnitConfObj = self.getReadUnitObj() |
|
906 | 915 | |
|
907 | 916 | return readUnitConfObj.id |
|
908 | 917 | |
|
909 | 918 | def getReadUnitObj(self): |
|
910 | 919 | |
|
911 | 920 | for obj in self.procUnitConfObjDict.values(): |
|
912 | 921 | if obj.getElementName() == "ReadUnit": |
|
913 | 922 | return obj |
|
914 | 923 | |
|
915 | 924 | return None |
|
916 | 925 | |
|
917 | 926 | def getProcUnitObj(self, id=None, name=None): |
|
918 | 927 | |
|
919 | 928 | if id != None: |
|
920 | 929 | return self.procUnitConfObjDict[id] |
|
921 | 930 | |
|
922 | 931 | if name != None: |
|
923 | 932 | return self.getProcUnitObjByName(name) |
|
924 | 933 | |
|
925 | 934 | return None |
|
926 | 935 | |
|
927 | 936 | def getProcUnitObjByName(self, name): |
|
928 | 937 | |
|
929 | 938 | for obj in self.procUnitConfObjDict.values(): |
|
930 | 939 | if obj.name == name: |
|
931 | 940 | return obj |
|
932 | 941 | |
|
933 | 942 | return None |
|
934 | 943 | |
|
935 | 944 | def procUnitItems(self): |
|
936 | 945 | |
|
937 | 946 | return self.procUnitConfObjDict.items() |
|
938 | 947 | |
|
939 | 948 | def makeXml(self): |
|
940 | 949 | |
|
941 | 950 | projectElement = Element('Project') |
|
942 | 951 | projectElement.set('id', str(self.id)) |
|
943 | 952 | projectElement.set('name', self.name) |
|
944 | 953 | projectElement.set('description', self.description) |
|
945 | 954 | |
|
946 | 955 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
947 | 956 | procUnitConfObj.makeXml(projectElement) |
|
948 | 957 | |
|
949 | 958 | self.projectElement = projectElement |
|
950 | 959 | |
|
951 | 960 | def writeXml(self, filename): |
|
952 | 961 | |
|
953 | 962 | abs_file = os.path.abspath(filename) |
|
954 | 963 | |
|
955 | 964 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
956 | 965 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
957 | 966 | return 0 |
|
958 | 967 | |
|
959 | 968 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
960 | 969 | print "File %s already exists and it could not be overwriten" %abs_file |
|
961 | 970 | return 0 |
|
962 | 971 | |
|
963 | 972 | self.makeXml() |
|
964 | 973 | |
|
965 | 974 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
966 | 975 | |
|
967 | 976 | self.filename = abs_file |
|
968 | 977 | |
|
969 | 978 | return 1 |
|
970 | 979 | |
|
971 | 980 | def readXml(self, filename): |
|
972 | 981 | |
|
973 | 982 | abs_file = os.path.abspath(filename) |
|
974 | 983 | |
|
975 | 984 | if not os.path.isfile(abs_file): |
|
976 | 985 | print "%s does not exist" %abs_file |
|
977 | 986 | return 0 |
|
978 | 987 | |
|
979 | 988 | self.projectElement = None |
|
980 | 989 | self.procUnitConfObjDict = {} |
|
981 | 990 | |
|
982 | 991 | self.projectElement = ElementTree().parse(abs_file) |
|
983 | 992 | |
|
984 | 993 | self.project = self.projectElement.tag |
|
985 | 994 | |
|
986 | 995 | self.id = self.projectElement.get('id') |
|
987 | 996 | self.name = self.projectElement.get('name') |
|
988 | 997 | self.description = self.projectElement.get('description') |
|
989 | 998 | |
|
990 | 999 | readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName()) |
|
991 | 1000 | |
|
992 | 1001 | for readUnitElement in readUnitElementList: |
|
993 | 1002 | readUnitConfObj = ReadUnitConf() |
|
994 | 1003 | readUnitConfObj.readXml(readUnitElement) |
|
995 | 1004 | |
|
996 | 1005 | if readUnitConfObj.parentId == None: |
|
997 | 1006 | readUnitConfObj.parentId = self.id |
|
998 | 1007 | |
|
999 | 1008 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1000 | 1009 | |
|
1001 | 1010 | procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName()) |
|
1002 | 1011 | |
|
1003 | 1012 | for procUnitElement in procUnitElementList: |
|
1004 | 1013 | procUnitConfObj = ProcUnitConf() |
|
1005 | 1014 | procUnitConfObj.readXml(procUnitElement) |
|
1006 | 1015 | |
|
1007 | 1016 | if procUnitConfObj.parentId == None: |
|
1008 | 1017 | procUnitConfObj.parentId = self.id |
|
1009 | 1018 | |
|
1010 | 1019 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1011 | 1020 | |
|
1012 | 1021 | return 1 |
|
1013 | 1022 | |
|
1014 | 1023 | def printattr(self): |
|
1015 | 1024 | |
|
1016 | 1025 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1017 | 1026 | self.name, |
|
1018 | 1027 | self.description) |
|
1019 | 1028 | |
|
1020 | 1029 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1021 | 1030 | procUnitConfObj.printattr() |
|
1022 | 1031 | |
|
1023 | 1032 | def createObjects(self): |
|
1024 | 1033 | |
|
1025 | 1034 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1026 | procUnitConfObj.createObjects() | |
|
1035 | procUnitConfObj.createObjects(self.__plotterQueue) | |
|
1027 | 1036 | |
|
1028 | 1037 | def __connect(self, objIN, thisObj): |
|
1029 | 1038 | |
|
1030 | 1039 | thisObj.setInput(objIN.getOutputObj()) |
|
1031 | 1040 | |
|
1032 | 1041 | def connectObjects(self): |
|
1033 | 1042 | |
|
1034 | 1043 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1035 | 1044 | |
|
1036 | 1045 | inputId = thisPUConfObj.getInputId() |
|
1037 | 1046 | |
|
1038 | 1047 | if int(inputId) == 0: |
|
1039 | 1048 | continue |
|
1040 | 1049 | |
|
1041 | 1050 | #Get input object |
|
1042 | 1051 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1043 | 1052 | puObjIN = puConfINObj.getProcUnitObj() |
|
1044 | 1053 | |
|
1045 | 1054 | #Get current object |
|
1046 | 1055 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1047 | 1056 | |
|
1048 | 1057 | self.__connect(puObjIN, thisPUObj) |
|
1049 | 1058 | |
|
1050 | 1059 | def __handleError(self, procUnitConfObj): |
|
1051 | 1060 | |
|
1052 | 1061 | import socket |
|
1053 | 1062 | |
|
1054 | 1063 | err = traceback.format_exception(sys.exc_info()[0], |
|
1055 | 1064 | sys.exc_info()[1], |
|
1056 | 1065 | sys.exc_info()[2]) |
|
1057 | 1066 | |
|
1058 | 1067 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1059 | 1068 | |
|
1060 | 1069 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1061 | 1070 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1062 | 1071 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1063 | 1072 | subtitle += "Configuration file: %s\n" %self.filename |
|
1064 | 1073 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1065 | 1074 | |
|
1066 | 1075 | readUnitConfObj = self.getReadUnitObj() |
|
1067 | 1076 | if readUnitConfObj: |
|
1068 | 1077 | subtitle += "\nInput parameters:\n" |
|
1069 | 1078 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1070 | 1079 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1071 | 1080 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1072 | 1081 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1073 | 1082 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1074 | 1083 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1075 | 1084 | |
|
1076 | 1085 | message = "".join(err) |
|
1077 | 1086 | |
|
1078 | 1087 | sys.stderr.write(message) |
|
1079 | 1088 | |
|
1080 | 1089 | adminObj = schainpy.admin.SchainNotify() |
|
1081 | 1090 | adminObj.sendAlert(message=message, |
|
1082 | 1091 | subject=subject, |
|
1083 | 1092 | subtitle=subtitle, |
|
1084 | 1093 | filename=self.filename) |
|
1085 | 1094 | |
|
1086 | 1095 | def isPaused(self): |
|
1087 | 1096 | return 0 |
|
1088 | 1097 | |
|
1089 | 1098 | def isStopped(self): |
|
1090 | 1099 | return 0 |
|
1091 | 1100 | |
|
1092 | 1101 | def runController(self): |
|
1093 | 1102 | """ |
|
1094 | 1103 | returns 0 when this process has been stopped, 1 otherwise |
|
1095 | 1104 | """ |
|
1096 | 1105 | |
|
1097 | 1106 | if self.isPaused(): |
|
1098 | 1107 | print "Process suspended" |
|
1099 | 1108 | |
|
1100 | 1109 | while True: |
|
1101 | 1110 | sleep(0.1) |
|
1102 | 1111 | |
|
1103 | 1112 | if not self.isPaused(): |
|
1104 | 1113 | break |
|
1105 | 1114 | |
|
1106 | 1115 | if self.isStopped(): |
|
1107 | 1116 | break |
|
1108 | 1117 | |
|
1109 | 1118 | print "Process reinitialized" |
|
1110 | 1119 | |
|
1111 | 1120 | if self.isStopped(): |
|
1112 | 1121 | print "Process stopped" |
|
1113 | 1122 | return 0 |
|
1114 | 1123 | |
|
1115 | 1124 | return 1 |
|
1116 | 1125 | |
|
1126 | def setFilename(self, filename): | |
|
1127 | ||
|
1128 | self.filename = filename | |
|
1129 | ||
|
1130 | def setPlotterQueue(self, plotter_queue): | |
|
1131 | ||
|
1132 | self.__plotterQueue = plotter_queue | |
|
1133 | ||
|
1134 | def getPlotterQueue(self): | |
|
1135 | ||
|
1136 | return self.__plotterQueue | |
|
1137 | ||
|
1117 | 1138 | def run(self): |
|
1118 | 1139 | |
|
1119 | 1140 | |
|
1120 | 1141 | print "*"*60 |
|
1121 | 1142 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1122 | 1143 | print "*"*60 |
|
1123 | 1144 | |
|
1124 | 1145 | |
|
1125 | 1146 | keyList = self.procUnitConfObjDict.keys() |
|
1126 | 1147 | keyList.sort() |
|
1127 | 1148 | |
|
1128 | 1149 | while(True): |
|
1129 | 1150 | |
|
1130 | 1151 | is_ok = False |
|
1131 | 1152 | |
|
1132 | 1153 | for procKey in keyList: |
|
1133 | 1154 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1134 | 1155 | |
|
1135 | 1156 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1136 | 1157 | |
|
1137 | 1158 | try: |
|
1138 | 1159 | sts = procUnitConfObj.run() |
|
1139 | 1160 | is_ok = is_ok or sts |
|
1140 | 1161 | except ValueError, e: |
|
1141 | 1162 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1142 | 1163 | sleep(0.5) |
|
1143 | 1164 | print e |
|
1144 | 1165 | is_ok = False |
|
1145 | 1166 | break |
|
1146 | 1167 | except: |
|
1147 | 1168 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1148 | 1169 | sleep(0.5) |
|
1149 | 1170 | self.__handleError(procUnitConfObj) |
|
1150 | 1171 | is_ok = False |
|
1151 | 1172 | break |
|
1152 | 1173 | |
|
1153 | 1174 | #If every process unit finished so end process |
|
1154 | 1175 | if not(is_ok): |
|
1155 | 1176 | print "Every process unit have finished" |
|
1156 | 1177 | break |
|
1157 | 1178 | |
|
1158 | 1179 | if not self.runController(): |
|
1159 | 1180 | break |
|
1160 | 1181 | |
|
1161 | 1182 | #Closing every process |
|
1162 | 1183 | for procKey in keyList: |
|
1163 | 1184 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1164 | 1185 | procUnitConfObj.close() |
|
1165 | 1186 | |
|
1166 | 1187 | print "Process finished" |
|
1167 | 1188 |
|
|
1168 |
def start(self |
|
|
1169 | ||
|
1170 | if not self.writeXml(filename): | |
|
1171 | return | |
|
1189 | def start(self): | |
|
1172 | 1190 | |
|
1173 |
if not self.re |
|
|
1191 | if not self.writeXml(self.filename): | |
|
1174 | 1192 | return |
|
1175 | 1193 | |
|
1176 | 1194 | self.createObjects() |
|
1177 | 1195 | self.connectObjects() |
|
1178 | 1196 | self.run() |
|
1179 | 1197 | |
|
1180 | 1198 | if __name__ == '__main__': |
|
1181 | 1199 | |
|
1182 | 1200 | desc = "Segundo Test" |
|
1183 | 1201 | filename = "schain.xml" |
|
1184 | 1202 | |
|
1185 | 1203 | controllerObj = Project() |
|
1186 | 1204 | |
|
1187 | 1205 | controllerObj.setup(id = '191', name='test01', description=desc) |
|
1188 | 1206 | |
|
1189 | 1207 | readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage', |
|
1190 | 1208 | path='data/rawdata/', |
|
1191 | 1209 | startDate='2011/01/01', |
|
1192 | 1210 | endDate='2012/12/31', |
|
1193 | 1211 | startTime='00:00:00', |
|
1194 | 1212 | endTime='23:59:59', |
|
1195 | 1213 | online=1, |
|
1196 | 1214 | walk=1) |
|
1197 | 1215 | |
|
1198 | 1216 | procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId()) |
|
1199 | 1217 | |
|
1200 | 1218 | opObj10 = procUnitConfObj0.addOperation(name='selectChannels') |
|
1201 | 1219 | opObj10.addParameter(name='channelList', value='3,4,5', format='intlist') |
|
1202 | 1220 | |
|
1203 | 1221 | opObj10 = procUnitConfObj0.addOperation(name='selectHeights') |
|
1204 | 1222 | opObj10.addParameter(name='minHei', value='90', format='float') |
|
1205 | 1223 | opObj10.addParameter(name='maxHei', value='180', format='float') |
|
1206 | 1224 | |
|
1207 | 1225 | opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external') |
|
1208 | 1226 | opObj12.addParameter(name='n', value='10', format='int') |
|
1209 | 1227 | |
|
1210 | 1228 | procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId()) |
|
1211 | 1229 | procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int') |
|
1212 | 1230 | # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='') |
|
1213 | 1231 | |
|
1214 | 1232 | |
|
1215 | 1233 | opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') |
|
1216 | 1234 | opObj11.addParameter(name='idfigure', value='1', format='int') |
|
1217 | 1235 | opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str') |
|
1218 | 1236 | opObj11.addParameter(name='zmin', value='40', format='int') |
|
1219 | 1237 | opObj11.addParameter(name='zmax', value='90', format='int') |
|
1220 | 1238 | opObj11.addParameter(name='showprofile', value='1', format='int') |
|
1221 | 1239 | |
|
1222 | 1240 | print "Escribiendo el archivo XML" |
|
1223 | 1241 | |
|
1224 | 1242 | controllerObj.writeXml(filename) |
|
1225 | 1243 | |
|
1226 | 1244 | print "Leyendo el archivo XML" |
|
1227 | 1245 | controllerObj.readXml(filename) |
|
1228 | 1246 | #controllerObj.printattr() |
|
1229 | 1247 | |
|
1230 | 1248 | controllerObj.createObjects() |
|
1231 | 1249 | controllerObj.connectObjects() |
|
1232 | 1250 | controllerObj.run() |
|
1233 | 1251 | |
|
1234 | 1252 | No newline at end of file |
@@ -1,141 +1,139 | |||
|
1 | 1 | import threading |
|
2 | 2 | |
|
3 | 3 | from schainpy.controller import Project |
|
4 | 4 | |
|
5 | 5 | class ControllerThread(threading.Thread, Project): |
|
6 | 6 | |
|
7 | def __init__(self, filename): | |
|
7 | def __init__(self, filename=None, plotter_queue=None): | |
|
8 | 8 | |
|
9 | 9 | threading.Thread.__init__(self) |
|
10 | Project.__init__(self) | |
|
10 | Project.__init__(self, filename, plotter_queue) | |
|
11 | 11 | |
|
12 | 12 | self.setDaemon(True) |
|
13 | 13 | |
|
14 | self.filename = filename | |
|
15 | ||
|
16 | 14 | self.lock = threading.Lock() |
|
17 | 15 | self.control = {'stop':False, 'pause':False} |
|
18 | 16 | |
|
19 | 17 | def __del__(self): |
|
20 | 18 | |
|
21 | 19 | self.control['stop'] = True |
|
22 | 20 | |
|
23 | 21 | def stop(self): |
|
24 | 22 | |
|
25 | 23 | self.lock.acquire() |
|
26 | 24 | |
|
27 | 25 | self.control['stop'] = True |
|
28 | 26 | |
|
29 | 27 | self.lock.release() |
|
30 | 28 | |
|
31 | 29 | def pause(self): |
|
32 | 30 | |
|
33 | 31 | self.lock.acquire() |
|
34 | 32 | |
|
35 | 33 | self.control['pause'] = not(self.control['pause']) |
|
36 | 34 | paused = self.control['pause'] |
|
37 | 35 | |
|
38 | 36 | self.lock.release() |
|
39 | 37 | |
|
40 | 38 | return paused |
|
41 | 39 | |
|
42 | 40 | def isPaused(self): |
|
43 | 41 | |
|
44 | 42 | self.lock.acquire() |
|
45 | 43 | paused = self.control['pause'] |
|
46 | 44 | self.lock.release() |
|
47 | 45 | |
|
48 | 46 | return paused |
|
49 | 47 | |
|
50 | 48 | def isStopped(self): |
|
51 | 49 | |
|
52 | 50 | self.lock.acquire() |
|
53 | 51 | stopped = self.control['stop'] |
|
54 | 52 | self.lock.release() |
|
55 | 53 | |
|
56 | 54 | return stopped |
|
57 | 55 | |
|
58 | 56 | def run(self): |
|
59 | 57 | self.control['stop'] = False |
|
60 | 58 | self.control['pause'] = False |
|
61 | 59 | |
|
62 | 60 | self.readXml(self.filename) |
|
63 | 61 | self.createObjects() |
|
64 | 62 | self.connectObjects() |
|
65 | 63 | Project.run(self) |
|
66 | 64 | |
|
67 | 65 | def isRunning(self): |
|
68 | 66 | |
|
69 | 67 | return self.is_alive() |
|
70 | 68 | |
|
71 | 69 | def isFinished(self): |
|
72 | 70 | |
|
73 | 71 | return not self.is_alive() |
|
74 | 72 | |
|
75 | 73 | # from PyQt4 import QtCore |
|
76 | 74 | # from PyQt4.QtCore import SIGNAL |
|
77 | 75 | # |
|
78 | 76 | # class ControllerQThread(QtCore.QThread, Project): |
|
79 | 77 | # |
|
80 | 78 | # def __init__(self, filename): |
|
81 | 79 | # |
|
82 | 80 | # QtCore.QThread.__init__(self) |
|
83 | 81 | # Project.__init__(self) |
|
84 | 82 | # |
|
85 | 83 | # self.filename = filename |
|
86 | 84 | # |
|
87 | 85 | # self.lock = threading.Lock() |
|
88 | 86 | # self.control = {'stop':False, 'pause':False} |
|
89 | 87 | # |
|
90 | 88 | # def __del__(self): |
|
91 | 89 | # |
|
92 | 90 | # self.control['stop'] = True |
|
93 | 91 | # self.wait() |
|
94 | 92 | # |
|
95 | 93 | # def stop(self): |
|
96 | 94 | # |
|
97 | 95 | # self.lock.acquire() |
|
98 | 96 | # |
|
99 | 97 | # self.control['stop'] = True |
|
100 | 98 | # |
|
101 | 99 | # self.lock.release() |
|
102 | 100 | # |
|
103 | 101 | # def pause(self): |
|
104 | 102 | # |
|
105 | 103 | # self.lock.acquire() |
|
106 | 104 | # |
|
107 | 105 | # self.control['pause'] = not(self.control['pause']) |
|
108 | 106 | # paused = self.control['pause'] |
|
109 | 107 | # |
|
110 | 108 | # self.lock.release() |
|
111 | 109 | # |
|
112 | 110 | # return paused |
|
113 | 111 | # |
|
114 | 112 | # def isPaused(self): |
|
115 | 113 | # |
|
116 | 114 | # self.lock.acquire() |
|
117 | 115 | # paused = self.control['pause'] |
|
118 | 116 | # self.lock.release() |
|
119 | 117 | # |
|
120 | 118 | # return paused |
|
121 | 119 | # |
|
122 | 120 | # def isStopped(self): |
|
123 | 121 | # |
|
124 | 122 | # self.lock.acquire() |
|
125 | 123 | # stopped = self.control['stop'] |
|
126 | 124 | # self.lock.release() |
|
127 | 125 | # |
|
128 | 126 | # return stopped |
|
129 | 127 | # |
|
130 | 128 | # def run(self): |
|
131 | 129 | # |
|
132 | 130 | # self.control['stop'] = False |
|
133 | 131 | # self.control['pause'] = False |
|
134 | 132 | # |
|
135 | 133 | # self.readXml(self.filename) |
|
136 | 134 | # self.createObjects() |
|
137 | 135 | # self.connectObjects() |
|
138 | 136 | # self.emit( SIGNAL( "jobStarted( PyQt_PyObject )" ), 1) |
|
139 | 137 | # Project.run(self) |
|
140 | 138 | # self.emit( SIGNAL( "jobFinished( PyQt_PyObject )" ), 1) |
|
141 | 139 | # No newline at end of file |
@@ -1,5 +1,6 | |||
|
1 | 1 | from jroplot_voltage import * |
|
2 | 2 | from jroplot_spectra import * |
|
3 | 3 | from jroplot_heispectra import * |
|
4 | 4 | from jroplot_correlation import * |
|
5 | 5 | from jroplot_parameters import * |
|
6 | from jroplotter import * No newline at end of file |
@@ -1,286 +1,294 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: murco $ |
|
4 | 4 | $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $ |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | class ProcessingUnit(object): |
|
8 | 8 | |
|
9 | 9 | """ |
|
10 | 10 | Esta es la clase base para el procesamiento de datos. |
|
11 | 11 | |
|
12 | 12 | Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser: |
|
13 | 13 | - Metodos internos (callMethod) |
|
14 | 14 | - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos |
|
15 | 15 | tienen que ser agreagados con el metodo "add". |
|
16 | 16 | |
|
17 | 17 | """ |
|
18 | 18 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
19 | 19 | dataIn = None |
|
20 | 20 | dataInList = [] |
|
21 | 21 | |
|
22 | 22 | # objeto de datos de entrada (Voltage, Spectra o Correlation) |
|
23 | 23 | dataOut = None |
|
24 | 24 | |
|
25 | 25 | operations2RunDict = None |
|
26 | 26 | |
|
27 | 27 | isConfig = False |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | def __init__(self): |
|
31 | 31 | |
|
32 | 32 | self.dataIn = None |
|
33 | 33 | self.dataInList = [] |
|
34 | 34 | |
|
35 | 35 | self.dataOut = None |
|
36 | 36 | |
|
37 | 37 | self.operations2RunDict = {} |
|
38 | 38 | |
|
39 | 39 | self.isConfig = False |
|
40 | 40 | |
|
41 | 41 | def addOperation(self, opObj, objId): |
|
42 | 42 | |
|
43 | 43 | """ |
|
44 | 44 | Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el |
|
45 | 45 | identificador asociado a este objeto. |
|
46 | 46 | |
|
47 | 47 | Input: |
|
48 | 48 | |
|
49 | 49 | object : objeto de la clase "Operation" |
|
50 | 50 | |
|
51 | 51 | Return: |
|
52 | 52 | |
|
53 | 53 | objId : identificador del objeto, necesario para ejecutar la operacion |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 | 56 | self.operations2RunDict[objId] = opObj |
|
57 | 57 | |
|
58 | 58 | return objId |
|
59 | 59 | |
|
60 | 60 | def getOperationObj(self, objId): |
|
61 | 61 | |
|
62 | 62 | if objId not in self.operations2RunDict.keys(): |
|
63 | 63 | return None |
|
64 | 64 | |
|
65 | 65 | return self.operations2RunDict[objId] |
|
66 | 66 | |
|
67 | 67 | def operation(self, **kwargs): |
|
68 | 68 | |
|
69 | 69 | """ |
|
70 | 70 | Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los |
|
71 | 71 | atributos del objeto dataOut |
|
72 | 72 | |
|
73 | 73 | Input: |
|
74 | 74 | |
|
75 | 75 | **kwargs : Diccionario de argumentos de la funcion a ejecutar |
|
76 | 76 | """ |
|
77 | 77 | |
|
78 | 78 | raise NotImplementedError |
|
79 | 79 | |
|
80 | 80 | def callMethod(self, name, **kwargs): |
|
81 | 81 | |
|
82 | 82 | """ |
|
83 | 83 | Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase. |
|
84 | 84 | |
|
85 | 85 | Input: |
|
86 | 86 | name : nombre del metodo a ejecutar |
|
87 | 87 | |
|
88 | 88 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
89 | 89 | |
|
90 | 90 | """ |
|
91 | 91 | |
|
92 | 92 | #Checking the inputs |
|
93 | 93 | if name == 'run': |
|
94 | 94 | |
|
95 | 95 | if not self.checkInputs(): |
|
96 | 96 | self.dataOut.flagNoData = True |
|
97 | 97 | return False |
|
98 | 98 | else: |
|
99 | 99 | #Si no es un metodo RUN la entrada es la misma dataOut (interna) |
|
100 | 100 | if self.dataOut.isEmpty(): |
|
101 | 101 | return False |
|
102 | 102 | |
|
103 | 103 | #Getting the pointer to method |
|
104 | 104 | methodToCall = getattr(self, name) |
|
105 | 105 | |
|
106 | 106 | #Executing the self method |
|
107 | 107 | methodToCall(**kwargs) |
|
108 | 108 | |
|
109 | 109 | #Checkin the outputs |
|
110 | 110 | |
|
111 | 111 | # if name == 'run': |
|
112 | 112 | # pass |
|
113 | 113 | # else: |
|
114 | 114 | # pass |
|
115 | 115 | # |
|
116 | 116 | # if name != 'run': |
|
117 | 117 | # return True |
|
118 | 118 | |
|
119 | 119 | if self.dataOut is None: |
|
120 | 120 | return False |
|
121 | 121 | |
|
122 | 122 | if self.dataOut.isEmpty(): |
|
123 | 123 | return False |
|
124 | 124 | |
|
125 | 125 | return True |
|
126 | 126 | |
|
127 | 127 | def callObject(self, objId, **kwargs): |
|
128 | 128 | |
|
129 | 129 | """ |
|
130 | 130 | Ejecuta la operacion asociada al identificador del objeto "objId" |
|
131 | 131 | |
|
132 | 132 | Input: |
|
133 | 133 | |
|
134 | 134 | objId : identificador del objeto a ejecutar |
|
135 | 135 | |
|
136 | 136 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. |
|
137 | 137 | |
|
138 | 138 | Return: |
|
139 | 139 | |
|
140 | 140 | None |
|
141 | 141 | """ |
|
142 | 142 | |
|
143 | 143 | if self.dataOut.isEmpty(): |
|
144 | 144 | return False |
|
145 | 145 | |
|
146 | 146 | externalProcObj = self.operations2RunDict[objId] |
|
147 | 147 | |
|
148 | 148 | externalProcObj.run(self.dataOut, **kwargs) |
|
149 | 149 | |
|
150 | 150 | return True |
|
151 | 151 | |
|
152 | 152 | def call(self, opType, opName=None, opId=None, **kwargs): |
|
153 | 153 | |
|
154 | 154 | """ |
|
155 | 155 | Return True si ejecuta la operacion interna nombrada "opName" o la operacion externa |
|
156 | 156 | identificada con el id "opId"; con los argumentos "**kwargs". |
|
157 | 157 | |
|
158 | 158 | False si la operacion no se ha ejecutado. |
|
159 | 159 | |
|
160 | 160 | Input: |
|
161 | 161 | |
|
162 | 162 | opType : Puede ser "self" o "external" |
|
163 | 163 | |
|
164 |
|
|
|
164 | Depende del tipo de operacion para llamar a:callMethod or callObject: | |
|
165 | 165 | |
|
166 |
1. |
|
|
166 | 1. If opType = "self": Llama a un metodo propio de esta clase: | |
|
167 | 167 | |
|
168 |
|
|
|
168 | name_method = getattr(self, name) | |
|
169 | name_method(**kwargs) | |
|
169 | 170 | |
|
170 | 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella: | |
|
171 | 171 | |
|
172 |
|
|
|
172 | 2. If opType = "other" o"external": Llama al metodo "run()" de una instancia de la | |
|
173 | clase "Operation" o de un derivado de ella: | |
|
174 | ||
|
175 | instanceName = self.operationList[opId] | |
|
176 | instanceName.run(**kwargs) | |
|
173 | 177 | |
|
174 | 178 | opName : Si la operacion es interna (opType = 'self'), entonces el "opName" sera |
|
175 | 179 | usada para llamar a un metodo interno de la clase Processing |
|
176 | 180 | |
|
177 |
opId : Si la operacion es externa (opType = 'other'), entonces el |
|
|
178 |
usada para llamar al metodo "run" de la clase Operation |
|
|
181 | opId : Si la operacion es externa (opType = 'other' o 'external), entonces el | |
|
182 | "opId" sera usada para llamar al metodo "run" de la clase Operation | |
|
183 | registrada anteriormente con ese Id | |
|
179 | 184 | |
|
180 | 185 | Exception: |
|
181 | 186 | Este objeto de tipo Operation debe de haber sido agregado antes con el metodo: |
|
182 | 187 | "addOperation" e identificado con el valor "opId" = el id de la operacion. |
|
183 |
De lo contrario retornara un error del tipo |
|
|
188 | De lo contrario retornara un error del tipo ValueError | |
|
184 | 189 | |
|
185 | 190 | """ |
|
186 | 191 | |
|
187 | 192 | if opType == 'self': |
|
188 | 193 | |
|
189 | 194 | if not opName: |
|
190 | 195 | raise ValueError, "opName parameter should be defined" |
|
191 | 196 | |
|
192 | 197 | sts = self.callMethod(opName, **kwargs) |
|
193 | 198 | |
|
194 | if opType == 'other' or opType == 'external': | |
|
199 | elif opType == 'other' or opType == 'external' or opType == 'plotter': | |
|
195 | 200 | |
|
196 | 201 | if not opId: |
|
197 | 202 | raise ValueError, "opId parameter should be defined" |
|
198 | 203 | |
|
199 | 204 | if opId not in self.operations2RunDict.keys(): |
|
200 |
raise ValueError, " |
|
|
205 | raise ValueError, "Any operation with id=%s has been added" %str(opId) | |
|
201 | 206 | |
|
202 | 207 | sts = self.callObject(opId, **kwargs) |
|
203 | 208 | |
|
209 | else: | |
|
210 | raise ValueError, "opType should be 'self', 'external' or 'plotter'; and not '%s'" %opType | |
|
211 | ||
|
204 | 212 | return sts |
|
205 | 213 | |
|
206 | 214 | def setInput(self, dataIn): |
|
207 | 215 | |
|
208 | 216 | self.dataIn = dataIn |
|
209 | 217 | self.dataInList.append(dataIn) |
|
210 | 218 | |
|
211 | 219 | def getOutputObj(self): |
|
212 | 220 | |
|
213 | 221 | return self.dataOut |
|
214 | 222 | |
|
215 | 223 | def checkInputs(self): |
|
216 | 224 | |
|
217 | 225 | for thisDataIn in self.dataInList: |
|
218 | 226 | |
|
219 | 227 | if thisDataIn.isEmpty(): |
|
220 | 228 | return False |
|
221 | 229 | |
|
222 | 230 | return True |
|
223 | 231 | |
|
224 | 232 | def setup(self): |
|
225 | 233 | |
|
226 | 234 | raise NotImplementedError |
|
227 | 235 | |
|
228 | 236 | def run(self): |
|
229 | 237 | |
|
230 | 238 | raise NotImplementedError |
|
231 | 239 | |
|
232 | 240 | def close(self): |
|
233 | 241 | #Close every thread, queue or any other object here is it is neccesary. |
|
234 | 242 | return |
|
235 | 243 | |
|
236 | 244 | class Operation(object): |
|
237 | 245 | |
|
238 | 246 | """ |
|
239 | 247 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit |
|
240 | 248 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de |
|
241 | 249 | acumulacion dentro de esta clase |
|
242 | 250 | |
|
243 | 251 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) |
|
244 | 252 | |
|
245 | 253 | """ |
|
246 | 254 | |
|
247 | 255 | __buffer = None |
|
248 | 256 | isConfig = False |
|
249 | 257 | |
|
250 | 258 | def __init__(self): |
|
251 | 259 | |
|
252 | 260 | self.__buffer = None |
|
253 | 261 | self.isConfig = False |
|
254 | 262 | |
|
255 | 263 | def setup(self): |
|
256 | 264 | |
|
257 | 265 | self.isConfig = True |
|
258 | 266 | |
|
259 | 267 | raise NotImplementedError |
|
260 | 268 | |
|
261 | 269 | def run(self, dataIn, **kwargs): |
|
262 | 270 | |
|
263 | 271 | """ |
|
264 | 272 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los |
|
265 | 273 | atributos del objeto dataIn. |
|
266 | 274 | |
|
267 | 275 | Input: |
|
268 | 276 | |
|
269 | 277 | dataIn : objeto del tipo JROData |
|
270 | 278 | |
|
271 | 279 | Return: |
|
272 | 280 | |
|
273 | 281 | None |
|
274 | 282 | |
|
275 | 283 | Affected: |
|
276 | 284 | __buffer : buffer de recepcion de datos. |
|
277 | 285 | |
|
278 | 286 | """ |
|
279 | 287 | if not self.isConfig: |
|
280 | 288 | self.setup(**kwargs) |
|
281 | 289 | |
|
282 | 290 | raise NotImplementedError |
|
283 | 291 | |
|
284 | 292 | def close(self): |
|
285 | 293 | |
|
286 | 294 | pass No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now