Back to index

salome-kernel  6.5.0
SALOME_Container.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 #  SALOME Container : implementation of container and engine for Kernel
00026 #  File   : SALOME_Container.py
00027 #  Author : Paul RASCLE, EDF
00028 #  Module : SALOME
00029 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/SALOME_Container.py,v 1.5.2.4.14.4.12.1 2012-04-12 14:05:03 vsr Exp $
00030 
00031 ## @package SALOME_Container
00032 # \brief python implementation of container interface for Kernel
00033 #
00034 
00035 import os
00036 import sys
00037 import string
00038 import traceback
00039 import imp
00040 from omniORB import CORBA, PortableServer
00041 import SALOMEDS
00042 import Engines, Engines__POA
00043 from SALOME_NamingServicePy import *
00044 from SALOME_ComponentPy import *
00045 import SALOME_PyNode
00046 
00047 from SALOME_utilities import *
00048 from Utils_Identity import getShortHostName
00049 from launchConfigureParser import verbose
00050 
00051 #=============================================================================
00052 
00053 #define an implementation of the container interface for embedding in Container implemented in C++
00054 
00055 class SALOME_Container_i:
00056     _orb = None
00057     _poa = None
00058     _containerName = ""
00059     _naming_service = None
00060 
00061     #-------------------------------------------------------------------------
00062 
00063     def __init__(self ,containerName, containerIORStr):
00064         MESSAGE( "SALOME_Container_i::__init__" )
00065         self._orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
00066         self._poa = self._orb.resolve_initial_references("RootPOA")
00067         self._containerName = containerName
00068         if verbose(): print "SALOME_Container.SALOME_Container_i : _containerName ",self._containerName
00069         #self._naming_service = SALOME_NamingServicePy_i(self._orb)
00070         self._container = self._orb.string_to_object(containerIORStr)
00071 
00072     #-------------------------------------------------------------------------
00073 
00074     def import_component(self, componentName):
00075         MESSAGE( "SALOME_Container_i::import_component" )
00076         ret=""
00077         try:
00078             if verbose(): print "try import ",componentName
00079             __import__(componentName)
00080             if verbose(): print "import ",componentName," successful"
00081         except ImportError,e:
00082             #can't import python module componentName
00083             #try to find it in python path
00084             try:
00085               fp, pathname, description = imp.find_module(componentName)
00086               if fp:fp.close()
00087               #module file found in path
00088               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
00089               ret=ret+traceback.format_exc(10)
00090             except ImportError,ee:
00091               ret="ImplementationNotFound"
00092             except:
00093               if verbose():print "error when calling find_module"
00094               ret="ImplementationNotFound"
00095         except:
00096             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
00097             ret=ret+traceback.format_exc(10)
00098             if verbose():
00099               traceback.print_exc()
00100               print "import ",componentName," not possible"
00101         return ret
00102         
00103     #-------------------------------------------------------------------------
00104 
00105     def create_component_instance(self, componentName, instanceName, studyId):
00106         MESSAGE( "SALOME_Container_i::create_component_instance" )
00107         comp_iors=""
00108         ret=""
00109         try:
00110             component=__import__(componentName)
00111             factory=getattr(component,componentName)
00112             comp_i=factory(self._orb,
00113                            self._poa,
00114                            self._container,
00115                            self._containerName,
00116                            instanceName,
00117                            componentName)
00118 
00119             MESSAGE( "SALOME_Container_i::create_component_instance : OK")
00120             comp_o = comp_i._this()
00121             comp_iors = self._orb.object_to_string(comp_o)
00122         except:
00123             ret=traceback.format_exc(10)
00124             traceback.print_exc()
00125             MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
00126         return comp_iors, ret
00127         
00128 
00129     def create_pynode(self,nodeName,code):
00130         try:
00131           node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
00132           id_o = self._poa.activate_object(node)
00133           comp_o = self._poa.id_to_reference(id_o)
00134           comp_iors = self._orb.object_to_string(comp_o)
00135           return 0,comp_iors
00136         except:
00137           exc_typ,exc_val,exc_fr=sys.exc_info()
00138           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
00139           return 1,"".join(l)
00140 
00141     def create_pyscriptnode(self,nodeName,code):
00142         try:
00143           node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self)
00144           id_o = self._poa.activate_object(node)
00145           comp_o = self._poa.id_to_reference(id_o)
00146           comp_iors = self._orb.object_to_string(comp_o)
00147           return 0,comp_iors
00148         except:
00149           exc_typ,exc_val,exc_fr=sys.exc_info()
00150           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
00151           return 1,"".join(l)