Salome HOME
Copyright update: 2016
[tools/yacsgen.git] / module_generator / pyth_tmpl.py
1 # Copyright (C) 2009-2016  EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 try:
21   from string import Template
22 except:
23   from compat import Template,set
24
25 pyCompo="""
26 import sys,traceback,os
27 sys.path=sys.path+[${python_path}]
28 import ${module}_ORB__POA
29 import calcium
30 import dsccalcium
31 import SALOME
32 import Engines
33 import cPickle
34
35 try:
36   import numpy
37 except:
38   numpy=None
39
40 #COMPODEFS
41 ${compodefs}
42 #ENDDEF
43
44 #DEFS
45 ${servicesdef}
46 #ENDDEF
47
48 class ${component}(${module}_ORB__POA.${component}, ${inheritedclass} dsccalcium.PyDSCComponent):
49   '''
50      To be identified as a SALOME component this Python class
51      must have the same name as the component, inherit omniorb
52      class ${module}_ORB__POA.${component} and DSC class dsccalcium.PyDSCComponent
53      that implements DSC API.
54   '''
55   def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
56     dsccalcium.PyDSCComponent.__init__(self, orb, poa,contID,containerName,instanceName,interfaceName)
57 ${callconstructor}
58
59   def init_service(self,service):
60 ${initservice}
61     return False
62
63 ${servicesimpl}
64 """
65
66 pyCompoEXE="""#!/usr/bin/env python
67 """+pyCompo+"""
68   def destroy(self):
69      self._orb.shutdown(0)
70
71 if __name__ == '__main__':
72   from omniORB import CORBA
73   print sys.argv
74   orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
75   poa = orb.resolve_initial_references("RootPOA")
76   print "ORB and POA initialized",orb,poa
77   sys.stdout.flush()
78   sys.stderr.flush()
79
80   container=orb.string_to_object(sys.argv[1])
81   containerName=sys.argv[2]
82   instanceName=sys.argv[3]
83
84   compo=${component}(orb,poa,container,containerName, instanceName, "${component}")
85   comp_o = compo._this()
86   comp_iors = orb.object_to_string(comp_o)
87   print "ior ${component}",comp_iors
88
89   sys.stdout.flush()
90   sys.stderr.flush()
91
92   #activate the POA
93   poaManager = poa._get_the_POAManager()
94   poaManager.activate()
95
96   orb.run()
97   print "fin du composant ${component} standalone"
98
99 """
100
101 pyCompo=Template(pyCompo)
102 pyCompoEXE=Template(pyCompoEXE)
103
104 pyService="""
105   def ${service}(self,${inparams}):
106     self.beginService("${component}.${service}")
107     component=self.proxy
108     returns=None
109     try:
110 ${convertinparams}
111 #BODY
112 ${body}
113 #ENDBODY
114       sys.stdout.flush()
115       self.endService("${component}.${service}")
116 ${convertoutparams}
117       return ${outparams}
118     except:
119       sys.stdout.flush()
120       exc_typ,exc_val,exc_fr=sys.exc_info()
121       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
122       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"${component}.py",0)) """
123 pyService=Template(pyService)
124
125 pyinitService="""    if service == "${service}":
126        #initialization CALCIUM ports IN
127 ${instream}
128        #initialization CALCIUM ports OUT
129 ${outstream}
130        return True """
131 pyinitService=Template(pyinitService)
132 pyinitCEXEService=pyinitService
133 pyinitEXEService=pyinitService
134
135 # CMakeLists.txt in src/<component> for a python component
136 # template parameters:
137 #   sources: source files, separated by spaces
138 cmake_src_compo_py="""
139 # scripts / static
140 SET(_bin_SCRIPTS
141   ${sources}
142 )
143
144 # --- rules ---
145 SALOME_INSTALL_SCRIPTS("$${_bin_SCRIPTS}" $${SALOME_INSTALL_SCRIPT_PYTHON})
146 """
147 cmake_src_compo_py=Template(cmake_src_compo_py)