From 4cefc7671508f0446f0aa9a34abc1929e45a4d4a Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 7 Aug 2014 15:15:19 +0200 Subject: [PATCH] Add some useful method in containers to extend the services to execute remotely python scripts. --- idl/SALOME_Component.idl | 6 ++++++ idl/SALOME_PyNode.idl | 5 +++++ src/Container/Container_i.cxx | 28 ++++++++++++++++++++++++++++ src/Container/SALOME_Container_i.hxx | 4 ++++ src/Container/SALOME_PyNode.py | 8 ++++++++ 5 files changed, 51 insertions(+) diff --git a/idl/SALOME_Component.idl b/idl/SALOME_Component.idl index f399b3308..f645e2219 100644 --- a/idl/SALOME_Component.idl +++ b/idl/SALOME_Component.idl @@ -222,12 +222,18 @@ module Engines */ PyNode createPyNode(in string nodeName, in string code) raises(SALOME::SALOME_Exception); + //! Retrieves the last created PyNode instance with createPyNode. + PyNode getDefaultPyNode(); + //! Create a PyScriptNode in the container /*! \param nodeName the name of the PyScriptNode \param code python code as text to load in the node */ PyScriptNode createPyScriptNode(in string nodeName, in string code) raises(SALOME::SALOME_Exception); + + //! Retrieves the last created PyScriptNode instance with createPyScriptNode. + PyScriptNode getDefaultPyScriptNode(); }; /*! \brief Interface to pass data files from the client side to the SALOME Container. diff --git a/idl/SALOME_PyNode.idl b/idl/SALOME_PyNode.idl index 97c419d3c..401b3b0e9 100644 --- a/idl/SALOME_PyNode.idl +++ b/idl/SALOME_PyNode.idl @@ -53,6 +53,11 @@ module Engines interface PyScriptNode : SALOME::GenericObj { + /*! + This methode executes the python code in \a codeStr and can append/remove symboles in context to make them available or not for future call of execute on this. + \param [in] codeStr - the python code (without statement) to be executed, that can modify the context initialized at initialization. + */ + void executeAnotherPieceOfCode(in string codeStr) raises (SALOME::SALOME_Exception); /*! \brief execute a python script defined in the node diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index a8a5f0147..5552a5df5 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -1646,6 +1646,7 @@ Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, cons { CORBA::Object_var obj = _orb->string_to_object(astr.c_str()); node = Engines::PyNode::_narrow(obj); + _dftPyNode = node; return node._retn(); } else @@ -1658,6 +1659,19 @@ Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, cons } +//============================================================================= +/*! \brief Retrieves the last created PyNode instance with createPyNode. + * + */ +//============================================================================= +Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode() +{ + if(!CORBA::is_nil(_dftPyNode)) + return Engines::PyNode::_duplicate(_dftPyNode); + else + return Engines::PyNode::_nil(); +} + //============================================================================= /*! \brief create a PyScriptNode object to execute remote python code * \param nodeName the name of the node @@ -1695,6 +1709,7 @@ Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* no { CORBA::Object_var obj = _orb->string_to_object(astr.c_str()); node = Engines::PyScriptNode::_narrow(obj); + _dftPyScriptNode = node; return node._retn(); } else @@ -1706,6 +1721,19 @@ Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* no } } +//============================================================================= +/*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode. + * + */ +//============================================================================= +Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode() +{ + if(!CORBA::is_nil(_dftPyScriptNode)) + return Engines::PyScriptNode::_duplicate(_dftPyScriptNode); + else + return Engines::PyScriptNode::_nil(); +} + //============================================================================= /* int checkifexecutable(const char *filename) * diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index 5a48dc9c0..494d563b5 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -106,7 +106,9 @@ public: virtual Engines::Salome_file_ptr createSalome_file(const char* origFileName); void copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile); Engines::PyNode_ptr createPyNode(const char* nodeName, const char* code); + Engines::PyNode_ptr getDefaultPyNode(); Engines::PyScriptNode_ptr createPyScriptNode(const char* nodeName, const char* code); + Engines::PyScriptNode_ptr getDefaultPyScriptNode(); // --- local C++ methods Engines::EngineComponent_ptr @@ -154,6 +156,8 @@ protected: std::map _listInstances_map; std::map _fileRef_map; std::map _Salome_file_map; + Engines::PyScriptNode_var _dftPyScriptNode; + Engines::PyNode_var _dftPyNode; std::list _tmp_files; Engines::fileTransfer_var _fileTransfer; diff --git a/src/Container/SALOME_PyNode.py b/src/Container/SALOME_PyNode.py index 04ce26b7a..2f8418fab 100644 --- a/src/Container/SALOME_PyNode.py +++ b/src/Container/SALOME_PyNode.py @@ -90,6 +90,14 @@ class PyScriptNode_i (Engines__POA.PyScriptNode,Generic): self.context={} self.context["my_container"] = self.my_container + def executeAnotherPieceOfCode(self,code): + """Called for initialization of container lodging self.""" + try: + ccode=compile(code,self.nodeName,'exec') + exec ccode in self.context + except: + raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0)) + def execute(self,outargsname,argsin): """Execute the script stored in attribute ccode with pickled args (argsin)""" try: -- 2.39.2