Salome HOME
Make PYHELLO component appear into SALOME_Session_Server_No_Server application
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
index f27c680b4bb5017e20da85c7ba3f5df9994adb0d..77ed258a712fdf4634e9c684955b6c7806feab7b 100644 (file)
+# 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
+#
+
+# ---
+# File   : PYHELLOGUI.py
+# Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
+# ---
+#
 import PYHELLO_ORB__POA
 import SALOME_ComponentPy
+import SALOME_Embedded_NamingService_ClientPy
+import SALOME_DriverPy
+import SALOMEDS
+from PYHELLO_utils import findOrCreateComponent, objectID, moduleName, getStudy
 
 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
-              SALOME_ComponentPy.SALOME_ComponentPy_i):
+              SALOME_ComponentPy.SALOME_ComponentPy_i,
+              SALOME_DriverPy.SALOME_DriverPy_i):
     """
-        Pour etre un composant SALOME cette classe Python
-        doit avoir le nom du composant et heriter de la
-        classe PYHELLO_Gen issue de la compilation de l'idl
-        par omniidl et de la classe SALOME_ComponentPy_i
-        qui porte les services generaux d'un composant SALOME
+    Construct an instance of PYHELLO module engine.
+    The class PYHELLO implements CORBA interface PYHELLO_Gen (see PYHELLO_Gen.idl).
+    It is inherited from the classes SALOME_ComponentPy_i (implementation of
+    Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
+    (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
     """
-    def __init__ (self, orb, poa, contID, containerName, instanceName, 
-                  interfaceName):
-        print "PYHELLO.__init__: ",containerName,' ',instanceName
+    def __init__ ( self, orb, poa, contID, containerName, instanceName,
+                   interfaceName ):
         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
-                    contID, containerName,instanceName, interfaceName, 0 )
-        # On stocke dans l'attribut _naming_service, une reference sur
-        # le Naming Service CORBA
-        self._naming_service=SALOME_ComponentPy.SALOME_NamingServicePy_i(self._orb)
+                    contID, containerName, instanceName, interfaceName, False)
+        SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
+        #
+        emb_ns = self._contId.get_embedded_NS_if_ssl()
+        import CORBA
+        if CORBA.is_nil(emb_ns):
+            self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
+        else:
+            self._naming_service = SALOME_Embedded_NamingService_ClientPy.SALOME_Embedded_NamingService_ClientPy(emb_ns)
+        #
+        pass
+
+    def getNamingService(self):
+        return self._naming_service
+
+    """
+    Get version information.
+    """
+    def getVersion( self ):
+        import salome_version
+        return salome_version.getVersion("PYHELLO", True)
 
-    def makeBanner(self,name):
-        banner= "Hello %s" % name
+    """
+    Generate hello banner.
+    """
+    def makeBanner( self, name ):
+        banner = "Hello %s!" % name
         return banner
 
+    """
+    Intentionnally raises an exception for test purposes.
+    """
+    def raiseAnException( self ):
+        import SALOME
+        exData = SALOME.ExceptionStruct( SALOME.BAD_PARAM, "Test exception in raiseAnException()",'',0)
+        raise SALOME.SALOME_Exception( exData )
+
+    """
+    Create object.
+    """
+    def createObject( self, name ):
+        study = getStudy( self._naming_service )
+        builder = study.NewBuilder()
+        father  = findOrCreateComponent()
+        obj  = builder.NewObject( father )
+        attr    = builder.FindOrCreateAttribute( obj, "AttributeName" )
+        attr.SetValue( name )
+        attr    = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
+        attr.SetValue( objectID() )
+        pass
+
+    """
+    Dump module data to the Python script.
+    """
+    def DumpPython( self, isPublished, isMultiFile ):
+        abuffer = []
+        names = []
+        study = getStudy()
+        father = study.FindComponent( moduleName() )
+        if father:
+            iterator = study.NewChildIterator(father)
+            while iterator.More():
+                name = iterator.Value().GetName()
+                if name: names.append( name )
+                iterator.Next()
+                pass
+            pass
+        if names:
+            abuffer += [ "import salome" ]
+            abuffer += [ "import PYHELLO_ORB" ]
+            abuffer += [ "" ]
+            abuffer += [ "pyhello = salome.lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
+            abuffer += [ "" ]
+            abuffer += [ "pyhello.createObject( '%s')" % name for name in names ]
+            abuffer += [ "" ]
+            pass
+        if isMultiFile:
+            abuffer = [ "  " + s for s in abuffer ]
+            abuffer[0:0] = [ "def RebuildData():" ]
+            abuffer += [ "    pass" ]
+        abuffer += [ "\0" ]
+        res = "\n".join( abuffer )
+        return (res.encode(), 1)