From: Anthony Geay Date: Tue, 20 Apr 2021 06:39:45 +0000 (+0200) Subject: On donne acces en python a SALOME_Fake_NamingService C++ avec le module python Naming... X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=78f04636a38991ac7c4ce58cebddac55c9b5437e;p=modules%2Fkernel.git On donne acces en python a SALOME_Fake_NamingService C++ avec le module python NamingService --- diff --git a/src/NamingService/CMakeLists.txt b/src/NamingService/CMakeLists.txt index 1de6ae9fc..051bfb4bb 100644 --- a/src/NamingService/CMakeLists.txt +++ b/src/NamingService/CMakeLists.txt @@ -17,6 +17,8 @@ # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # +include(${SWIG_USE_FILE}) + INCLUDE_DIRECTORIES( ${OMNIORB_INCLUDE_DIR} ${PTHREAD_INCLUDE_DIR} @@ -28,6 +30,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/../LifeCycleCORBA ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_BINARY_DIR}/idl + ${PYTHON_INCLUDE_DIRS} ) SET(SalomeNS_SOURCES @@ -54,3 +57,19 @@ SALOME_INSTALL_SCRIPTS(SALOME_Embedded_NamingService_ClientPy.py ${SALOME_INSTAL FILE(GLOB COMMON_HEADERS_HXX "${CMAKE_CURRENT_SOURCE_DIR}/*.hxx") INSTALL(FILES ${COMMON_HEADERS_HXX} DESTINATION ${SALOME_INSTALL_HEADERS}) + +SET(NamingService_HEADERS NamingService.i) +SET(NamingService_SOURCES ${NamingService_HEADERS}) +SET_SOURCE_FILES_PROPERTIES(NamingService.i PROPERTIES CPLUSPLUS ON) +SET_SOURCE_FILES_PROPERTIES(NamingService.i PROPERTIES SWIG_FLAGS "-py3") +SET_SOURCE_FILES_PROPERTIES(NamingService_wrap.cpp PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H") +SET(_swig_SCRIPTS ${CMAKE_CURRENT_BINARY_DIR}/NamingService.py ) +IF(${CMAKE_VERSION} VERSION_LESS "3.8.0") + SWIG_ADD_MODULE(NamingService python ${NamingService_SOURCES}) +ELSE() + SWIG_ADD_LIBRARY(NamingService LANGUAGE python SOURCES ${NamingService_SOURCES}) +ENDIF() +SWIG_LINK_LIBRARIES(NamingService ${PYTHON_LIBRARIES} ${PLATFORM_LIBS} SalomeNS) +install(TARGETS _NamingService DESTINATION ${SALOME_INSTALL_LIBS}) +install(FILES ${NamingService_HEADERS} DESTINATION ${SALOME_INSTALL_HEADERS}) +SALOME_INSTALL_SCRIPTS("${_swig_SCRIPTS}" ${SALOME_INSTALL_BINS} EXTRA_DPYS "${SWIG_MODULE_NamingService_REAL_NAME}") diff --git a/src/NamingService/NamingService.i b/src/NamingService/NamingService.i new file mode 100644 index 000000000..a477d6165 --- /dev/null +++ b/src/NamingService/NamingService.i @@ -0,0 +1,68 @@ +// Copyright (C) 2021 CEA/DEN, EDF R&D +// +// 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, or (at your option) any later version. +// +// 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.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +%module NamingService + +%{ +#include "SALOME_Fake_NamingService.hxx" +#include "SALOME_KernelORB.hxx" +%} + +%include "std_string.i" +%include "std_vector.i" + +%rename(NamingService) SALOME_Fake_NamingService ; + +%template(svec) std::vector; + +class SALOME_Fake_NamingService +{ +public: + SALOME_Fake_NamingService(); + std::vector< std::string > repr(); + %extend { + std::string _ResolveInternal(const char *Path) + { + CORBA::Object_var obj = self->Resolve(Path); + CORBA::ORB_ptr orb = KERNEL::getORB(); + CORBA::String_var ior = orb->object_to_string(obj); + return std::string(ior); + } + void _RegisterInternal(const char *ior, const char* Path) + { + CORBA::ORB_ptr orb = KERNEL::getORB(); + CORBA::Object_var obj = orb->string_to_object(ior); + self->Register(obj,Path); + } + } +}; + +%pythoncode %{ +def NamingService_Resolve(self,Path): + ret = self._ResolveInternal(Path) + import CORBA + orb=CORBA.ORB_init(['']) + return orb.string_to_object(ret) +def NamingService_Register(self,obj,Path): + import CORBA + orb=CORBA.ORB_init(['']) + self._RegisterInternal( orb.object_to_string(obj) , Path) +NamingService.Resolve = NamingService_Resolve +NamingService.Register = NamingService_Register +%} diff --git a/src/NamingService/SALOME_Fake_NamingService.cxx b/src/NamingService/SALOME_Fake_NamingService.cxx index 15d378aaa..9dd24c20f 100644 --- a/src/NamingService/SALOME_Fake_NamingService.cxx +++ b/src/NamingService/SALOME_Fake_NamingService.cxx @@ -29,15 +29,15 @@ SALOME_Fake_NamingService::SALOME_Fake_NamingService(CORBA::ORB_ptr orb) { } -std::string SALOME_Fake_NamingService::repr() +std::vector< std::string > SALOME_Fake_NamingService::repr() { std::lock_guard g(_mutex); - std::ostringstream oss; + std::vector< std::string > ret; for(auto it : _map) { - oss << it.first << std::endl; + ret.push_back( it.first ); } - return oss.str(); + return ret; } void SALOME_Fake_NamingService::init_orb(CORBA::ORB_ptr orb) diff --git a/src/NamingService/SALOME_Fake_NamingService.hxx b/src/NamingService/SALOME_Fake_NamingService.hxx index 64a107937..1288aed74 100644 --- a/src/NamingService/SALOME_Fake_NamingService.hxx +++ b/src/NamingService/SALOME_Fake_NamingService.hxx @@ -32,7 +32,7 @@ class NAMINGSERVICE_EXPORT SALOME_Fake_NamingService : public SALOME_NamingServi public: SALOME_Fake_NamingService(CORBA::ORB_ptr orb); SALOME_Fake_NamingService() = default; - std::string repr() override; + std::vector< std::string > repr() override; void init_orb(CORBA::ORB_ptr orb=0) override; void Register(CORBA::Object_ptr ObjRef, const char* Path) override; CORBA::Object_ptr Resolve(const char* Path) override; diff --git a/src/NamingService/SALOME_NamingService.cxx b/src/NamingService/SALOME_NamingService.cxx index d3f62fddd..898a83c16 100644 --- a/src/NamingService/SALOME_NamingService.cxx +++ b/src/NamingService/SALOME_NamingService.cxx @@ -92,9 +92,9 @@ SALOME_NamingService::~SALOME_NamingService() //MESSAGE("SALOME_NamingService destruction"); } -std::string SALOME_NamingService::repr() +std::vector< std::string > SALOME_NamingService::repr() { - return std::string(); + return std::vector< std::string >(); } // ============================================================================ diff --git a/src/NamingService/SALOME_NamingService.hxx b/src/NamingService/SALOME_NamingService.hxx index 78a62fc44..ec19bb7b7 100644 --- a/src/NamingService/SALOME_NamingService.hxx +++ b/src/NamingService/SALOME_NamingService.hxx @@ -51,7 +51,7 @@ public: SALOME_NamingService(CORBA::ORB_ptr orb); virtual ~SALOME_NamingService(); - std::string repr() override; + std::vector< std::string > repr() override; void init_orb(CORBA::ORB_ptr orb=0) override; void Register(CORBA::Object_ptr ObjRef, const char* Path) override; CORBA::Object_ptr Resolve(const char* Path) override; diff --git a/src/NamingService/SALOME_NamingService.i b/src/NamingService/SALOME_NamingService.i deleted file mode 100644 index 9369d0281..000000000 --- a/src/NamingService/SALOME_NamingService.i +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE -// -// Copyright (C) 2003-2007 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, or (at your option) any later version. -// -// 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.salome-platform.org/ or email : webmaster.salome@opencascade.com -// - -// SALOME NamingService : wrapping NamingService services -// File : SALOME_NamingService.i -// Author : Paul RASCLE, EDF -// Module : SALOME -// $Header$ -// -%{ -#include "SALOME_NamingService.hxx" -//#include -#include -#include "ServiceUnreachable.hxx" -%} - -%except(python) -{ - try - { - $function - } - catch (ServiceUnreachable) - { - PyErr_SetString(PyExc_IndexError,"Naming Service Unreacheable"); - return NULL; - } -} - -class SALOME_NamingService -{ -public: - - //! standard constructor - SALOME_NamingService(); - - //! standard destructor - virtual ~SALOME_NamingService(); - - //! initialize ORB reference after default constructor - void init_orb(CORBA::ORB_ptr orb); - - //! method to create an association in the NamingService between an object reference and a path - void Register(CORBA::Object_ptr ObjRef, const char* Path) - throw(ServiceUnreachable); - - //! method to get the ObjRef of a symbolic name - CORBA::Object_ptr Resolve(const char* Path) - throw( ServiceUnreachable); - - //! method to research a name from the naming service's current directory - int Find(const char* name) - throw(ServiceUnreachable); - - //! method to create a directory from the current directory - bool Create_Directory(const char* Path) - throw(ServiceUnreachable); - - //! method to change the current directory to the directory Path indicated in "in" Parameter - bool Change_Directory(const char* Path) - throw(ServiceUnreachable); - - //!method to get the current directory - char* Current_Directory() - throw(ServiceUnreachable); - - //!method to print all the contexts contained from the current directory - void list() - throw(ServiceUnreachable); - - //! method to destroy an association Path-Object Reference - void Destroy_Name(const char* Path) - throw(ServiceUnreachable); - - //! method to destroy a directory if it is empty - virtual void Destroy_Directory(const char* Path) - throw(ServiceUnreachable); -}; - diff --git a/src/NamingService/SALOME_NamingService_Abstract.hxx b/src/NamingService/SALOME_NamingService_Abstract.hxx index dc86047e3..fdd6ae0d4 100644 --- a/src/NamingService/SALOME_NamingService_Abstract.hxx +++ b/src/NamingService/SALOME_NamingService_Abstract.hxx @@ -45,7 +45,7 @@ class NAMINGSERVICE_EXPORT SALOME_NamingService_Abstract : public SALOME_NamingS { public: SALOME_NamingService_Abstract *cloneCoVar(); - virtual std::string repr() = 0; + virtual std::vector< std::string > repr() = 0; virtual void Destroy_Directory(const char* Path) = 0; virtual bool Change_Directory(const char* Path) = 0; virtual std::vector list_subdirs() = 0;