def TestOk(self): ''' Here everything should go fine. Parameters: None Returns: None Raises: ACSErrTypeCommonImpl.CouldntAccessComponentExImpl ''' self.logger.logTrace("ClientErrorComponent.TestOk") if self.foo == None: raise ACSErrTypeCommonImpl.CouldntAccessComponentExImpl() try: self.foo.displayMessage() except CORBA.SystemException, ex: # Map...... acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc( "badMethod has thrown a CORBA exception") displayMessageEx.log(self.logger)
def __narrowComponentReference(self, corba_obj, comp_class): ''' Helper method which narrows the component reference to the correct type for the developer. Parameters: corba_obj - reference to the component comp_class - CORBA stub class for the component Returns: the narrowed component reference or None Raises: Nothing ''' #try to narrow the object...if this fails for any reason, just return #the unnarrowed object try: narrowed_ref = corba_obj._narrow(comp_class) return narrowed_ref except Exception, e: if corba_obj is None: self.__logger.logCritical( "Unable to obtain reference to component: " + str(e)) else: self.__logger.logWarning("Unable to narrow component: " + str(e)) acsPrintExcDebug() return corba_obj
def __narrowComponentReference(self, corba_obj, comp_class): ''' Helper method which narrows the component reference to the correct type for the developer. Parameters: corba_obj - reference to the component comp_class - CORBA stub class for the component Returns: the narrowed component reference or None Raises: Nothing ''' #try to narrow the object...if this fails for any reason, just return #the unnarrowed object try: narrowed_ref = corba_obj._narrow(comp_class) return narrowed_ref except Exception, e: if corba_obj is None: self.__logger.logCritical("Unable to obtain reference to component: " + str(e)) else: self.__logger.logWarning("Unable to narrow component: " + str(e)) acsPrintExcDebug() return corba_obj
def TestReceiveRemoteCompletion(self): ''' Example 2: Calls a method that returns a completion If the completion contains an error, then: Catches the exception, prints it locally sends it to the logging system Parameters: None Returns: Nothing Raises: ACSErrTypeCommonImpl.CouldntAccessComponentExImpl ''' self.logger.logTrace( "ClientErrorComponent.TestReceiveRemoteCompletion") if self.foo == None: raise ACSErrTypeCommonImpl.CouldntAccessComponentExImpl() try: # OK Completion self.logger.logInfo( "Example 2a: Calls a method that returns an OK completion.") comp = self.foo.completionFromException(0) addComplHelperMethods(comp) if comp.isErrorFree() == 1: self.logger.logInfo("Completion Ok, without error trace") else: self.logger.logInfo("Completion with error trace (UNEXPECTED)") comp.log(self.logger) # ERROR completion with an error trace inside. self.logger.logInfo( "Example 2b: Calls a method that returns an Error completion, of depth 3." ) comp2 = self.foo.completionFromException(3) addComplHelperMethods(comp2) if comp2.isErrorFree() == 1: self.logger.logInfo( "Completion Ok, without error trace (UNEXPECTED)") else: self.logger.logInfo("Completion with error trace") comp2.log(self.logger) except CORBA.SystemException, ex: # Map...... acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc( "completionFromException has thrown an UNEXPECTED CORBA exception" ) displayMessageEx.log(self.logger)
def TestReceiveRemoteCompletion(self): ''' Example 2: Calls a method that returns a completion If the completion contains an error, then: Catches the exception, prints it locally sends it to the logging system Parameters: None Returns: Nothing Raises: ACSErrTypeCommonImpl.CouldntAccessComponentExImpl ''' self.logger.logTrace("ClientErrorComponent.TestReceiveRemoteCompletion") if self.foo == None: raise ACSErrTypeCommonImpl.CouldntAccessComponentExImpl() try: # OK Completion self.logger.logInfo("Example 2a: Calls a method that returns an OK completion.") comp = self.foo.completionFromException(0) addComplHelperMethods(comp) if comp.isErrorFree() == 1: self.logger.logInfo("Completion Ok, without error trace") else: self.logger.logInfo("Completion with error trace (UNEXPECTED)") comp.log(self.logger) # ERROR completion with an error trace inside. self.logger.logInfo("Example 2b: Calls a method that returns an Error completion, of depth 3.") comp2 = self.foo.completionFromException(3) addComplHelperMethods(comp2) if comp2.isErrorFree() == 1: self.logger.logInfo("Completion Ok, without error trace (UNEXPECTED)") else: self.logger.logInfo("Completion with error trace") comp2.log(self.logger) except CORBA.SystemException, ex: # Map...... acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc("completionFromException has thrown an UNEXPECTED CORBA exception") displayMessageEx.log(self.logger)
def eventFunctionHelper(self, event, ifr_id): ''' Returns an event instance or None based off the contents of an _almaEvent or _almaEventReponse XML element (DOM). ''' #here comes the fun part...it might be necessary to dynamically #create the object now! #get the code to be executed yielding a return value try: #if the following line of code throws an exception, #it's not really a big deal. it just means that #no function was defined within the XML element to #generate the event instance value = event.getValue().rstrip().lstrip().split('\n') #this next block is wrapped in a separate try/except #because it's possible that the end-user has problems #in their function definition. try: _locals = {} #attach all imports to the function definition value = getCompLocalNSList(self.comp_name) + value #make the code list a function in the _locals namespace value = listToCodeObj(value, _locals) #create the function exec value in globals(), _locals #execute the function as well to get the event instance exec "joe = stringFunction([])" in globals(), _locals event_instance = _locals['joe'] except Exception, e: #the function definition given by the end-user was bad! #warn them and schedule dynamic events instead acsPrintExcDebug() self.logger.logCritical( "Something was wrong within the function definition for the '" + ifr_id + "' event type.") self.logger.logInfo( "Will try dynamically creating the event instead.") #just rethrow e so the next block catches it raise e except: event_instance = None return event_instance
def eventFunctionHelper(self, event, ifr_id): ''' Returns an event instance or None based off the contents of an _almaEvent or _almaEventReponse XML element (DOM). ''' #here comes the fun part...it might be necessary to dynamically #create the object now! #get the code to be executed yielding a return value try: #if the following line of code throws an exception, #it's not really a big deal. it just means that #no function was defined within the XML element to #generate the event instance value = event.getValue().rstrip().lstrip().split('\n') #this next block is wrapped in a separate try/except #because it's possible that the end-user has problems #in their function definition. try: _locals = {} #attach all imports to the function definition value = getCompLocalNSList(self.comp_name) + value #make the code list a function in the _locals namespace value = listToCodeObj(value, _locals) #create the function exec value in globals(), _locals #execute the function as well to get the event instance exec "joe = stringFunction([])" in globals(), _locals event_instance = _locals['joe'] except Exception, e: #the function definition given by the end-user was bad! #warn them and schedule dynamic events instead acsPrintExcDebug() self.logger.logCritical("Something was wrong within the function definition for the '" + ifr_id + "' event type.") self.logger.logInfo("Will try dynamically creating the event instead.") #just rethrow e so the next block catches it raise e except: event_instance = None return event_instance
def TestOk(self): ''' Here everything should go fine. Parameters: None Returns: None Raises: ACSErrTypeCommonImpl.CouldntAccessComponentExImpl ''' self.logger.logTrace("ClientErrorComponent.TestOk") if self.foo == None: raise ACSErrTypeCommonImpl.CouldntAccessComponentExImpl() try: self.foo.displayMessage() except CORBA.SystemException, ex: # Map...... acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc("badMethod has thrown a CORBA exception") displayMessageEx.log(self.logger)
if self.foo == None: raise ACSErrTypeCommonImpl.CouldntAccessComponentExImpl() try: self.foo.displayMessage() except CORBA.SystemException, ex: # Map...... acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc( "badMethod has thrown a CORBA exception") displayMessageEx.log(self.logger) except: acsPrintExcDebug() displayMessageEx = ACSErrTypeCommonImpl.GenericErrorExImpl() displayMessageEx.setErrorDesc( "badMethod has thrown an UNEXPECTED exception") displayMessageEx.log(self.logger) def TestReceiveRemoteException(self): ''' Example 1: Calls a method that throws an exception with a stack trace: Catches the exception, Adds context information Sends it to the logging system Parameters: None
def __importComponentStubs(self, comp_name=None, comp_type=None): ''' Helper method tries to automatically import the CORBA stubs for a developer. In the event that this fails, a critical message is logged. Parameters: comp_name - name of the component comp_type - IFR type of the component Notes: at least one of the parameters above has be a string Returns: the component class CORBA stub Raises: CannotGetComponentExImpl if type is unknown or import fails ''' t_idl_type = comp_type if (comp_name is not None) and (comp_type is None): #Get a list of all components components = self.availableComponents() #search each Component for component in components: #for the one that has the given name if component.name == comp_name: #get that component's IR location #e.g., "IDL:alma/PS/PowerSupply:1.0" t_idl_type = component.type break if t_idl_type is None: #getting this far means the component was not #found ex = CannotGetComponentExImpl() ex.setReason("Component type unavailable!") self.__logger.logWarning("Unable to import '" + str(comp_name) + "' component's module: " + ex.getReason()) raise ex try: #extract the proper Python module from the type string. #("alma", "PS", "PowerSupply") temp = t_idl_type.split(':')[1].split('/') #component's class name comp_class = temp.pop() #"PowerSupply" #components module name comp_module = temp.pop() #"PS" #Now import the real module comp_module = __import__(comp_module, globals(), locals(), [comp_class]) #import it #get class reference comp_class = comp_module.__dict__.get(comp_class) except Exception, e: #for some reason or another, the module could not be imported. #this is not a total failure so it's just logged. self.__logger.logWarning("Unable to import '" + str(comp_name) + "' component's module: " + str(e)) acsPrintExcDebug() raise CannotGetComponentExImpl(e)
def __importComponentStubs(self, comp_name=None, comp_type=None): ''' Helper method tries to automatically import the CORBA stubs for a developer. In the event that this fails, a critical message is logged. Parameters: comp_name - name of the component comp_type - IFR type of the component Notes: at least one of the parameters above has be a string Returns: the component class CORBA stub Raises: CannotGetComponentExImpl if type is unknown or import fails ''' t_idl_type = comp_type if (comp_name is not None) and (comp_type is None): #Get a list of all components components = self.availableComponents(comp_name) #search each Component for component in components: #for the one that has the given name if component.name == comp_name: #get that component's IR location #e.g., "IDL:alma/PS/PowerSupply:1.0" t_idl_type = component.type break if t_idl_type is None: #getting this far means the component was not #found ex = CannotGetComponentExImpl() ex.setReason("Component type unavailable!") self.__logger.logWarning("Unable to import '" + str(comp_name) + "' component's module: " + ex.getReason()) raise ex try: #extract the proper Python module from the type string. #("alma", "PS", "PowerSupply") temp = t_idl_type.split(':')[1].split('/') #component's class name comp_class = temp.pop() #"PowerSupply" #components module name comp_module = temp.pop() #"PS" #Now import the real module comp_module = __import__(comp_module, globals(), locals(), [comp_class]) #import it #get class reference comp_class = comp_module.__dict__.get(comp_class) except Exception, e: #for some reason or another, the module could not be imported. #this is not a total failure so it's just logged. self.__logger.logWarning("Unable to import '" + str(comp_name) + "' component's module: " + str(e)) acsPrintExcDebug() raise CannotGetComponentExImpl(e)
if depth >= 1: try: # We decrement the depth, because we are going to add one # error here in any case. self.__errorTrace(depth - 1) except ACSErrTypeCommonImpl.GenericErrorExImpl, ex: ex2 = ACSErrTypeCommonImpl.GenericErrorExImpl(exception=ex) ex2.addData("ErrorDesc", "Generated multi level exception") raise ex2 except: #never expect this code to be executed but just in case #there was some unknown problem generating the ACS-based #exception acsPrintExcDebug() ex2 = ACSErrTypeCommonImpl.GenericErrorExImpl() ex2.addData("ErrorDesc", "Got unexpected exception") raise ex2 #We should get here only if a depth<=1 was requested. ex = ACSErrTypeCommonImpl.GenericErrorExImpl() ex.addData("ErrorDesc", "An error trace with depth lower or equal to 1 was requested.") raise ex #------------------------------------------------------------------------------ def exceptionFromCompletion(self, depth): ''' Implementation of IDL method: ACSErr::Completion exceptionFromCompletion(in short depth); For details on what this method does, please see the IDL Doxygen