Salome HOME
Copyright update 2020
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
index b9592b9f503048fdb43aff48ce29223258a583b9..1cdcadcedd2ec85ceeb52fce2b777bc7a95c3e9a 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2020  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
@@ -29,9 +29,7 @@ import PYHELLO_ORB__POA
 import SALOME_ComponentPy
 import SALOME_DriverPy
 import SALOMEDS
-from SALOME_DataContainerPy import *
-
-from PYHELLO_utils import *
+from PYHELLO_utils import findOrCreateComponent, objectID, moduleName, getStudy
 
 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
               SALOME_ComponentPy.SALOME_ComponentPy_i,
@@ -43,10 +41,10 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
     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, 
+    def __init__ ( self, orb, poa, contID, containerName, instanceName,
                    interfaceName ):
         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
-                    contID, containerName, instanceName, interfaceName, 0)
+                    contID, containerName, instanceName, interfaceName, False)
         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
         #
         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
@@ -67,84 +65,57 @@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
         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, study, name ):
-        self._createdNew = True # used for getModifiedData method
+    def createObject( self, name ):
+        study = getStudy()
         builder = study.NewBuilder()
-        father  = findOrCreateComponent( study )
-        object  = builder.NewObject( father )
-        attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
+        father  = findOrCreateComponent()
+        obj  = builder.NewObject( father )
+        attr    = builder.FindOrCreateAttribute( obj, "AttributeName" )
         attr.SetValue( name )
-        attr    = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
+        attr    = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
         attr.SetValue( objectID() )
         pass
 
     """
     Dump module data to the Python script.
     """
-    def DumpPython( self, study, isPublished, isMultiFile ):
+    def DumpPython( self, isPublished, isMultiFile ):
         abuffer = []
         names = []
+        study = getStudy()
         father = study.FindComponent( moduleName() )
         if father:
-            iter = study.NewChildIterator( father )
-            while iter.More():
-                name = iter.Value().GetName()
+            iterator = study.NewChildIterator(father)
+            while iterator.More():
+                name = iterator.Value().GetName()
                 if name: names.append( name )
-                iter.Next()
+                iterator.Next()
                 pass
             pass
         if names:
-            abuffer += [ "from salome import lcc" ]
+            abuffer += [ "import salome" ]
             abuffer += [ "import PYHELLO_ORB" ]
             abuffer += [ "" ]
-            abuffer += [ "pyhello = lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
+            abuffer += [ "pyhello = salome.lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
             abuffer += [ "" ]
-            abuffer += [ "pyhello.createObject( theStudy, '%s' )" % name for name in names ]
+            abuffer += [ "pyhello.createObject( '%s')" % name for name in names ]
             abuffer += [ "" ]
             pass
         if isMultiFile:
-            abuffer       = [ "  " + s for s in abuffer ]
-            abuffer[0:0]  = [ "def RebuildData( theStudy ):" ]
-            abuffer      += [ "  pass" ]
+            abuffer = [ "  " + s for s in abuffer ]
+            abuffer[0:0] = [ "def RebuildData():" ]
+            abuffer += [ "    pass" ]
         abuffer += [ "\0" ]
-        return ("\n".join( abuffer ), 1)
-
-    """
-    Import file to restore module data
-    """
-    def importData(self, studyId, dataContainer, options):
-      # get study by Id
-      obj = self._naming_service.Resolve("myStudyManager")
-      myStudyManager = obj._narrow(SALOMEDS.StudyManager)
-      study = myStudyManager.GetStudyByID(studyId)
-      # create all objects from the imported stream
-      stream = dataContainer.get()
-      for objname in stream.split("\n"):
-        if len(objname) != 0:
-          self.createObject(study, objname)
-      self._createdNew = False # to store the modification of the study information later
-      return ["objects"] # identifier what is in this file
-
-    def getModifiedData(self, studyId):
-      if self._createdNew:
-        # get study by Id
-        obj = self._naming_service.Resolve("myStudyManager")
-        myStudyManager = obj._narrow(SALOMEDS.StudyManager)
-        study = myStudyManager.GetStudyByID(studyId)
-        # iterate all objects to get their names and store this information in stream
-        stream=""
-        father = study.FindComponent( moduleName() )
-        if father:
-            iter = study.NewChildIterator( father )
-            while iter.More():
-                name = iter.Value().GetName()
-                stream += name + "\n"
-                iter.Next()
-        # store stream to the temporary file to send it in DataContainer
-        dataContainer = SALOME_DataContainerPy_i(stream, "", "objects", False, True)
-        aVar = dataContainer._this()
-        return [aVar]
-      return []
+        res = "\n".join( abuffer )
+        return (res.encode(), 1)