Salome HOME
Merge master into V9_dev
[modules/kernel.git] / src / Container / SALOME_Container.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 #  SALOME Container : implementation of container and engine for Kernel
26 #  File   : SALOME_Container.py
27 #  Author : Paul RASCLE, EDF
28 #  Module : SALOME
29 #  $Header$
30
31 ## @package SALOME_Container
32 # \brief python implementation of container interface for Kernel
33 #
34
35 import os
36 import sys
37 import traceback
38 import imp
39 from omniORB import CORBA, PortableServer
40 import SALOMEDS
41 import Engines, Engines__POA
42 from SALOME_NamingServicePy import *
43 from SALOME_ComponentPy import *
44 import SALOME_PyNode
45
46 from SALOME_utilities import *
47 from Utils_Identity import getShortHostName
48 from launchConfigureParser import verbose
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         self._orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
65         self._poa = self._orb.resolve_initial_references("RootPOA")
66         self._containerName = containerName
67         if verbose(): print("SALOME_Container.SALOME_Container_i : _containerName ",self._containerName)
68         #self._naming_service = SALOME_NamingServicePy_i(self._orb)
69         self._container = self._orb.string_to_object(containerIORStr)
70
71     #-------------------------------------------------------------------------
72
73     def import_component(self, componentName):
74         MESSAGE( "SALOME_Container_i::import_component" )
75         ret=""
76         try:
77             if verbose(): print("try import ",componentName)
78             __import__(componentName)
79             if verbose(): print("import ",componentName," successful")
80         except ImportError as e:
81             #can't import python module componentName
82             #try to find it in python path
83             try:
84               fp, pathname, description = imp.find_module(componentName)
85               if fp:fp.close()
86               #module file found in path
87               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
88               ret=ret+traceback.format_exc(10)
89             except ImportError as ee:
90               ret="ImplementationNotFound"
91             except:
92               if verbose():print("error when calling find_module")
93               ret="ImplementationNotFound"
94         except:
95             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
96             ret=ret+traceback.format_exc(10)
97             if verbose():
98               traceback.print_exc()
99               print("import ",componentName," not possible")
100         return ret
101         
102     #-------------------------------------------------------------------------
103
104     def create_component_instance(self, componentName, instanceName):
105         MESSAGE( "SALOME_Container_i::create_component_instance" )
106         comp_iors=""
107         ret=""
108         try:
109             component=__import__(componentName)
110             factory=getattr(component,componentName)
111             comp_i=factory(self._orb,
112                            self._poa,
113                            self._container,
114                            self._containerName,
115                            instanceName,
116                            componentName)
117
118             MESSAGE( "SALOME_Container_i::create_component_instance : OK")
119             comp_o = comp_i._this()
120             comp_iors = self._orb.object_to_string(comp_o)
121         except:
122             ret=traceback.format_exc(10)
123             traceback.print_exc()
124             MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
125         return comp_iors, ret
126         
127
128     def create_pynode(self,nodeName,code):
129         try:
130           node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
131           id_o = self._poa.activate_object(node)
132           comp_o = self._poa.id_to_reference(id_o)
133           comp_iors = self._orb.object_to_string(comp_o)
134           return 0,comp_iors
135         except:
136           exc_typ,exc_val,exc_fr=sys.exc_info()
137           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
138           return 1,"".join(l)
139
140     def create_pyscriptnode(self,nodeName,code):
141         try:
142           node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self)
143           id_o = self._poa.activate_object(node)
144           comp_o = self._poa.id_to_reference(id_o)
145           comp_iors = self._orb.object_to_string(comp_o)
146           return 0,comp_iors
147         except:
148           exc_typ,exc_val,exc_fr=sys.exc_info()
149           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
150           return 1,"".join(l)