1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # SALOME Container : implementation of container and engine for Kernel
25 # File : SALOME_Container.py
26 # Author : Paul RASCLE, EDF
30 ## @package SALOME_Container
31 # \brief python implementation of container interface for Kernel
38 from omniORB import CORBA, PortableServer
40 import Engines, Engines__POA
41 from SALOME_NamingServicePy import *
42 from SALOME_ComponentPy import *
46 from SALOME_utilities import *
47 from Utils_Identity import getShortHostName
48 from salome_utils import verbose
49 from KernelBasis import VerbosityActivated,getSSLMode
51 #=============================================================================
53 #define an implementation of the container interface for embedding in Container implemented in C++
55 class SALOME_Container_i:
59 _naming_service = None
61 #-------------------------------------------------------------------------
63 def __init__(self ,containerName, containerIORStr, dftTimeIntervalInMs):
64 # Warning this part of code is called at the very first step of container launching
65 # so logging is not instanciate. So use verbose method to discrimine if a message should be printed or not
68 except AttributeError :
69 # for embedded python interpreter
70 # shouldn't be needed after python 3.8
71 # see https://bugs.python.org/issue32573
73 self._orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
74 self._poa = self._orb.resolve_initial_references("RootPOA")
75 self._containerName = containerName
76 self._logFileName = None
77 self._timeIntervalInMs = dftTimeIntervalInMs
80 self._container = self._orb.string_to_object(containerIORStr)
85 if self._logm is None:
87 self._logm = salome.logm
90 #-------------------------------------------------------------------------
92 def import_component(self, componentName):
95 logging.debug("try import ",componentName)
96 importlib.import_module(componentName)
97 logging.debug("import ",componentName," successful")
99 #can't import python module componentName
100 #try to find it in python path
102 _specs = importlib.util.find_spec(componentName)
103 _module = importlib.util.module_from_spec(_specs)
104 _specs.loader.exec_module(_module)
105 #module file found in path
106 ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
107 ret=ret+traceback.format_exc(10)
108 except ImportError as ee:
109 ret="ImplementationNotFound"
111 print("error when calling find_module")
112 ret="ImplementationNotFound"
114 ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
115 ret=ret+traceback.format_exc(10)
116 traceback.print_exc()
117 print("import ",componentName," not possible")
120 #-------------------------------------------------------------------------
122 def create_component_instance(self, componentName, instanceName):
126 component=importlib.import_module(componentName)
127 factory=getattr(component,componentName)
128 comp_i=factory(self._orb,
135 comp_o = comp_i._this()
136 comp_iors = self._orb.object_to_string(comp_o)
138 ret=traceback.format_exc(10)
139 traceback.print_exc()
140 return comp_iors, ret
143 def create_pynode(self,nodeName,code):
145 node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
146 id_o = self._poa.activate_object(node)
147 comp_o = self._poa.id_to_reference(id_o)
148 comp_iors = self._orb.object_to_string(comp_o)
151 exc_typ,exc_val,exc_fr=sys.exc_info()
152 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
155 def create_pyscriptnode(self,nodeName,code):
159 logscript = self._log.addScript(nodeName,code)
160 node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self, logscript)
161 id_o = self._poa.activate_object(node)
162 comp_o = self._poa.id_to_reference(id_o)
163 comp_iors = self._orb.object_to_string(comp_o)
166 exc_typ,exc_val,exc_fr=sys.exc_info()
167 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
168 print("".join(l)) ; sys.stdout.flush() # print error also in logs of remote container
171 def positionVerbosityOfLogger(self):
172 if VerbosityActivated():
174 salome_utils.positionVerbosityOfLoggerRegardingState()
176 def monitoringtimeresms(self):
177 return self._timeIntervalInMs
179 def setLogFileName(self, logFileName):
181 self._log = self.logm.declareContainer( self._containerName, logFileName )
183 def SetMonitoringtimeresms(self , value):
184 self._timeIntervalInMs = value