]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_Container.py
Salome HOME
8203c6b57b5a784519c3311b827791d5ee43c459
[modules/kernel.git] / src / Container / SALOME_Container.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  SALOME Container : implementation of container and engine for Kernel
25 #  File   : SALOME_Container.py
26 #  Author : Paul RASCLE, EDF
27 #  Module : SALOME
28 #  $Header$
29
30 ## @package SALOME_Container
31 # \brief python implementation of container interface for Kernel
32 #
33
34 import os
35 import sys
36 import traceback
37 import importlib
38 from omniORB import CORBA, PortableServer
39 import SALOMEDS
40 import Engines, Engines__POA
41 from SALOME_NamingServicePy import *
42 from SALOME_ComponentPy import *
43 import SALOME_PyNode
44
45 from SALOME_utilities import *
46 from Utils_Identity import getShortHostName
47 from launchConfigureParser import verbose
48 from KernelBasis import VerbosityActivated
49
50 #=============================================================================
51
52 #define an implementation of the container interface for embedding in Container implemented in C++
53
54 class SALOME_Container_i:
55     _orb = None
56     _poa = None
57     _containerName = ""
58     _naming_service = None
59
60     #-------------------------------------------------------------------------
61
62     def __init__(self ,containerName, containerIORStr):
63         MESSAGE( "SALOME_Container_i::__init__" )
64         try:
65           argv = sys.argv
66         except AttributeError :
67           # for embedded python interpreter
68           # shouldn't be needed after python 3.8
69           # see https://bugs.python.org/issue32573
70           argv = ['']
71         self._orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
72         self._poa = self._orb.resolve_initial_references("RootPOA")
73         self._containerName = containerName
74         if verbose(): print("SALOME_Container.SALOME_Container_i : _containerName ",self._containerName)
75         self._container = self._orb.string_to_object(containerIORStr)
76
77     #-------------------------------------------------------------------------
78
79     def import_component(self, componentName):
80         MESSAGE( "SALOME_Container_i::import_component" )
81         ret=""
82         try:
83             if verbose(): print("try import ",componentName)
84             importlib.import_module(componentName)
85             if verbose(): print("import ",componentName," successful")
86         except ImportError:
87             #can't import python module componentName
88             #try to find it in python path
89             try:
90               _specs = importlib.util.find_spec(componentName)
91               _module = importlib.util.module_from_spec(_specs)
92               _specs.loader.exec_module(_module)
93               #module file found in path
94               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
95               ret=ret+traceback.format_exc(10)
96             except ImportError as ee:
97               ret="ImplementationNotFound"
98             except Exception:
99               if verbose():print("error when calling find_module")
100               ret="ImplementationNotFound"
101         except Exception:
102             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
103             ret=ret+traceback.format_exc(10)
104             if verbose():
105               traceback.print_exc()
106               print("import ",componentName," not possible")
107         return ret
108
109     #-------------------------------------------------------------------------
110
111     def create_component_instance(self, componentName, instanceName):
112         MESSAGE( "SALOME_Container_i::create_component_instance" )
113         comp_iors=""
114         ret=""
115         try:
116             component=importlib.import_module(componentName)
117             factory=getattr(component,componentName)
118             comp_i=factory(self._orb,
119                            self._poa,
120                            self._container,
121                            self._containerName,
122                            instanceName,
123                            componentName)
124
125             MESSAGE( "SALOME_Container_i::create_component_instance : OK")
126             comp_o = comp_i._this()
127             comp_iors = self._orb.object_to_string(comp_o)
128         except Exception:
129             ret=traceback.format_exc(10)
130             traceback.print_exc()
131             MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
132         return comp_iors, ret
133
134
135     def create_pynode(self,nodeName,code):
136         try:
137           node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
138           id_o = self._poa.activate_object(node)
139           comp_o = self._poa.id_to_reference(id_o)
140           comp_iors = self._orb.object_to_string(comp_o)
141           return 0,comp_iors
142         except Exception:
143           exc_typ,exc_val,exc_fr=sys.exc_info()
144           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
145           return 1,"".join(l)
146
147     def create_pyscriptnode(self,nodeName,code):
148         try:
149           node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self)
150           id_o = self._poa.activate_object(node)
151           comp_o = self._poa.id_to_reference(id_o)
152           comp_iors = self._orb.object_to_string(comp_o)
153           return 0,comp_iors
154         except Exception:
155           exc_typ,exc_val,exc_fr=sys.exc_info()
156           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
157           return 1,"".join(l)
158         
159     def positionVerbosityOfLogger(self):
160         if VerbosityActivated():
161           import salome_utils
162           salome_utils.positionVerbosityOfLoggerRegardingState()