]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
PR: C++ container used as a Python container
authorprascle <prascle>
Fri, 13 May 2005 08:32:04 +0000 (08:32 +0000)
committerprascle <prascle>
Fri, 13 May 2005 08:32:04 +0000 (08:32 +0000)
src/Container/Container_i.cxx
src/Container/Makefile.in
src/Container/SALOME_Container.cxx
src/Container/SALOME_Container.py [new file with mode: 0644]

index 5cc7350ff93eb2f2ab1ab59e5c4c9f616f44191e..a4a0d12c677338794852aa1f1f98be50141b1a36 100644 (file)
@@ -37,6 +37,7 @@
 #include <stdio.h>
 #include <dlfcn.h>
 #include <unistd.h>
+#include <Python.h>
 
 #include "utilities.h"
 using namespace std;
@@ -54,6 +55,24 @@ char ** _ArgV ;
 extern "C" {void ActSigIntHandler() ; }
 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
 
+
+static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }};
+PyThreadState *gtstate;
+
+void init_python(int argc, char **argv)
+{
+  if (gtstate)
+    return;
+  Py_SetProgramName(argv[0]);
+  Py_Initialize(); // Initialize the interpreter
+  PySys_SetArgv(argc, argv);
+  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
+  Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ;
+  //PyOS_setsig(SIGSEGV,&Handler);
+  //PyOS_setsig(SIGINT,&Handler);
+  gtstate = PyEval_SaveThread(); // Release the global thread state
+}
+
 const char *Engines_Container_i::_defaultContainerName="FactoryServer";
 map<std::string, int> Engines_Container_i::_cntInstances_map;
 map<std::string, void *> Engines_Container_i::_library_map;
@@ -139,7 +158,24 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
       Engines::Container_var pCont 
        = Engines::Container::_narrow(obj);
       SCRUTE(_containerName);
-      _NS->Register(pCont, _containerName.c_str()); 
+      _NS->Register(pCont, _containerName.c_str());
+
+      // Python: 
+      // import SALOME_Container
+      // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
+    
+      init_python(argc,argv);
+
+      CORBA::String_var sior =  _orb->object_to_string(pCont);
+      string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
+      myCommand += sior;
+      myCommand += "')\n";
+      SCRUTE(myCommand);
+
+      PyEval_RestoreThread(gtstate);
+      PyRun_SimpleString("import SALOME_Container\n");
+      PyRun_SimpleString((char*)myCommand.c_str());
+      PyEval_ReleaseThread(gtstate);
     }
 }
 
@@ -332,6 +368,7 @@ Engines_Container_i::find_component_instance( const char* registeredName,
              return anEngine._retn();
            }
        }
+      itm++;
     }
   return anEngine._retn();  
 }
index fa963603b2e7c1e5197cd99ab423f60e34a0e6a2..7098ace1516fea8440a17bca12e5045031642b33 100644 (file)
@@ -34,7 +34,9 @@ VPATH=.:@srcdir@:@top_srcdir@/idl
 
 @COMMENCE@
 
-EXPORT_PYSCRIPTS = SALOME_ComponentPy.py SALOME_ContainerPy.py
+EXPORT_PYSCRIPTS = SALOME_ComponentPy.py \
+       SALOME_ContainerPy.py \
+       SALOME_Container.py
 
 EXPORT_HEADERS = \
        SALOME_Component_i.hxx \
index ccfcbc16599e1dbbb2950683467d4710cd1834a4..bd7d8539bb5fea6eff914be5dd7a279376e4e6fd 100644 (file)
@@ -53,23 +53,6 @@ using namespace std;
 
 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
 
-static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }};
-PyThreadState *gtstate;
-
-void init_python(int argc, char **argv)
-{
-  if (gtstate)
-    return;
-  Py_SetProgramName(argv[0]);
-  Py_Initialize(); // Initialize the interpreter
-  PySys_SetArgv(argc, argv);
-  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
-  Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ;
-  //PyOS_setsig(SIGSEGV,&Handler);
-  //PyOS_setsig(SIGINT,&Handler);
-  gtstate = PyEval_SaveThread(); // Release the global thread state
-}
-
 int main(int argc, char* argv[])
 {
 #ifdef HAVE_MPI2
@@ -81,8 +64,6 @@ int main(int argc, char* argv[])
   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
   INFOS_COMPILATION;
   BEGIN_OF(argv[0]);
-    
-  init_python(argc,argv);
   
   try{
     // Obtain a reference to the root POA.
diff --git a/src/Container/SALOME_Container.py b/src/Container/SALOME_Container.py
new file mode 100644 (file)
index 0000000..f1bfd82
--- /dev/null
@@ -0,0 +1,105 @@
+#! /usr/bin/env python
+#
+#  SALOME Container : implementation of container and engine for Kernel
+#
+#  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
+# 
+#  This library is free software; you can redistribute it and/or 
+#  modify it under the terms of the GNU Lesser General Public 
+#  License as published by the Free Software Foundation; either 
+#  version 2.1 of the License. 
+# 
+#  This library is distributed in the hope that it will be useful, 
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+#  Lesser General Public License for more details. 
+# 
+#  You should have received a copy of the GNU Lesser General Public 
+#  License along with this library; if not, write to the Free Software 
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
+# 
+#  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+#
+#
+#
+#  File   : SALOME_Container.py
+#  Author : Paul RASCLE, EDF
+#  Module : SALOME
+#  $Header$
+
+import os
+import sys
+import string
+from omniORB import CORBA, PortableServer
+# import SALOMEDS before other SALOME modules
+# (if not, incomplete import done by SALOME module: no load of SALOMEDS_attributes)
+import SALOMEDS 
+import Engines, Engines__POA
+reload(Engines)
+reload(Engines__POA)
+from SALOME_NamingServicePy import *
+from SALOME_ComponentPy import *
+
+from SALOME_utilities import *
+from Utils_Identity import getShortHostName
+
+#=============================================================================
+
+#define an implementation of the container interface
+
+class SALOME_Container_i:
+    _orb = None
+    _poa = None
+    _containerName = ""
+    _naming_service = None
+
+    #-------------------------------------------------------------------------
+
+    def __init__(self, containerIORStr ):
+        MESSAGE( "SALOME_Container_i::__init__" )
+        self._orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
+        self._poa = self._orb.resolve_initial_references("RootPOA")
+        self._containerName = sys.argv[1]
+        #self._naming_service = SALOME_NamingServicePy_i(self._orb)
+        self._container = self._orb.string_to_object(containerIORStr)
+
+    #-------------------------------------------------------------------------
+
+    def import_component(self, componentName):
+        MESSAGE( "SALOME_Container_i::import_component" )
+        ret=0
+        try:
+            print "try import ",componentName
+            __import__(componentName)
+            print "import ",componentName," successful"
+            ret=1
+        except:
+            print "import ",componentName," not possible"
+        return ret
+        
+    #-------------------------------------------------------------------------
+
+    def create_component_instance(self, componentName, instanceName, studyId):
+        MESSAGE( "SALOME_Container_i::create_component_instance" )
+        comp_iors=""
+        try:
+            component=__import__(componentName)
+            factory=getattr(component,componentName)
+            comp_i=factory(self._orb,
+                           self._poa,
+                           self._container,
+                           self._containerName,
+                           instanceName,
+                           componentName)
+
+            MESSAGE( "SALOME_Container_i::create_component_instance : OK")
+            comp_o = comp_i._this()
+            comp_iors = self._orb.object_to_string(comp_o)
+        except:
+            import traceback
+            traceback.print_exc()
+            MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
+        return comp_iors 
+        
+