##// END OF EJS Templates
Miguel Valdez -
r681:a13b962c6023
parent child
Show More
@@ -0,0 +1,346
1 """The admin module contains all administrative classes relating to the schain python api.
2
3 The main role of this module is to send some reports. It contains a
4 notification class and a standard error handing class.
5
6 $Id: admin.py 3966 2015-12-01 14:32:29Z miguel.urco $
7 """
8 import os
9 import smtplib
10 import ConfigParser
11 import StringIO
12
13 from email.mime.text import MIMEText
14 from email.mime.application import MIMEApplication
15 from email.mime.multipart import MIMEMultipart
16
17 class SchainConfigure():
18
19 __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe"
20 __DEFAULT_ADMINISTRATOR_EMAIL = "miguel.urco@jro.igp.gob.pe"
21 __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe"
22
23 __SCHAIN_ADMINISTRATOR_EMAIL = "CONTACT"
24 __SCHAIN_EMAIL_SERVER = "MAILSERVER"
25 __SCHAIN_SENDER_EMAIL = "MAILSERVER_ACCOUNT"
26
27 def __init__(self, initFile = None):
28
29 # Set configuration file
30 if (initFile == None):
31 self.__confFilePath = "/etc/schain.conf"
32 else:
33 self.__confFilePath = initFile
34
35 # open configuration file
36 try:
37 self.__confFile = open(self.__confFilePath, "r")
38 except IOError:
39 # can't read from file - use all hard-coded values
40 self.__initFromHardCode()
41 return
42
43 # create Parser using standard module ConfigParser
44 self.__parser = ConfigParser.ConfigParser()
45
46 # read conf file into a StringIO with "[madrigal]\n" section heading prepended
47 strConfFile = StringIO.StringIO("[schain]\n" + self.__confFile.read())
48
49 # parse StringIO configuration file
50 self.__parser.readfp(strConfFile)
51
52 # read information from configuration file
53 self.__readConfFile()
54
55 # close conf file
56 self.__confFile.close()
57
58
59 def __initFromHardCode(self):
60
61 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
62 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
63 self.__email_server = self.__DEFAULT_EMAIL_SERVER
64
65 def __readConfFile(self):
66 """__readConfFile is a private helper function that reads information from the parsed config file.
67
68 Inputs: None
69
70 Returns: Void.
71
72 Affects: Initializes class member variables that are found in the config file.
73
74 Exceptions: MadrigalError thrown if any key not found.
75 """
76
77 # get the sender email
78 try:
79 self.__sender_email = self.__parser.get("schain", self.__SCHAIN_SENDER_EMAIL)
80 except:
81 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
82
83 # get the administrator email
84 try:
85 self.__admin_email = self.__parser.get("schain", self.__SCHAIN_ADMINISTRATOR_EMAIL)
86 except:
87 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
88
89 # get the server email
90 try:
91 self.__email_server = self.__parser.get("schain", self.__SCHAIN_EMAIL_SERVER)
92 except:
93 self.__email_server = self.__DEFAULT_EMAIL_SERVER
94
95 def getEmailServer(self):
96
97 return self.__email_server
98
99 def getSenderEmail(self):
100
101 return self.__sender_email
102
103 def getAdminEmail(self):
104
105 return self.__admin_email
106
107 class SchainNotify:
108 """SchainNotify is an object used to send messages to an administrator about a Schain software.
109
110 This object provides functions needed to send messages to an administrator about a Schain , for now
111 only sendAlert, which sends an email to the site administrator found is ADMIN_EMAIL
112
113 Usage example:
114
115 import schainpy.admin
116
117 try:
118
119 adminObj = schainpy.admin.SchainNotify()
120 adminObj.sendAlert('This is important!', 'Important Message')
121
122 except schainpy.admin.SchainError, e:
123
124 print e.getExceptionStr()
125
126
127 Non-standard Python modules used:
128 None
129
130 Exceptions thrown: None - Note that SchainNotify tries every trick it knows to avoid
131 throwing exceptions, since this is the class that will generally be called when there is a problem.
132
133 Change history:
134
135 Written by "Miguel Urco":mailto:miguel.urco@jro.igp.gob.pe Dec. 1, 2015
136 """
137
138 #constants
139
140 def __init__(self):
141 """__init__ initializes SchainNotify by getting some basic information from SchainDB and SchainSite.
142
143 Note that SchainNotify tries every trick it knows to avoid throwing exceptions, since
144 this is the class that will generally be called when there is a problem.
145
146 Inputs: Existing SchainDB object, by default = None.
147
148 Returns: void
149
150 Affects: Initializes self.__binDir.
151
152 Exceptions: None.
153 """
154
155 # note that the main configuration file is unavailable
156 # the best that can be done is send an email to root using localhost mailserver
157 confObj = SchainConfigure()
158
159 self.__emailFromAddress = confObj.getSenderEmail()
160 self.__emailToAddress = confObj.getAdminEmail()
161 self.__emailServer = confObj.getEmailServer()
162
163 def sendEmail(self, email_from, email_to, subject='Error running ...', message="", subtitle="", filename="", html_format=True):
164
165 msg = MIMEMultipart()
166 msg['Subject'] = subject
167 msg['From'] = "(Python SChain API): " + email_from
168 msg['Reply-to'] = email_from
169 msg['To'] = email_to
170
171 # That is what u see if dont have an email reader:
172 msg.preamble = 'SChainPy'
173
174 if html_format:
175 message = "<h1> %s </h1>" %subject + "<h3>" + subtitle.replace("\n", "</h3><h3>\n") + "</h3>" + message.replace("\n", "<br>\n")
176 message = "<html>\n" + message + '</html>'
177
178 # This is the textual part:
179 part = MIMEText(message, "html")
180 else:
181 message = subject + "\n" + subtitle + "\n" + message
182 part = MIMEText(message)
183
184 msg.attach(part)
185
186 if os.path.isfile(filename):
187 # This is the binary part(The Attachment):
188 part = MIMEApplication(open(filename,"rb").read())
189 part.add_header('Content-Disposition',
190 'attachment',
191 filename=os.path.basename(filename))
192 msg.attach(part)
193
194 # Create an instance in SMTP server
195 smtp = smtplib.SMTP(self.__emailServer)
196 # Start the server:
197 # smtp.ehlo()
198 # smtp.login(email_from, email_from_pass)
199
200 # Send the email
201 smtp.sendmail(msg['From'], msg['To'], msg.as_string())
202 smtp.quit()
203
204
205 def sendAlert(self, message, subject = "", subtitle="", filename=""):
206 """sendAlert sends an email with the given message and optional title.
207
208 Inputs: message (string), and optional title (string)
209
210 Returns: void
211
212 Affects: none
213
214 Exceptions: None.
215 """
216 print "***** Sending alert to %s *****" %self.__emailToAddress
217 # set up message
218
219 self.sendEmail(email_from=self.__emailFromAddress,
220 email_to=self.__emailToAddress,
221 subject=subject,
222 message=message,
223 subtitle=subtitle,
224 filename=filename)
225
226 print "***** Your system administrator has been notified *****"
227
228
229 def notify(self, email, message, subject = "", subtitle="", filename=""):
230 """notify sends an email with the given message and title to email.
231
232 Inputs: email (string), message (string), and subject (string)
233
234 Returns: void
235
236 Affects: none
237
238 Exceptions: None.
239 """
240
241 print "Notifying to %s ..." %email
242
243 self.sendEmail(email_from=self.__emailFromAddress,
244 email_to=email,
245 subject=subject,
246 message=message,
247 subtitle=subtitle,
248 filename=filename)
249
250 print "***** Your system administrator has been notified *****"
251
252 class SchainError:
253 """SchainError is an exception class that is thrown for all known errors in using Schain Py lib.
254
255 Usage example:
256
257 import sys, traceback
258 import schainpy.admin
259
260 try:
261
262 test = open('ImportantFile.txt', 'r')
263
264 except:
265
266 raise schainpy.admin.SchainError('ImportantFile.txt not opened!',
267 traceback.format_exception(sys.exc_info()[0],
268 sys.exc_info()[1],
269 sys.exc_info()[2]))
270 """
271
272
273 def __init__(self, strInterpretation, exceptionList):
274 """ __init__ gathers the interpretation string along with all information from sys.exc_info().
275
276 Inputs: strIntepretation - A string representing the programmer's interpretation of
277 why the exception occurred
278
279 exceptionList - a list of strings completely describing the exception.
280 Generated by traceback.format_exception(sys.exc_info()[0],
281 sys.exc_info()[1],
282 sys.exc_info()[2])
283
284 Returns: Void.
285
286 Affects: Initializes class member variables _strInterp, _strExcList.
287
288 Exceptions: None.
289 """
290
291 self._strInterp = strInterpretation
292 self._strExcList = exceptionList
293
294
295 def getExceptionStr(self):
296 """ getExceptionStr returns a formatted string ready for printing completely describing the exception.
297
298 Inputs: None
299
300 Returns: A formatted string ready for printing completely describing the exception.
301
302 Affects: None
303
304 Exceptions: None.
305 """
306 excStr = 'The following Schain Python exception has occurred:\n'
307 excStr = excStr + self._strInterp + '\n\n'
308
309 if self._strExcList != None:
310 for item in self._strExcList:
311 excStr = excStr + str(item) + '\n'
312
313 return excStr
314
315 def __str__(self):
316 return(self.getExceptionStr())
317
318
319 def getExceptionHtml(self):
320 """ getExceptionHtml returns an Html formatted string completely describing the exception.
321
322 Inputs: None
323
324 Returns: A formatted string ready for printing completely describing the exception.
325
326 Affects: None
327
328 Exceptions: None.
329 """
330
331 excStr = '<BR>The following Schain Python exception has occurred:\n<BR>'
332 excStr = excStr + self._strInterp + '\n<BR>\n'
333
334 if self._strExcList != None:
335 for item in self._strExcList:
336 excStr = excStr + str(item) + '\n<BR>'
337
338 return excStr
339
340 if __name__ == '__main__':
341
342 test = SchainNotify()
343
344 test.sendAlert('This is a message from the python module SchainNotify', 'Test from SchainNotify')
345
346 print 'Hopefully message sent - check.'
@@ -0,0 +1,5
1 #Copy this file to /etc/schain.conf
2 [schain]
3 CONTACT = miguel.urco@jro.igp.gob.pe
4 MAILSERVER = jro-zimbra.igp.gob.pe
5 MALSERVER_ACCOUNT = notifier-schain@jro.igp.gob.pe No newline at end of file
@@ -1,26 +1,29
1 1 VERSIONS:
2 2
3 3 2.1.2:
4 4 -jroutils_ftp.py: Bug fixed, Any error sending file stopped the Server Thread
5 5 Server thread opens and closes remote server each time file list is sent
6 6 -jroplot_spectra.py: Noise path was not being created saving noise data.
7 7 -jroIO_base.py: startTime can be greater than endTime
8 8
9 9 2.1.3:
10 10 -jroplot_heispectra.py: SpectraHeisScope was not showing the right channels
11 11 -jroproc_voltage.py: Bug fixed selecting profiles (self.nProfiles took a wrong value),
12 12 Bug fixed selecting heights by block (selecting profiles instead heights)
13 13 -jroproc_voltage.py: New feature added: decoding data by block using FFT.
14 14 -jroIO_heispectra.py: Bug fixed in FitsReader. Using local Fits object instead schainpy.mode.data.jrodata.Fits object.
15 15 -jroIO_heispectra.py: Channel index list does not exist.
16 16
17 17 2.1.3.1:
18 18 -GUI: every icon were resized
19 19 -jroproc_voltage.py: Print a message when "Read from code" option is selected and the code is not defined inside data file
20 20
21 21 2.1.3.2:
22 22 -GUI: user interaction enhanced
23 23 -controller_api.py: Safe access to ControllerThead
24 24
25 25 2.1.3.3:
26 -Colored Button Icons were added to GUI No newline at end of file
26 -Colored Button Icons were added to GUI
27
28 2.1.4:
29 -Sending error notifications to signal chain administrator No newline at end of file
@@ -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.3.3" No newline at end of file
7 __version__ = "2.1.4" No newline at end of file
@@ -1,1150 +1,1206
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
6 from xml.dom import minidom
7
8 from model import *
9 from time import sleep
10 5
11 6 import sys
12 7 import ast
13 8 import traceback
9 import schainpy
10 import schainpy.admin
14 11
15 SCHAIN_MAIL = "miguel.urco@jro.igp.gob.pe"
16 EMAIL_SERVER = "jro.igp.gob.pe"
12 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
13 from xml.dom import minidom
14
15 from schainpy.model import *
16 from time import sleep
17 17
18 18 def prettify(elem):
19 19 """Return a pretty-printed XML string for the Element.
20 20 """
21 21 rough_string = tostring(elem, 'utf-8')
22 22 reparsed = minidom.parseString(rough_string)
23 23 return reparsed.toprettyxml(indent=" ")
24 24
25 25 class ParameterConf():
26 26
27 27 id = None
28 28 name = None
29 29 value = None
30 30 format = None
31 31
32 32 __formated_value = None
33 33
34 34 ELEMENTNAME = 'Parameter'
35 35
36 36 def __init__(self):
37 37
38 38 self.format = 'str'
39 39
40 40 def getElementName(self):
41 41
42 42 return self.ELEMENTNAME
43 43
44 44 def getValue(self):
45 45
46 46 value = self.value
47 47 format = self.format
48 48
49 49 if self.__formated_value != None:
50 50
51 51 return self.__formated_value
52 52
53 53 if format == 'str':
54 54 self.__formated_value = str(value)
55 55 return self.__formated_value
56 56
57 57 if value == '':
58 58 raise ValueError, "%s: This parameter value is empty" %self.name
59 59
60 60 if format == 'list':
61 61 strList = value.split(',')
62 62
63 63 self.__formated_value = strList
64 64
65 65 return self.__formated_value
66 66
67 67 if format == 'intlist':
68 68 """
69 69 Example:
70 70 value = (0,1,2)
71 71 """
72 72 value = value.replace('(', '')
73 73 value = value.replace(')', '')
74 74
75 75 value = value.replace('[', '')
76 76 value = value.replace(']', '')
77 77
78 78 strList = value.split(',')
79 79 intList = [int(float(x)) for x in strList]
80 80
81 81 self.__formated_value = intList
82 82
83 83 return self.__formated_value
84 84
85 85 if format == 'floatlist':
86 86 """
87 87 Example:
88 88 value = (0.5, 1.4, 2.7)
89 89 """
90 90
91 91 value = value.replace('(', '')
92 92 value = value.replace(')', '')
93 93
94 94 value = value.replace('[', '')
95 95 value = value.replace(']', '')
96 96
97 97 strList = value.split(',')
98 98 floatList = [float(x) for x in strList]
99 99
100 100 self.__formated_value = floatList
101 101
102 102 return self.__formated_value
103 103
104 104 if format == 'date':
105 105 strList = value.split('/')
106 106 intList = [int(x) for x in strList]
107 107 date = datetime.date(intList[0], intList[1], intList[2])
108 108
109 109 self.__formated_value = date
110 110
111 111 return self.__formated_value
112 112
113 113 if format == 'time':
114 114 strList = value.split(':')
115 115 intList = [int(x) for x in strList]
116 116 time = datetime.time(intList[0], intList[1], intList[2])
117 117
118 118 self.__formated_value = time
119 119
120 120 return self.__formated_value
121 121
122 122 if format == 'pairslist':
123 123 """
124 124 Example:
125 125 value = (0,1),(1,2)
126 126 """
127 127
128 128 value = value.replace('(', '')
129 129 value = value.replace(')', '')
130 130
131 131 value = value.replace('[', '')
132 132 value = value.replace(']', '')
133 133
134 134 strList = value.split(',')
135 135 intList = [int(item) for item in strList]
136 136 pairList = []
137 137 for i in range(len(intList)/2):
138 138 pairList.append((intList[i*2], intList[i*2 + 1]))
139 139
140 140 self.__formated_value = pairList
141 141
142 142 return self.__formated_value
143 143
144 144 if format == 'multilist':
145 145 """
146 146 Example:
147 147 value = (0,1,2),(3,4,5)
148 148 """
149 149 multiList = ast.literal_eval(value)
150 150
151 151 if type(multiList[0]) == int:
152 152 multiList = ast.literal_eval("(" + value + ")")
153 153
154 154 self.__formated_value = multiList
155 155
156 156 return self.__formated_value
157 157
158 158 if format == 'bool':
159 159 value = int(value)
160 160
161 161 if format == 'int':
162 162 value = float(value)
163 163
164 164 format_func = eval(format)
165 165
166 166 self.__formated_value = format_func(value)
167 167
168 168 return self.__formated_value
169 169
170 170 def updateId(self, new_id):
171 171
172 172 self.id = str(new_id)
173 173
174 174 def setup(self, id, name, value, format='str'):
175 175
176 176 self.id = str(id)
177 177 self.name = name
178 178 self.value = str(value)
179 179 self.format = str.lower(format)
180 180
181 181 try:
182 182 self.getValue()
183 183 except:
184 184 return 0
185 185
186 186 return 1
187 187
188 188 def update(self, name, value, format='str'):
189 189
190 190 self.name = name
191 191 self.value = str(value)
192 192 self.format = format
193 193
194 194 def makeXml(self, opElement):
195 195
196 196 parmElement = SubElement(opElement, self.ELEMENTNAME)
197 197 parmElement.set('id', str(self.id))
198 198 parmElement.set('name', self.name)
199 199 parmElement.set('value', self.value)
200 200 parmElement.set('format', self.format)
201 201
202 202 def readXml(self, parmElement):
203 203
204 204 self.id = parmElement.get('id')
205 205 self.name = parmElement.get('name')
206 206 self.value = parmElement.get('value')
207 207 self.format = str.lower(parmElement.get('format'))
208 208
209 209 #Compatible with old signal chain version
210 210 if self.format == 'int' and self.name == 'idfigure':
211 211 self.name = 'id'
212 212
213 213 def printattr(self):
214 214
215 215 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
216 216
217 217 class OperationConf():
218 218
219 219 id = None
220 220 name = None
221 221 priority = None
222 222 type = None
223 223
224 224 parmConfObjList = []
225 225
226 226 ELEMENTNAME = 'Operation'
227 227
228 228 def __init__(self):
229 229
230 230 self.id = '0'
231 231 self.name = None
232 232 self.priority = None
233 233 self.type = 'self'
234 234
235 235
236 236 def __getNewId(self):
237 237
238 238 return int(self.id)*10 + len(self.parmConfObjList) + 1
239 239
240 240 def updateId(self, new_id):
241 241
242 242 self.id = str(new_id)
243 243
244 244 n = 1
245 245 for parmObj in self.parmConfObjList:
246 246
247 247 idParm = str(int(new_id)*10 + n)
248 248 parmObj.updateId(idParm)
249 249
250 250 n += 1
251 251
252 252 def getElementName(self):
253 253
254 254 return self.ELEMENTNAME
255 255
256 256 def getParameterObjList(self):
257 257
258 258 return self.parmConfObjList
259 259
260 260 def getParameterObj(self, parameterName):
261 261
262 262 for parmConfObj in self.parmConfObjList:
263 263
264 264 if parmConfObj.name != parameterName:
265 265 continue
266 266
267 267 return parmConfObj
268 268
269 269 return None
270 270
271 271 def getParameterObjfromValue(self, parameterValue):
272 272
273 273 for parmConfObj in self.parmConfObjList:
274 274
275 275 if parmConfObj.getValue() != parameterValue:
276 276 continue
277 277
278 278 return parmConfObj.getValue()
279 279
280 280 return None
281 281
282 282 def getParameterValue(self, parameterName):
283 283
284 284 parameterObj = self.getParameterObj(parameterName)
285 285
286 286 # if not parameterObj:
287 287 # return None
288 288
289 289 value = parameterObj.getValue()
290 290
291 291 return value
292 292
293 293 def setup(self, id, name, priority, type):
294 294
295 295 self.id = str(id)
296 296 self.name = name
297 297 self.type = type
298 298 self.priority = priority
299 299
300 300 self.parmConfObjList = []
301 301
302 302 def removeParameters(self):
303 303
304 304 for obj in self.parmConfObjList:
305 305 del obj
306 306
307 307 self.parmConfObjList = []
308 308
309 309 def addParameter(self, name, value, format='str'):
310 310
311 311 id = self.__getNewId()
312 312
313 313 parmConfObj = ParameterConf()
314 314 if not parmConfObj.setup(id, name, value, format):
315 315 return None
316 316
317 317 self.parmConfObjList.append(parmConfObj)
318 318
319 319 return parmConfObj
320 320
321 321 def changeParameter(self, name, value, format='str'):
322 322
323 323 parmConfObj = self.getParameterObj(name)
324 324 parmConfObj.update(name, value, format)
325 325
326 326 return parmConfObj
327 327
328 def makeXml(self, upElement):
328 def makeXml(self, procUnitElement):
329 329
330 opElement = SubElement(upElement, self.ELEMENTNAME)
330 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
331 331 opElement.set('id', str(self.id))
332 332 opElement.set('name', self.name)
333 333 opElement.set('type', self.type)
334 334 opElement.set('priority', str(self.priority))
335 335
336 336 for parmConfObj in self.parmConfObjList:
337 337 parmConfObj.makeXml(opElement)
338 338
339 339 def readXml(self, opElement):
340 340
341 341 self.id = opElement.get('id')
342 342 self.name = opElement.get('name')
343 343 self.type = opElement.get('type')
344 344 self.priority = opElement.get('priority')
345 345
346 346 #Compatible with old signal chain version
347 347 #Use of 'run' method instead 'init'
348 348 if self.type == 'self' and self.name == 'init':
349 349 self.name = 'run'
350 350
351 351 self.parmConfObjList = []
352 352
353 353 parmElementList = opElement.getiterator(ParameterConf().getElementName())
354 354
355 355 for parmElement in parmElementList:
356 356 parmConfObj = ParameterConf()
357 357 parmConfObj.readXml(parmElement)
358 358
359 359 #Compatible with old signal chain version
360 360 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
361 361 if self.type != 'self' and self.name == 'Plot':
362 362 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
363 363 self.name = parmConfObj.value
364 364 continue
365 365
366 366 self.parmConfObjList.append(parmConfObj)
367 367
368 368 def printattr(self):
369 369
370 370 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
371 371 self.id,
372 372 self.name,
373 373 self.type,
374 374 self.priority)
375 375
376 376 for parmConfObj in self.parmConfObjList:
377 377 parmConfObj.printattr()
378 378
379 379 def createObject(self):
380 380
381 381 if self.type == 'self':
382 382 raise ValueError, "This operation type cannot be created"
383 383
384 384 if self.type == 'external' or self.type == 'other':
385 385 className = eval(self.name)
386 386 opObj = className()
387 387
388 388 return opObj
389 389
390 390 class ProcUnitConf():
391 391
392 392 id = None
393 393 name = None
394 394 datatype = None
395 395 inputId = None
396 396 parentId = None
397 397
398 398 opConfObjList = []
399 399
400 400 procUnitObj = None
401 401 opObjList = []
402 402
403 403 ELEMENTNAME = 'ProcUnit'
404 404
405 405 def __init__(self):
406 406
407 407 self.id = None
408 408 self.datatype = None
409 409 self.name = None
410 410 self.inputId = None
411 411
412 412 self.opConfObjList = []
413 413
414 414 self.procUnitObj = None
415 415 self.opObjDict = {}
416 416
417 417 def __getPriority(self):
418 418
419 419 return len(self.opConfObjList)+1
420 420
421 421 def __getNewId(self):
422 422
423 423 return int(self.id)*10 + len(self.opConfObjList) + 1
424 424
425 425 def getElementName(self):
426 426
427 427 return self.ELEMENTNAME
428 428
429 429 def getId(self):
430 430
431 431 return self.id
432 432
433 433 def updateId(self, new_id, parentId=parentId):
434 434
435 435
436 436 new_id = int(parentId)*10 + (int(self.id) % 10)
437 437 new_inputId = int(parentId)*10 + (int(self.inputId) % 10)
438 438
439 439 #If this proc unit has not inputs
440 440 if self.inputId == '0':
441 441 new_inputId = 0
442 442
443 443 n = 1
444 444 for opConfObj in self.opConfObjList:
445 445
446 446 idOp = str(int(new_id)*10 + n)
447 447 opConfObj.updateId(idOp)
448 448
449 449 n += 1
450 450
451 451 self.parentId = str(parentId)
452 452 self.id = str(new_id)
453 453 self.inputId = str(new_inputId)
454 454
455 455
456 456 def getInputId(self):
457 457
458 458 return self.inputId
459 459
460 460 def getOperationObjList(self):
461 461
462 462 return self.opConfObjList
463 463
464 464 def getOperationObj(self, name=None):
465 465
466 466 for opConfObj in self.opConfObjList:
467 467
468 468 if opConfObj.name != name:
469 469 continue
470 470
471 471 return opConfObj
472 472
473 473 return None
474 474
475 475 def getOpObjfromParamValue(self, value=None):
476 476
477 477 for opConfObj in self.opConfObjList:
478 478 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
479 479 continue
480 480 return opConfObj
481 481 return None
482 482
483 483 def getProcUnitObj(self):
484 484
485 485 return self.procUnitObj
486 486
487 487 def setup(self, id, name, datatype, inputId, parentId=None):
488 488
489 489 #Compatible with old signal chain version
490 490 if datatype==None and name==None:
491 491 raise ValueError, "datatype or name should be defined"
492 492
493 493 if name==None:
494 494 if 'Proc' in datatype:
495 495 name = datatype
496 496 else:
497 497 name = '%sProc' %(datatype)
498 498
499 499 if datatype==None:
500 500 datatype = name.replace('Proc','')
501 501
502 502 self.id = str(id)
503 503 self.name = name
504 504 self.datatype = datatype
505 505 self.inputId = inputId
506 506 self.parentId = parentId
507 507
508 508 self.opConfObjList = []
509 509
510 510 self.addOperation(name='run', optype='self')
511 511
512 512 def removeOperations(self):
513 513
514 514 for obj in self.opConfObjList:
515 515 del obj
516 516
517 517 self.opConfObjList = []
518 518 self.addOperation(name='run')
519 519
520 520 def addParameter(self, **kwargs):
521 521 '''
522 522 Add parameters to "run" operation
523 523 '''
524 524 opObj = self.opConfObjList[0]
525 525
526 526 opObj.addParameter(**kwargs)
527 527
528 528 return opObj
529 529
530 530 def addOperation(self, name, optype='self'):
531 531
532 532 id = self.__getNewId()
533 533 priority = self.__getPriority()
534 534
535 535 opConfObj = OperationConf()
536 536 opConfObj.setup(id, name=name, priority=priority, type=optype)
537 537
538 538 self.opConfObjList.append(opConfObj)
539 539
540 540 return opConfObj
541 541
542 def makeXml(self, procUnitElement):
542 def makeXml(self, projectElement):
543 543
544 upElement = SubElement(procUnitElement, self.ELEMENTNAME)
545 upElement.set('id', str(self.id))
546 upElement.set('name', self.name)
547 upElement.set('datatype', self.datatype)
548 upElement.set('inputId', str(self.inputId))
544 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
545 procUnitElement.set('id', str(self.id))
546 procUnitElement.set('name', self.name)
547 procUnitElement.set('datatype', self.datatype)
548 procUnitElement.set('inputId', str(self.inputId))
549 549
550 550 for opConfObj in self.opConfObjList:
551 opConfObj.makeXml(upElement)
551 opConfObj.makeXml(procUnitElement)
552 552
553 553 def readXml(self, upElement):
554 554
555 555 self.id = upElement.get('id')
556 556 self.name = upElement.get('name')
557 557 self.datatype = upElement.get('datatype')
558 558 self.inputId = upElement.get('inputId')
559 559
560 560 if self.ELEMENTNAME == "ReadUnit":
561 561 self.datatype = self.datatype.replace("Reader", "")
562 562
563 563 if self.ELEMENTNAME == "ProcUnit":
564 564 self.datatype = self.datatype.replace("Proc", "")
565 565
566 566 if self.inputId == 'None':
567 567 self.inputId = '0'
568 568
569 569 self.opConfObjList = []
570 570
571 571 opElementList = upElement.getiterator(OperationConf().getElementName())
572 572
573 573 for opElement in opElementList:
574 574 opConfObj = OperationConf()
575 575 opConfObj.readXml(opElement)
576 576 self.opConfObjList.append(opConfObj)
577 577
578 578 def printattr(self):
579 579
580 580 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
581 581 self.id,
582 582 self.name,
583 583 self.datatype,
584 584 self.inputId)
585 585
586 586 for opConfObj in self.opConfObjList:
587 587 opConfObj.printattr()
588 588
589 589 def createObjects(self):
590 590
591 591 className = eval(self.name)
592 592 procUnitObj = className()
593 593
594 594 for opConfObj in self.opConfObjList:
595 595
596 596 if opConfObj.type == 'self':
597 597 continue
598 598
599 599 opObj = opConfObj.createObject()
600 600
601 601 self.opObjDict[opConfObj.id] = opObj
602 602 procUnitObj.addOperation(opObj, opConfObj.id)
603 603
604 604 self.procUnitObj = procUnitObj
605 605
606 606 return procUnitObj
607 607
608 608 def run(self):
609 609
610 610 is_ok = False
611 611
612 612 for opConfObj in self.opConfObjList:
613 613
614 614 kwargs = {}
615 615 for parmConfObj in opConfObj.getParameterObjList():
616 616 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
617 617 continue
618 618
619 619 kwargs[parmConfObj.name] = parmConfObj.getValue()
620 620
621 621 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
622 622 sts = self.procUnitObj.call(opType = opConfObj.type,
623 623 opName = opConfObj.name,
624 624 opId = opConfObj.id,
625 625 **kwargs)
626 626 is_ok = is_ok or sts
627 627
628 628 return is_ok
629 629
630 630 def close(self):
631 631
632 632 for opConfObj in self.opConfObjList:
633 633 if opConfObj.type == 'self':
634 634 continue
635 635
636 636 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
637 637 opObj.close()
638 638
639 639 self.procUnitObj.close()
640 640
641 641 return
642 642
643 643 class ReadUnitConf(ProcUnitConf):
644 644
645 645 path = None
646 646 startDate = None
647 647 endDate = None
648 648 startTime = None
649 649 endTime = None
650 650
651 651 ELEMENTNAME = 'ReadUnit'
652 652
653 653 def __init__(self):
654 654
655 655 self.id = None
656 656 self.datatype = None
657 657 self.name = None
658 658 self.inputId = None
659 659
660 660 self.parentId = None
661 661
662 662 self.opConfObjList = []
663 663 self.opObjList = []
664 664
665 665 def getElementName(self):
666 666
667 667 return self.ELEMENTNAME
668 668
669 669 def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs):
670 670
671 671 #Compatible with old signal chain version
672 672 if datatype==None and name==None:
673 673 raise ValueError, "datatype or name should be defined"
674 674
675 675 if name==None:
676 676 if 'Reader' in datatype:
677 677 name = datatype
678 678 else:
679 679 name = '%sReader' %(datatype)
680 680
681 681 if datatype==None:
682 682 datatype = name.replace('Reader','')
683 683
684 684 self.id = id
685 685 self.name = name
686 686 self.datatype = datatype
687 687
688 688 self.path = path
689 689 self.startDate = startDate
690 690 self.endDate = endDate
691 691 self.startTime = startTime
692 692 self.endTime = endTime
693 693
694 694 self.inputId = '0'
695 695 self.parentId = parentId
696 696
697 697 self.addRunOperation(**kwargs)
698 698
699 699 def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs):
700 700
701 701 #Compatible with old signal chain version
702 702 if datatype==None and name==None:
703 703 raise ValueError, "datatype or name should be defined"
704 704
705 705 if name==None:
706 706 if 'Reader' in datatype:
707 707 name = datatype
708 708 else:
709 709 name = '%sReader' %(datatype)
710 710
711 711 if datatype==None:
712 712 datatype = name.replace('Reader','')
713 713
714 714 self.datatype = datatype
715 715 self.name = name
716 716 self.path = path
717 717 self.startDate = startDate
718 718 self.endDate = endDate
719 719 self.startTime = startTime
720 720 self.endTime = endTime
721 721
722 722 self.inputId = '0'
723 723 self.parentId = parentId
724 724
725 725 self.updateRunOperation(**kwargs)
726 726
727 727 def addRunOperation(self, **kwargs):
728 728
729 729 opObj = self.addOperation(name = 'run', optype = 'self')
730 730
731 731 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
732 732 opObj.addParameter(name='path' , value=self.path, format='str')
733 733 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
734 734 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
735 735 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
736 736 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
737 737
738 738 for key, value in kwargs.items():
739 739 opObj.addParameter(name=key, value=value, format=type(value).__name__)
740 740
741 741 return opObj
742 742
743 743 def updateRunOperation(self, **kwargs):
744 744
745 745 opObj = self.getOperationObj(name = 'run')
746 746 opObj.removeParameters()
747 747
748 748 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
749 749 opObj.addParameter(name='path' , value=self.path, format='str')
750 750 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
751 751 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
752 752 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
753 753 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
754 754
755 755 for key, value in kwargs.items():
756 756 opObj.addParameter(name=key, value=value, format=type(value).__name__)
757 757
758 758 return opObj
759 759
760 # def makeXml(self, projectElement):
761 #
762 # procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
763 # procUnitElement.set('id', str(self.id))
764 # procUnitElement.set('name', self.name)
765 # procUnitElement.set('datatype', self.datatype)
766 # procUnitElement.set('inputId', str(self.inputId))
767 #
768 # for opConfObj in self.opConfObjList:
769 # opConfObj.makeXml(procUnitElement)
770
771 def readXml(self, upElement):
772
773 self.id = upElement.get('id')
774 self.name = upElement.get('name')
775 self.datatype = upElement.get('datatype')
776 self.inputId = upElement.get('inputId')
777
778 if self.ELEMENTNAME == "ReadUnit":
779 self.datatype = self.datatype.replace("Reader", "")
780
781 if self.inputId == 'None':
782 self.inputId = '0'
783
784 self.opConfObjList = []
785
786 opElementList = upElement.getiterator(OperationConf().getElementName())
787
788 for opElement in opElementList:
789 opConfObj = OperationConf()
790 opConfObj.readXml(opElement)
791 self.opConfObjList.append(opConfObj)
792
793 if opConfObj.name == 'run':
794 self.path = opConfObj.getParameterValue('path')
795 self.startDate = opConfObj.getParameterValue('startDate')
796 self.endDate = opConfObj.getParameterValue('endDate')
797 self.startTime = opConfObj.getParameterValue('startTime')
798 self.endTime = opConfObj.getParameterValue('endTime')
799
760 800 class Project():
761 801
762 802 id = None
763 803 name = None
764 804 description = None
765 # readUnitConfObjList = None
805 filename = None
806
766 807 procUnitConfObjDict = None
767 808
768 809 ELEMENTNAME = 'Project'
769 810
770 811 def __init__(self):
771 812
772 813 self.id = None
773 814 self.name = None
774 815 self.description = None
775 816
776 817 self.procUnitConfObjDict = {}
777 818
778 819 def __getNewId(self):
779 820
780 821 id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1
781 822
782 823 return str(id)
783 824
784 825 def getElementName(self):
785 826
786 827 return self.ELEMENTNAME
787 828
788 829 def getId(self):
789 830
790 831 return self.id
791 832
792 833 def updateId(self, new_id):
793 834
794 835 self.id = str(new_id)
795 836
796 837 keyList = self.procUnitConfObjDict.keys()
797 838 keyList.sort()
798 839
799 840 n = 1
800 841 newProcUnitConfObjDict = {}
801 842
802 843 for procKey in keyList:
803 844
804 845 procUnitConfObj = self.procUnitConfObjDict[procKey]
805 846 idProcUnit = str(int(self.id)*10 + n)
806 847 procUnitConfObj.updateId(idProcUnit, parentId = self.id)
807 848
808 849 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
809 850 n += 1
810 851
811 852 self.procUnitConfObjDict = newProcUnitConfObjDict
812 853
813 854 def setup(self, id, name, description):
814 855
815 856 self.id = str(id)
816 857 self.name = name
817 858 self.description = description
818 859
819 860 def update(self, name, description):
820 861
821 862 self.name = name
822 863 self.description = description
823 864
824 865 def addReadUnit(self, datatype=None, name=None, **kwargs):
825 866
826 867 idReadUnit = self.__getNewId()
827 868
828 869 readUnitConfObj = ReadUnitConf()
829 870 readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs)
830 871
831 872 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
832 873
833 874 return readUnitConfObj
834 875
835 876 def addProcUnit(self, inputId='0', datatype=None, name=None):
836 877
837 878 idProcUnit = self.__getNewId()
838 879
839 880 procUnitConfObj = ProcUnitConf()
840 881 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id)
841 882
842 883 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
843 884
844 885 return procUnitConfObj
845 886
846 887 def removeProcUnit(self, id):
847 888
848 889 if id in self.procUnitConfObjDict.keys():
849 890 self.procUnitConfObjDict.pop(id)
850 891
851 892 def getReadUnitId(self):
852 893
853 894 readUnitConfObj = self.getReadUnitObj()
854 895
855 896 return readUnitConfObj.id
856 897
857 898 def getReadUnitObj(self):
858 899
859 900 for obj in self.procUnitConfObjDict.values():
860 901 if obj.getElementName() == "ReadUnit":
861 902 return obj
862 903
863 904 return None
864 905
865 906 def getProcUnitObj(self, id=None, name=None):
866 907
867 908 if id != None:
868 909 return self.procUnitConfObjDict[id]
869 910
870 911 if name != None:
871 912 return self.getProcUnitObjByName(name)
872 913
873 914 return None
874 915
875 916 def getProcUnitObjByName(self, name):
876 917
877 918 for obj in self.procUnitConfObjDict.values():
878 919 if obj.name == name:
879 920 return obj
880 921
881 922 return None
882 923
883 924 def procUnitItems(self):
884 925
885 926 return self.procUnitConfObjDict.items()
886 927
887 928 def makeXml(self):
888 929
889 930 projectElement = Element('Project')
890 931 projectElement.set('id', str(self.id))
891 932 projectElement.set('name', self.name)
892 933 projectElement.set('description', self.description)
893 934
894 935 for procUnitConfObj in self.procUnitConfObjDict.values():
895 936 procUnitConfObj.makeXml(projectElement)
896 937
897 938 self.projectElement = projectElement
898 939
899 940 def writeXml(self, filename):
900 941
901 self.makeXml()
942 if not os.access(os.path.dirname(filename), os.W_OK):
943 return 0
902 944
903 #print prettify(self.projectElement)
945 if os.path.isfile(filename) and not(os.access(filename, os.W_OK)):
946 return 0
947
948 self.makeXml()
904 949
905 950 ElementTree(self.projectElement).write(filename, method='xml')
951
952 self.filename = filename
953
954 return 1
906 955
907 956 def readXml(self, filename):
908 957
958 if not os.path.isfile(filename):
959 print "%s does not exist" %filename
960 return 0
961
909 962 self.projectElement = None
910 963 self.procUnitConfObjDict = {}
911 964
912 965 self.projectElement = ElementTree().parse(filename)
913 966
914 967 self.project = self.projectElement.tag
915 968
916 969 self.id = self.projectElement.get('id')
917 970 self.name = self.projectElement.get('name')
918 971 self.description = self.projectElement.get('description')
919 972
920 973 readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName())
921 974
922 975 for readUnitElement in readUnitElementList:
923 976 readUnitConfObj = ReadUnitConf()
924 977 readUnitConfObj.readXml(readUnitElement)
925 978
926 979 if readUnitConfObj.parentId == None:
927 980 readUnitConfObj.parentId = self.id
928 981
929 982 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
930 983
931 984 procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName())
932 985
933 986 for procUnitElement in procUnitElementList:
934 987 procUnitConfObj = ProcUnitConf()
935 988 procUnitConfObj.readXml(procUnitElement)
936 989
937 990 if procUnitConfObj.parentId == None:
938 991 procUnitConfObj.parentId = self.id
939 992
940 993 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
941
994
995 return 1
996
942 997 def printattr(self):
943 998
944 999 print "Project[%s]: name = %s, description = %s" %(self.id,
945 1000 self.name,
946 1001 self.description)
947 1002
948 1003 for procUnitConfObj in self.procUnitConfObjDict.values():
949 1004 procUnitConfObj.printattr()
950 1005
951 1006 def createObjects(self):
952 1007
953 1008 for procUnitConfObj in self.procUnitConfObjDict.values():
954 1009 procUnitConfObj.createObjects()
955 1010
956 1011 def __connect(self, objIN, thisObj):
957 1012
958 1013 thisObj.setInput(objIN.getOutputObj())
959 1014
960 1015 def connectObjects(self):
961 1016
962 1017 for thisPUConfObj in self.procUnitConfObjDict.values():
963 1018
964 1019 inputId = thisPUConfObj.getInputId()
965 1020
966 1021 if int(inputId) == 0:
967 1022 continue
968 1023
969 1024 #Get input object
970 1025 puConfINObj = self.procUnitConfObjDict[inputId]
971 1026 puObjIN = puConfINObj.getProcUnitObj()
972 1027
973 1028 #Get current object
974 1029 thisPUObj = thisPUConfObj.getProcUnitObj()
975 1030
976 1031 self.__connect(puObjIN, thisPUObj)
977 1032
978 1033 def isPaused(self):
979 1034 return 0
980 1035
981 1036 def isStopped(self):
982 1037 return 0
983 1038
984 1039 def runController(self):
985 1040 """
986 1041 returns 0 when this process has been stopped, 1 otherwise
987 1042 """
988 1043
989 1044 if self.isPaused():
990 1045 print "Process suspended"
991 1046
992 1047 while True:
993 1048 sleep(0.1)
994 1049
995 1050 if not self.isPaused():
996 1051 break
997 1052
998 1053 if self.isStopped():
999 1054 break
1000 1055
1001 1056 print "Process reinitialized"
1002 1057
1003 1058 if self.isStopped():
1004 1059 print "Process stopped"
1005 1060 return 0
1006 1061
1007 1062 return 1
1008 1063
1009 1064 def run(self):
1010 1065
1011 1066 print
1012 1067 print "*"*60
1013 print " Starting SIGNAL CHAIN PROCESSING "
1068 print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__
1014 1069 print "*"*60
1015 1070 print
1016 1071
1017 1072 keyList = self.procUnitConfObjDict.keys()
1018 1073 keyList.sort()
1019 1074
1020 1075 while(True):
1021 1076
1022 1077 is_ok = False
1023 1078
1024 1079 for procKey in keyList:
1025 1080 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
1026 1081
1027 1082 procUnitConfObj = self.procUnitConfObjDict[procKey]
1028 1083
1029 message = ""
1030 1084 try:
1031 1085 sts = procUnitConfObj.run()
1032 1086 is_ok = is_ok or sts
1033 1087 except:
1034 print "***** Error running %s *****" %procUnitConfObj.name
1035 sleep(1)
1088 print "***** Error occurred in %s *****" %(procUnitConfObj.name)
1089
1090 sleep(0.5)
1036 1091
1037 1092 err = traceback.format_exception(sys.exc_info()[0],
1038 1093 sys.exc_info()[1],
1039 1094 sys.exc_info()[2])
1040 1095
1041 for thisLine in err:
1042 message += thisLine
1096 import socket
1097
1098 subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name)
1099
1100 subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name)
1101 subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname())
1102 subtitle += "Working directory: %s\n" %os.path.abspath("./")
1103 subtitle += "Configuration file: %s\n" %self.filename
1104
1105 readUnitConfObj = self.getReadUnitObj()
1106 if readUnitConfObj:
1107 subtitle += "Data path: %s\n" %readUnitConfObj.path
1108 subtitle += "Data type: %s\n" %readUnitConfObj.datatype
1109 subtitle += "Start date: %s\n" %readUnitConfObj.startDate
1110 subtitle += "End date: %s\n" %readUnitConfObj.endDate
1111 subtitle += "Start time: %s\n" %readUnitConfObj.startTime
1112 subtitle += "End time: %s\n" %readUnitConfObj.endTime
1113
1114 message = "".join(err)
1043 1115
1044 1116 sys.stderr.write(message)
1045 # print "*"*60
1046 # print message
1047 # print "*"*60
1117
1118 adminObj = schainpy.admin.SchainNotify()
1119 adminObj.sendAlert(message=message,
1120 subject=subject,
1121 subtitle=subtitle,
1122 filename=self.filename)
1048 1123
1049 # self.sendReport(message)
1050 sleep(0.1)
1051 1124 is_ok = False
1052 1125
1053 1126 break
1054 1127
1055 1128 #If every process unit finished so end process
1056 1129 if not(is_ok):
1057 1130 print "Every process unit have finished"
1058 1131 break
1059 1132
1060 1133 if not self.runController():
1061 1134 break
1062 1135
1063 1136 #Closing every process
1064 1137 for procKey in keyList:
1065 1138 procUnitConfObj = self.procUnitConfObjDict[procKey]
1066 1139 procUnitConfObj.close()
1067 1140
1068 1141 print "Process finished"
1069 1142
1070 1143 def start(self, filename):
1071 1144
1072 1145 self.writeXml(filename)
1073 1146 self.readXml(filename)
1074 1147
1075 1148 self.createObjects()
1076 1149 self.connectObjects()
1077 1150 self.run()
1078
1079 def sendReport(self, message, subject="Error occurred in Signal Chain", email=SCHAIN_MAIL):
1080
1081 import smtplib
1082
1083 print subject
1084 print "Sending report to %s ..." %email
1085
1086 message = 'From: (Python Signal Chain API) ' + email + '\n' + \
1087 'To: ' + email + '\n' + \
1088 'Subject: ' + str(subject) + '\n' + \
1089 'Content-type: text/html\n\n' + message
1090
1091 server = smtplib.SMTP(EMAIL_SERVER)
1092 server.sendmail(email.split(',')[0],
1093 email.split(','), message)
1094 server.quit()
1095 1151
1096 1152 if __name__ == '__main__':
1097 1153
1098 1154 desc = "Segundo Test"
1099 1155 filename = "schain.xml"
1100 1156
1101 1157 controllerObj = Project()
1102 1158
1103 1159 controllerObj.setup(id = '191', name='test01', description=desc)
1104 1160
1105 1161 readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage',
1106 1162 path='data/rawdata/',
1107 1163 startDate='2011/01/01',
1108 1164 endDate='2012/12/31',
1109 1165 startTime='00:00:00',
1110 1166 endTime='23:59:59',
1111 1167 online=1,
1112 1168 walk=1)
1113 1169
1114 1170 procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId())
1115 1171
1116 1172 opObj10 = procUnitConfObj0.addOperation(name='selectChannels')
1117 1173 opObj10.addParameter(name='channelList', value='3,4,5', format='intlist')
1118 1174
1119 1175 opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
1120 1176 opObj10.addParameter(name='minHei', value='90', format='float')
1121 1177 opObj10.addParameter(name='maxHei', value='180', format='float')
1122 1178
1123 1179 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external')
1124 1180 opObj12.addParameter(name='n', value='10', format='int')
1125 1181
1126 1182 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId())
1127 1183 procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int')
1128 1184 # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='')
1129 1185
1130 1186
1131 1187 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1132 1188 opObj11.addParameter(name='idfigure', value='1', format='int')
1133 1189 opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str')
1134 1190 opObj11.addParameter(name='zmin', value='40', format='int')
1135 1191 opObj11.addParameter(name='zmax', value='90', format='int')
1136 1192 opObj11.addParameter(name='showprofile', value='1', format='int')
1137 1193
1138 1194 print "Escribiendo el archivo XML"
1139 1195
1140 1196 controllerObj.writeXml(filename)
1141 1197
1142 1198 print "Leyendo el archivo XML"
1143 1199 controllerObj.readXml(filename)
1144 1200 #controllerObj.printattr()
1145 1201
1146 1202 controllerObj.createObjects()
1147 1203 controllerObj.connectObjects()
1148 1204 controllerObj.run()
1149 1205
1150 1206 No newline at end of file
@@ -1,141 +1,141
1 1 import threading
2 2
3 from PyQt4 import QtCore
4 from PyQt4.QtCore import SIGNAL
5
6 3 from schainpy.controller import Project
7 4
8 5 class ControllerThread(threading.Thread, Project):
9 6
10 7 def __init__(self, filename):
11 8
12 9 threading.Thread.__init__(self)
13 10 Project.__init__(self)
14 11
15 12 self.setDaemon(True)
16 13
17 14 self.filename = filename
18 15
19 16 self.lock = threading.Lock()
20 17 self.control = {'stop':False, 'pause':False}
21 18
22 19 def __del__(self):
23 20
24 21 self.control['stop'] = True
25 22
26 23 def stop(self):
27 24
28 25 self.lock.acquire()
29 26
30 27 self.control['stop'] = True
31 28
32 29 self.lock.release()
33 30
34 31 def pause(self):
35 32
36 33 self.lock.acquire()
37 34
38 35 self.control['pause'] = not(self.control['pause'])
39 36 paused = self.control['pause']
40 37
41 38 self.lock.release()
42 39
43 40 return paused
44 41
45 42 def isPaused(self):
46 43
47 44 self.lock.acquire()
48 45 paused = self.control['pause']
49 46 self.lock.release()
50 47
51 48 return paused
52 49
53 50 def isStopped(self):
54 51
55 52 self.lock.acquire()
56 53 stopped = self.control['stop']
57 54 self.lock.release()
58 55
59 56 return stopped
60 57
61 58 def run(self):
62 59 self.control['stop'] = False
63 60 self.control['pause'] = False
64 61
65 62 self.readXml(self.filename)
66 63 self.createObjects()
67 64 self.connectObjects()
68 65 Project.run(self)
69 66
70 67 def isRunning(self):
71 68
72 69 return self.is_alive()
73 70
74 71 def isFinished(self):
75 72
76 73 return not self.is_alive()
77
78 class ControllerQThread(QtCore.QThread, Project):
79
80 def __init__(self, filename):
81
82 QtCore.QThread.__init__(self)
83 Project.__init__(self)
84
85 self.filename = filename
86
87 self.lock = threading.Lock()
88 self.control = {'stop':False, 'pause':False}
89
90 def __del__(self):
91
92 self.control['stop'] = True
93 self.wait()
94
95 def stop(self):
96
97 self.lock.acquire()
98
99 self.control['stop'] = True
100
101 self.lock.release()
102
103 def pause(self):
104
105 self.lock.acquire()
106
107 self.control['pause'] = not(self.control['pause'])
108 paused = self.control['pause']
109
110 self.lock.release()
111
112 return paused
113
114 def isPaused(self):
115
116 self.lock.acquire()
117 paused = self.control['pause']
118 self.lock.release()
119
120 return paused
121
122 def isStopped(self):
123
124 self.lock.acquire()
125 stopped = self.control['stop']
126 self.lock.release()
127
128 return stopped
129
130 def run(self):
131
132 self.control['stop'] = False
133 self.control['pause'] = False
134
135 self.readXml(self.filename)
136 self.createObjects()
137 self.connectObjects()
138 self.emit( SIGNAL( "jobStarted( PyQt_PyObject )" ), 1)
139 Project.run(self)
140 self.emit( SIGNAL( "jobFinished( PyQt_PyObject )" ), 1)
141 No newline at end of file
74
75 # from PyQt4 import QtCore
76 # from PyQt4.QtCore import SIGNAL
77 #
78 # class ControllerQThread(QtCore.QThread, Project):
79 #
80 # def __init__(self, filename):
81 #
82 # QtCore.QThread.__init__(self)
83 # Project.__init__(self)
84 #
85 # self.filename = filename
86 #
87 # self.lock = threading.Lock()
88 # self.control = {'stop':False, 'pause':False}
89 #
90 # def __del__(self):
91 #
92 # self.control['stop'] = True
93 # self.wait()
94 #
95 # def stop(self):
96 #
97 # self.lock.acquire()
98 #
99 # self.control['stop'] = True
100 #
101 # self.lock.release()
102 #
103 # def pause(self):
104 #
105 # self.lock.acquire()
106 #
107 # self.control['pause'] = not(self.control['pause'])
108 # paused = self.control['pause']
109 #
110 # self.lock.release()
111 #
112 # return paused
113 #
114 # def isPaused(self):
115 #
116 # self.lock.acquire()
117 # paused = self.control['pause']
118 # self.lock.release()
119 #
120 # return paused
121 #
122 # def isStopped(self):
123 #
124 # self.lock.acquire()
125 # stopped = self.control['stop']
126 # self.lock.release()
127 #
128 # return stopped
129 #
130 # def run(self):
131 #
132 # self.control['stop'] = False
133 # self.control['pause'] = False
134 #
135 # self.readXml(self.filename)
136 # self.createObjects()
137 # self.connectObjects()
138 # self.emit( SIGNAL( "jobStarted( PyQt_PyObject )" ), 1)
139 # Project.run(self)
140 # self.emit( SIGNAL( "jobFinished( PyQt_PyObject )" ), 1)
141 # No newline at end of file
@@ -1,39 +1,41
1 1 '''
2 2 Created on Jul 16, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6
7 7 from schainpy import __version__
8 8 from setuptools import setup, Extension
9 9
10 10 setup(name="schainpy",
11 11 version=__version__,
12 12 description="Python tools to read, write and process Jicamarca data",
13 13 author="Miguel Urco",
14 14 author_email="miguel.urco@jro.igp.gob.pe",
15 15 url="http://jro.igp.gob.pe",
16 16 packages = {'schainpy',
17 17 'schainpy.model',
18 18 'schainpy.model.data',
19 19 'schainpy.model.graphics',
20 20 'schainpy.model.io',
21 21 'schainpy.model.proc',
22 22 'schainpy.model.utils',
23 23 'schainpy.gui',
24 24 'schainpy.gui.figures',
25 25 'schainpy.gui.viewcontroller',
26 26 'schainpy.gui.viewer',
27 27 'schainpy.gui.viewer.windows'},
28 28 py_modules=['schainpy.serializer.DataTranslate',
29 29 'schainpy.serializer.JROSerializer'],
30 package_data={'schainpy.gui.figures': ['*.png','*.jpg']},
30 package_data={'schainpy': ['*.cfg'],
31 'schainpy.gui.figures': ['*.png','*.jpg']
32 },
31 33 include_package_data=True,
32 34 scripts =['schainpy/gui/schainGUI'],
33 35 install_requires=["numpy >= 1.6.0",
34 36 "scipy >= 0.9.0",
35 37 "h5py >= 2.0.1",
36 38 "wxpython >= 2.8",
37 39 "matplotlib >= 1.0.0"
38 40 ],
39 41 ) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now