1 # Copyright (C) 2009-2014 EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Module that defines PYComponent for SALOME components implemented in Python
24 from gener import Component, Invalid
25 from pyth_tmpl import pyinitService, pyService, pyCompoEXE, pyCompo, cmake_src_compo_py
27 from string import split,rstrip,join
29 def indent(text, prefix=' '):
30 """Indent text by prepending a given prefix to each line."""
31 if not text: return ''
32 lines = split(text, '\n')
33 lines = map(lambda line, prefix=prefix: prefix + line, lines)
34 if lines: lines[-1] = rstrip(lines[-1])
35 return join(lines, '\n')
37 class PYComponent(Component):
39 A :class:`PYComponent` instance represents a Python SALOME component with services given as a list of :class:`Service`
40 instances with the parameter *services*.
42 :param name: gives the name of the component.
44 :param services: the list of services (:class:`Service`) of the component.
45 :param kind: If it is given and has the value "exe", the component will be built as a standalone
46 component (python executable). The default is to build the component as a python module.
47 :param sources: gives all the external Python source files to add in the component directory (list of paths).
48 :param python_path: If it is given (as a list of paths), all the paths are added to the python path (sys.path).
49 :param compodefs: can be used to add extra definition code in the component for example when using a base class
50 to define the component class by deriving it (see *inheritedclass* parameter)
51 :param inheritedclass: can be used to define a base class for the component. The base class can be defined in external
52 source or with the *compodefs* parameter. The value of the *inheritedclass* parameter is the name of the base class.
53 :param idls: can be used to add extra idl CORBA interfaces. This parameter must gives a list of idl file names that are
54 added into the generated module (idl directory) and compiled with the generated idl of the module.
55 :param interfacedefs: can be used to add idl definitions (or includes of idl files) into the generated idl of the module.
56 :param inheritedinterface: can be used to make the component inherit an extra idl interface that has been included through
57 the *idls* and *interfacedefs* parameters. See the pygui1 example for how to use these last parameters.
59 For example, the following call defines a Python component named "mycompo" with one service s1 (it must have been defined before)::
61 >>> c1 = module_generator.PYComponent('mycompo', services=[s1,],
65 def __init__(self, name, services=None, kind="lib", sources=None, python_path=None,
66 compodefs="", inheritedclass="", idls=None, interfacedefs="", inheritedinterface=""):
67 """initialise component attributes"""
68 self.python_path = python_path or []
69 Component.__init__(self, name, services, impl="PY", kind=kind, sources=sources,
70 inheritedclass=inheritedclass, compodefs=compodefs,
71 idls=idls,interfacedefs=interfacedefs,inheritedinterface=inheritedinterface)
74 """validate component attributes"""
75 Component.validate(self)
77 if self.kind not in kinds:
78 raise Invalid("kind must be one of %s for component %s" % (kinds,self.name))
80 def libraryName(self):
81 """ Name of the target library
82 No library for a python component
86 def makeCompo(self, gen):
87 """generate component sources as a dictionary containing
88 file names (key) and file content (values)
92 if self.kind == "lib":
93 pyfile = self.name + ".py"
94 file_content = self.makepy(gen)
95 elif self.kind == "exe":
96 pyfile = self.name + ".exe"
97 file_content = self.makepyexe(gen)
99 raise Invalid("Invalid kind ()%s for component %s" % (
100 self.kind, self.name))
102 sources = pyfile + "".join(map(lambda x: "\n " + os.path.basename(x),
104 cmake_content = cmake_src_compo_py.substitute(sources=sources)
106 return {"CMakeLists.txt":cmake_content,
110 def makepy(self, gen):
111 """generate standard SALOME component source (python module)"""
115 for serv in self.services:
116 defs.append(serv.defs)
119 for name, typ in serv.inport:
120 if typ=="file":continue #files are not passed through service interface
123 pyparams.append(" %s=cPickle.loads(%s)" %(name, name))
124 inparams = ",".join(params)
125 convertinparams = '\n'.join(pyparams)
129 for name, typ in serv.outport:
130 if typ=="file":continue #files are not passed through service interface
133 pyparams.append(" %s=cPickle.dumps(%s,-1)" %(name, name))
134 outparams = ",".join(params)
135 convertoutparams = '\n'.join(pyparams)
136 #dedent and indent the body
137 body=textwrap.dedent(serv.body)
138 body=indent(body,' '*6)
140 service = pyService.substitute(component=self.name, service=serv.name, inparams=inparams,
141 outparams=outparams, body= body,
142 convertinparams=convertinparams,
143 convertoutparams=convertoutparams)
145 for name, typ, dep in serv.instream:
146 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","IN","%s")'% (name, typ, dep))
147 instream = "\n".join(streams)
149 for name, typ, dep in serv.outstream:
150 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","OUT","%s")'% (name, typ, dep))
151 outstream = "\n".join(streams)
153 init = pyinitService.substitute(component=self.name, service=serv.name,
154 instream=instream, outstream=outstream)
155 services.append(service)
158 python_path = ",".join([repr(p) for p in self.python_path])
160 inheritedclass=self.inheritedclass
162 if self.inheritedclass:
163 inheritedclass= self.inheritedclass + ","
165 if hasattr(%s,"__init__"):
166 %s.__init__(self)""" % (self.inheritedclass,self.inheritedclass)
168 return pyCompo.substitute(component=self.name, module=gen.module.name,
169 servicesdef="\n".join(defs), servicesimpl="\n".join(services),
170 initservice='\n'.join(inits),
171 python_path=python_path,inheritedclass=inheritedclass,
172 compodefs=self.compodefs, callconstructor=callconstructor)
174 def makepyexe(self, gen):
175 """generate standalone component source (python executable)"""
179 for serv in self.services:
180 defs.append(serv.defs)
183 for name, typ in serv.inport:
184 if typ=="file":continue #files are not passed through service interface
187 pyparams.append(" %s=cPickle.loads(%s)" %(name, name))
188 inparams = ",".join(params)
189 convertinparams = '\n'.join(pyparams)
193 for name, typ in serv.outport:
194 if typ=="file":continue #files are not passed through service interface
197 pyparams.append(" %s=cPickle.dumps(%s,-1)" %(name, name))
198 outparams = ",".join(params)
199 convertoutparams = '\n'.join(pyparams)
200 #dedent and indent the body
201 body=textwrap.dedent(serv.body)
202 body=indent(body,' '*6)
203 service = pyService.substitute(component=self.name, service=serv.name,
204 inparams=inparams, outparams=outparams,
206 convertinparams=convertinparams,
207 convertoutparams=convertoutparams,
210 for name, typ, dep in serv.instream:
211 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","IN","%s")'% (name, typ, dep))
212 instream = "\n".join(streams)
214 for name, typ, dep in serv.outstream:
215 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","OUT","%s")'% (name, typ, dep))
216 outstream = "\n".join(streams)
218 init = pyinitService.substitute(component=self.name, service=serv.name,
219 instream=instream, outstream=outstream)
220 services.append(service)
223 python_path = ",".join([repr(p) for p in self.python_path])
225 inheritedclass=self.inheritedclass
227 if self.inheritedclass:
228 inheritedclass= self.inheritedclass + ","
230 if hasattr(%s,"__init__"):
231 %s.__init__(self)""" % (self.inheritedclass,self.inheritedclass)
233 return pyCompoEXE.substitute(component=self.name, module=gen.module.name,
234 servicesdef="\n".join(defs),
235 servicesimpl="\n".join(services),
236 initservice='\n'.join(inits),
237 python_path=python_path,inheritedclass=inheritedclass,
238 compodefs=self.compodefs, callconstructor=callconstructor)