Salome HOME
Synchronize adm files
[modules/kernel.git] / src / Container / SALOME_Container.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2014  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 string
38 import traceback
39 import imp
40 from omniORB import CORBA, PortableServer
41 import SALOMEDS
42 import Engines, Engines__POA
43 from SALOME_NamingServicePy import *
44 from SALOME_ComponentPy import *
45 import SALOME_PyNode
46
47 from SALOME_utilities import *
48 from Utils_Identity import getShortHostName
49 from launchConfigureParser import verbose
50
51 #=============================================================================
52
53 #define an implementation of the container interface for embedding in Container implemented in C++
54
55 class SALOME_Container_i:
56     _orb = None
57     _poa = None
58     _containerName = ""
59     _naming_service = None
60
61     #-------------------------------------------------------------------------
62
63     def __init__(self ,containerName, containerIORStr):
64         MESSAGE( "SALOME_Container_i::__init__" )
65         self._orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
66         self._poa = self._orb.resolve_initial_references("RootPOA")
67         self._containerName = containerName
68         if verbose(): print "SALOME_Container.SALOME_Container_i : _containerName ",self._containerName
69         #self._naming_service = SALOME_NamingServicePy_i(self._orb)
70         self._container = self._orb.string_to_object(containerIORStr)
71
72     #-------------------------------------------------------------------------
73
74     def import_component(self, componentName):
75         MESSAGE( "SALOME_Container_i::import_component" )
76         ret=""
77         try:
78             if verbose(): print "try import ",componentName
79             __import__(componentName)
80             if verbose(): print "import ",componentName," successful"
81         except ImportError,e:
82             #can't import python module componentName
83             #try to find it in python path
84             try:
85               fp, pathname, description = imp.find_module(componentName)
86               if fp:fp.close()
87               #module file found in path
88               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
89               ret=ret+traceback.format_exc(10)
90             except ImportError,ee:
91               ret="ImplementationNotFound"
92             except:
93               if verbose():print "error when calling find_module"
94               ret="ImplementationNotFound"
95         except:
96             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
97             ret=ret+traceback.format_exc(10)
98             if verbose():
99               traceback.print_exc()
100               print "import ",componentName," not possible"
101         return ret
102         
103     #-------------------------------------------------------------------------
104
105     def create_component_instance(self, componentName, instanceName, studyId):
106         MESSAGE( "SALOME_Container_i::create_component_instance" )
107         comp_iors=""
108         ret=""
109         try:
110             component=__import__(componentName)
111             factory=getattr(component,componentName)
112             comp_i=factory(self._orb,
113                            self._poa,
114                            self._container,
115                            self._containerName,
116                            instanceName,
117                            componentName)
118
119             MESSAGE( "SALOME_Container_i::create_component_instance : OK")
120             comp_o = comp_i._this()
121             comp_iors = self._orb.object_to_string(comp_o)
122         except:
123             ret=traceback.format_exc(10)
124             traceback.print_exc()
125             MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
126         return comp_iors, ret
127         
128
129     def create_pynode(self,nodeName,code):
130         try:
131           node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
132           id_o = self._poa.activate_object(node)
133           comp_o = self._poa.id_to_reference(id_o)
134           comp_iors = self._orb.object_to_string(comp_o)
135           return 0,comp_iors
136         except:
137           exc_typ,exc_val,exc_fr=sys.exc_info()
138           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
139           return 1,"".join(l)
140
141     def create_pyscriptnode(self,nodeName,code):
142         try:
143           node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self)
144           id_o = self._poa.activate_object(node)
145           comp_o = self._poa.id_to_reference(id_o)
146           comp_iors = self._orb.object_to_string(comp_o)
147           return 0,comp_iors
148         except:
149           exc_typ,exc_val,exc_fr=sys.exc_info()
150           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
151           return 1,"".join(l)