Salome HOME
Updated copyright comment
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
1 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 # ---
24 # File   : PYHELLOGUI.py
25 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 # ---
27 #
28 import PYHELLO_ORB__POA
29 import SALOME_ComponentPy
30 import SALOME_Embedded_NamingService_ClientPy
31 import SALOME_DriverPy
32 import SALOMEDS
33 from PYHELLO_utils import findOrCreateComponent, objectID, moduleName, getStudy
34
35 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
36               SALOME_ComponentPy.SALOME_ComponentPy_i,
37               SALOME_DriverPy.SALOME_DriverPy_i):
38     """
39     Construct an instance of PYHELLO module engine.
40     The class PYHELLO implements CORBA interface PYHELLO_Gen (see PYHELLO_Gen.idl).
41     It is inherited from the classes SALOME_ComponentPy_i (implementation of
42     Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
43     (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
44     """
45     def __init__ ( self, orb, poa, contID, containerName, instanceName,
46                    interfaceName ):
47         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
48                     contID, containerName, instanceName, interfaceName, False)
49         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
50         #
51         emb_ns = self._contId.get_embedded_NS_if_ssl()
52         import CORBA
53         if CORBA.is_nil(emb_ns):
54             self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
55         else:
56             self._naming_service = SALOME_Embedded_NamingService_ClientPy.SALOME_Embedded_NamingService_ClientPy(emb_ns)
57         pass
58
59     """
60     Get version information.
61     """
62     def getVersion( self ):
63         import salome_version
64         return salome_version.getVersion("PYHELLO", True)
65
66     """
67     Generate hello banner.
68     """
69     def makeBanner( self, name ):
70         banner = "Hello %s!" % name
71         return banner
72
73     """
74     Intentionnally raises an exception for test purposes.
75     """
76     def raiseAnException( self ):
77         import SALOME
78         exData = SALOME.ExceptionStruct( SALOME.BAD_PARAM, "Test exception in raiseAnException()",'',0)
79         raise SALOME.SALOME_Exception( exData )
80
81     """
82     Create object.
83     """
84     def createObject( self, name ):
85         study = getStudy()
86         builder = study.NewBuilder()
87         father  = findOrCreateComponent()
88         obj  = builder.NewObject( father )
89         attr    = builder.FindOrCreateAttribute( obj, "AttributeName" )
90         attr.SetValue( name )
91         attr    = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
92         attr.SetValue( objectID() )
93         pass
94
95     """
96     Dump module data to the Python script.
97     """
98     def DumpPython( self, isPublished, isMultiFile ):
99         abuffer = []
100         names = []
101         study = getStudy()
102         father = study.FindComponent( moduleName() )
103         if father:
104             iterator = study.NewChildIterator(father)
105             while iterator.More():
106                 name = iterator.Value().GetName()
107                 if name: names.append( name )
108                 iterator.Next()
109                 pass
110             pass
111         if names:
112             abuffer += [ "import salome" ]
113             abuffer += [ "import PYHELLO_ORB" ]
114             abuffer += [ "" ]
115             abuffer += [ "pyhello = salome.lcc.FindOrLoadComponent( 'FactoryServer', '%s' )" % moduleName() ]
116             abuffer += [ "" ]
117             abuffer += [ "pyhello.createObject( '%s')" % name for name in names ]
118             abuffer += [ "" ]
119             pass
120         if isMultiFile:
121             abuffer = [ "  " + s for s in abuffer ]
122             abuffer[0:0] = [ "def RebuildData():" ]
123             abuffer += [ "    pass" ]
124         abuffer += [ "\0" ]
125         res = "\n".join( abuffer )
126         return (res.encode(), 1)