00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2004
00004 # by
00005 # The Board of Trustees of the
00006 # Leland Stanford Junior University.
00007 # All rights reserved.
00008 #
00009
00010 __facility__ = "Online"
00011 __abstract__ = "Application for testing handling of exceptions"
00012 __author__ = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__ = "August 20, 2004" # Created
00014 __version__ = "$Revision: 1.1 $"
00015 __release__ = "$Name: R04-12-00 $"
00016 __credits__ = "SLAC"
00017
00018 import LATTE.copyright_SLAC
00019
00020 from qt import qApp
00021
00022 import threading
00023 import logging as log
00024
00025 from LATTE.runcontrol.rcTransitions import rcTransitions
00026 from LATTE.runcontrol.ArgumentImpl import ArgumentImpl
00027
00028 from support.SimpleGasuExample import *
00029 from support.MiniGLTExample import *
00030
00031
00032 # This class puts up a GUI for inputting a value of some sort. It is used by
00033 # the userApplication example to get the number of self triggers to take.
00034 class userArgument(ArgumentImpl):
00035 "GUI for getting the user to input some sort of value."
00036 def __init__(self, parent = None, name = None, modal = 0, fl = 0):
00037 ArgumentImpl.__init__(self, parent, name, modal, fl)
00038 self.__value = 0
00039
00040 def OKButtonClicked(self):
00041 self.__value = int(self.ArgumentList.text().latin1())
00042 self.close()
00043
00044 def CancelButtonClicked(self):
00045 self.__value = None
00046 self.close()
00047
00048 def getValue(self, caption):
00049 #self.setCaption(caption)
00050 self.ArgLabel.setText(qApp.translate("Argument", caption, None))
00051 self.show()
00052 self.exec_loop()
00053 return self.__value
00054
00055 def getValueExc(self, caption):
00056 raise Exception, "Exception taken during a GUI operation"
00057
00058 class userArgText(object):
00059 "Text user interface for getting the user to input some sort of value"
00060 def __init__(self):
00061 pass
00062
00063 def getValue(self, caption):
00064 return int(raw_input("%s: " % (caption)))
00065
00066
00067 # Null application implementation.
00068 class userApplication(rcTransitions):
00069 "Implmentation class for Null application"
00070 def __init__(self, rc, userId, debug):
00071 rcTransitions.__init__(self, rc, userId, debug)
00072 self.__eventSem = threading.Semaphore(0)
00073 self.__cmdSynchSem = threading.Semaphore(0)
00074
00075 def getName(self):
00076 return __name__
00077
00078 def setup(self):
00079 log.debug("userApplication.setup()")
00080
00081 if self.rc is None:
00082 self.__arg = userArgText()
00083 else:
00084 self.__arg = self.rc.createGUI(userArgument, self.rc, self.getName(), 1)
00085
00086 caption = "Select when to take an exception:\n 0 = GUI\n 1 = Setup\n 2 = StartRun\n 3 = StopRun\n 4 = Stop\n 5 = Teardown\n 6 = Pause\n 7 = Resume\n 8 = CmdSynch\n 9 = Process"
00087 if self.rc is None:
00088 self.__state = self.__arg.getValue(caption)
00089 else:
00090 self.__state = self.rc.execGUImethod(self.__arg.getValue, caption)
00091
00092 if self.__state == 0:
00093 crud = self.rc.execGUImethod(self.__arg.getValueExc, "Exception test")
00094 return False # To reject the transition
00095
00096 if self.__state == 1:
00097 raise Exception, "Exception taken during SETUP transition"
00098
00099 # Two choices for the trigger, GEM and MiniGLT
00100 if self.lat.existsGEM():
00101 self.trigger( SimpleGasuExample() )
00102 else:
00103 self.trigger( MiniGLTExample() )
00104
00105 # A state transition can be rejected by not returning None
00106 return None
00107
00108 def startRun(self):
00109 log.debug("userApplication.startRun()")
00110
00111 if self.__state == 2:
00112 raise Exception, "Exception taken during STARTRUN transition"
00113
00114 self.__cmdSynchQuit = False
00115
00116 # A state transition can be rejected by not returning None
00117 return None
00118
00119 def stopRun(self):
00120 log.debug("userApplication.stopRun()")
00121
00122 self.__cmdSynchQuit = True
00123 self.__eventSem.release()
00124
00125 # Set the test completion status value
00126 self.setCompletionStatus(self.COMPL_STATUS_PASSED)
00127
00128 if self.__state == 3:
00129 raise Exception, "Exception taken during STOPRUN transition"
00130
00131 # The STOP_RUN transition can not be rejected
00132
00133 def stop(self):
00134 log.debug("userApplication.stop()")
00135
00136 if self.__state == 4:
00137 raise Exception, "Exception taken during STOP transition"
00138
00139 return self.stopRun()
00140
00141 def teardown(self):
00142 log.debug("userApplication.teardown()")
00143
00144 if self.__state == 5:
00145 raise Exception, "Exception taken during TEARDOWN transition"
00146
00147 def pause(self):
00148 log.debug("userApplication.pause()")
00149
00150 if self.__state == 6:
00151 raise Exception, "Exception taken during PAUSE transition"
00152
00153 def resume(self):
00154 log.debug("userApplication.resume()")
00155
00156 if self.__state == 7:
00157 raise Exception, "Exception taken during RESUME transition"
00158
00159 # Issue self trigger to make up for the one that was lost during PAUSE
00160 self.trigger().solicit()
00161
00162 def process(self, (status, buffer)):
00163 "Method called back for each data event taken"
00164
00165 if self.__state == 8: log.debug("userApplication.process()")
00166
00167 if self.__state == 9:
00168 raise Exception, "Exception taken during process()"
00169
00170 # Get next event triggered
00171 self.__eventSem.release()
00172
00173 def commandSynch(self):
00174 log.debug("userApplication.commandSynch()")
00175
00176 trigger = self.trigger()
00177 eventSem = self.__eventSem
00178
00179 # Drain the semaphore release count
00180 # Handles the case when the stop run release collided with a trigger release
00181 while eventSem.acquire(0): pass
00182
00183 # Wait for START_RUN to enable triggers
00184 trigger.waitForMaskEnable()
00185
00186 cnt = 0
00187 while not self.__cmdSynchQuit:
00188 if cnt == 5 and self.__state == 8:
00189 raise Exception, "Exception taken during commandSynch()"
00190 trigger.solicit() # Issue an internal trigger
00191 eventSem.acquire() # Wait for the event to be processed
00192 cnt += 1
00193
00194 # Indicate we're done
00195 self.sync()
00196
00197 def wait(self):
00198 self.__cmdSynchSem.acquire()
00199
00200 def sync(self):
00201 if __name__ == "__main__" or self.isRunFromSuite():
00202 self.__cmdSynchSem.release()
00203
00204
00205 # Standalone mode:
00206 if __name__ == "__main__":
00207 import os
00208 import logging as log
00209 log.basicConfig()
00210 log.getLogger("").setLevel(log.DEBUG)
00211 ua = userApplication(None, 321, 0)
00212 prefs = {'datasave': 1, 'datadir':os.path.join(os.environ['ONLINE_ROOT'],'temp')}
00213 ua.setPrefs(prefs)
00214 ua.rcSetup(os.path.join(os.environ['ONLINE_ROOT'], 'repos/simpleTemSchema.xml'))
00215 ua.rcStartRun()
00216 ua.wait()
00217 ua.rcStopRun()
00218 ua.rcTeardown()
00219
00220 # History:
00221 # $Log: excApp.py,v $
00222 # Revision 1.1 2004/08/23 21:04:24 claus
00223 # Test exception handling.
00224 #
00225 #