From fb4509382591a648777bfdb9da00d7a7d7e2bcfb Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Tue, 2 Feb 2010 15:37:52 +0000 Subject: [PATCH] Use module pal.logger for logs. Added a method to raise SALOME_Exception objects in case of error. --- src/GENERICSOLVER/GENERICSOLVER.py | 93 +++++++++++++++++++----------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/src/GENERICSOLVER/GENERICSOLVER.py b/src/GENERICSOLVER/GENERICSOLVER.py index 702e5cb..ff6512e 100644 --- a/src/GENERICSOLVER/GENERICSOLVER.py +++ b/src/GENERICSOLVER/GENERICSOLVER.py @@ -22,13 +22,20 @@ # $Id$ # -import traceback +import logging +import threading +import inspect import salome import GENERICSOLVER_ORB__POA import SALOME_ComponentPy import SALOME_DriverPy +import SALOME +from pal.logger import Logger +from pal import termcolor +logger = Logger("GENERICSOLVER", color = termcolor.BLUE_FG) +#logger.setLevel(logging.INFO) VARIABLE_ID = 1030 @@ -44,7 +51,7 @@ def GetDataFromCase( studyId, caseEntry ): for name in ("E", "F", "L", "I"): var = getSubSObjectByName( studyId, case, name ) if var == None: - print "GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name + logger.error("GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name) break theCase[ name ] = getValueOfVariable( builder, var ) return theCase @@ -97,13 +104,13 @@ def getValueOfVariable( builder, varobj ): return 0. def getSubSObjectByName( studyId, sobjFather, childName ): - print "GENERICSOLVER.getSubSObjectByName Looking for sobjet named", childName + logger.debug("GENERICSOLVER.getSubSObjectByName Looking for sobjet named " + childName) study = salome.myStudyManager.GetStudyByID( studyId ) iter = study.NewChildIterator( sobjFather ) #builder = study.NewBuilder() while iter.More(): sobj = iter.Value() - print "GENERICSOLVER.getSubSObjectByName Got sobjet named", sobj.GetName() + logger.debug("GENERICSOLVER.getSubSObjectByName Got sobjet named " + sobj.GetName()) if sobj.GetName() == childName: return sobj iter.Next() @@ -115,6 +122,9 @@ def getSubSObjectByName( studyId, sobjFather, childName ): class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i, SALOME_DriverPy.SALOME_DriverPy_i): + + lock = threading.Lock() + """ Pour etre un composant SALOME cette classe Python doit avoir le nom du composant et heriter de la @@ -124,7 +134,7 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, """ def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ): - print "GENERICSOLVER.__init__: ", containerName, ';', instanceName + logger.info("GENERICSOLVER.__init__: " + containerName + ' ; ' + instanceName) SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, contID, containerName, instanceName, interfaceName, 0) SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName) @@ -142,42 +152,50 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, # are free to call the legacy interface (see below). ###################################################################### + def _raiseSalomeError(self): + message = "Error in %s.%s" % (self.__class__.__name__, inspect.stack()[1][3]) + logger.exception(message) + message += ". See logs of container %s for more details." % self._containerName + exc = SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, message, + inspect.stack()[1][1], inspect.stack()[1][2]) + raise SALOME.SALOME_Exception(exc) def Init ( self, studyId, caseEntry, wrapperDescription ): """ This method is an implementation for the GENERICSOLVER interface. It sets the component with some deterministic parametrization. """ - print "GENERICSOLVER.Init : enter" - print "GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription ) - self.wrapperDescription = wrapperDescription - salome.salome_init() - try: + logger.debug("GENERICSOLVER.Init : enter") + logger.debug("GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription )) + self.wrapperDescription = wrapperDescription + GENERICSOLVER.lock.acquire() + salome.salome_init() + GENERICSOLVER.lock.release() + self.case = GetDataFromCase( studyId, caseEntry ) + if self.case is None: + return 1 + logger.debug("GENERICSOLVER.Init : exit") + return 0 except: - traceback.print_exc() - if self.case is None: - return 1 - print "GENERICSOLVER.Init : exit" - return 0 + self._raiseSalomeError() def Exec ( self , inPoint ): """ This method is an implementation for the GENERICSOLVER interface. It runs the component with some new parameters compared with the deterministic ones. """ - if self.case is None : - print "GENERICSOLVER.Exec : Init not run" - return 1, None - - print "GENERICSOLVER.Exec (1): inPoint = ", inPoint - try: + if self.case is None : + logger.error("GENERICSOLVER.Exec : Init not run") + return 1, None + + logger.debug("GENERICSOLVER.Exec (1): inPoint = %s" % inPoint) case = dict( self.case ) if self.wrapperDescription != "": import sys - print "sys.path = ", sys.path + logger.debug("sys.path = %s" % sys.path) import openturns.wrapper wrapper = openturns.wrapper.WrapperFile.BuildWrapperFromStream( self.wrapperDescription ) data = wrapper.getWrapperData() @@ -186,29 +204,34 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, for idx in range( variableList.getSize() ): variable = variableList[ idx ] if variable.type_ == 0: - print "variable %s <-> index %d" % ( variable.id_, i ) + logger.debug("variable %s <-> index %d" % ( variable.id_, i )) case[ variable.id_ ] = inPoint[ i ] i += 1 - print "Case = ", case + logger.debug("Case = %s" % case) + logger.info("Evaluating case by component %s in container %s" % + (self._instanceName, self._containerName)) outPoint = self.BeamModel( **case ) - except: - traceback.print_exc() - return 1, None - - print "GENERICSOLVER.Exec (2): inPoint = ", inPoint - print "GENERICSOLVER.Exec (2): outPoint = ", outPoint - return 0, outPoint + logger.debug("GENERICSOLVER.Exec (2): inPoint = %s" % inPoint) + logger.debug("GENERICSOLVER.Exec (2): outPoint = %s" % outPoint) + return 0, outPoint + + except: + self._raiseSalomeError() def Finalize ( self ): """ This method is an implementation for the GENERICSOLVER interface. It cleans everything set so far. """ - print "GENERICSOLVER.Finalize : enter" - print "GENERICSOLVER.Finalize : exit" - return 0 + try: + logger.debug("GENERICSOLVER.Finalize : enter") + logger.debug("GENERICSOLVER.Finalize : exit") + return 0 + except: + self._raiseSalomeError() + ###################################################################### # This is the computation part of the GENERICSOLVER module, ie # the following method realizes what the solver is intended to do. @@ -222,6 +245,6 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection ) """ d = ( F * L*L*L ) / ( 3. * E * I ) - print "GENERICSOLVER.Exec (2): BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d) + logger.debug("GENERICSOLVER.Exec (2): BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d)) return (d,) -- 2.39.2