38 return "Python component code generator" 42 Python generator specific usage 45 ---------------------------------- 46 Help for Python code generator 47 ---------------------------------- 48 Python code generator generates the following files. 49 [Component name].py............Component class and executable 50 README.[Component name]........Specification template of the component 52 No additional options are available for Python code generator. 61 [for service_idl]from [service_idl.idl_basename]_idl_example import * 64 consumer_import =
"""\ 65 import _GlobalIDL, _GlobalIDL__POA 68 initialize_configuration_param =
"""\ 69 [for config]self._[config.name] = [config.default_data] 73 [l_name]_spec = ["implementation_id", "[module.name]", 74 "type_name", "[module.name]", 75 "description", "[module.desc]", 76 "version", "[module.version]", 77 "vendor", "[module.vendor]", 78 "category", "[module.category]", 79 "activity_type", "[module.comp_type]", 80 "max_instance", "[module.max_inst]", 82 "lang_type", "SCRIPT", 83 [for config] "conf.default.[config.name]", "[config.default]", 89 py_source =
"""#!/usr/bin/env python 100 # Import Service implementation class 101 # <rtc-template block="service_impl"> 104 # Import Service stub modules 105 # <rtc-template block="consumer_import"> 109 # This module's spesification 110 # <rtc-template block="module_spec"> 113 class [module.name](OpenRTM_aist.DataFlowComponentBase): 114 def __init__(self, manager): 115 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager) 117 [for inport]self._d_[inport.name] = RTC.[inport.type]([inport.data_type_args]) 118 self._[inport.name]In = OpenRTM_aist.InPort("[inport.name]", self._d_[inport.name]) 119 [end][for outport]self._d_[outport.name] = RTC.[outport.type]([outport.data_type_args]) 120 self._[outport.name]Out = OpenRTM_aist.OutPort("[outport.name]", self._d_[outport.name]) 122 [for corbaport]self._[corbaport.name]Port = OpenRTM_aist.CorbaPort("[corbaport.name]") 124 [for service]self._[service.name] = [service.type]_i() 126 [for consumer]self._[consumer.name] = OpenRTM_aist.CorbaConsumer(interfaceType=_GlobalIDL.[consumer.type]) 128 # initialize of configuration-data. 129 # <rtc-template block="init_conf_param"> 134 def onInitialize(self): 135 # Bind variables and configuration variable 136 [for config]self.bindParameter("[config.name]", self._[config.name], "[config.default]") 140 [for inport]self.addInPort("[inport.name]",self._[inport.name]In) 142 # Set OutPort buffers 143 [for outport]self.addOutPort("[outport.name]",self._[outport.name]Out) 146 # Set service provider to Ports 147 [for service]self._[service.port]Port.registerProvider("[service.name]", "[service.type]", self._[service.name]) 149 # Set service consumers to Ports 150 [for consumer]self._[consumer.port]Port.registerConsumer("[consumer.name]", "[consumer.type]", self._[consumer.name]) 152 # Set CORBA Service Ports 153 [for corbaport]self.addPort(self._[corbaport.name]Port) 160 #def [activity.name](self, ec_id): 166 def [module.name]Init(manager): 167 profile = OpenRTM_aist.Properties(defaults_str=[l_name]_spec) 168 manager.registerFactory(profile, 172 def MyModuleInit(manager): 173 [module.name]Init(manager) 176 comp = manager.createComponent("[module.name]") 181 mgr = OpenRTM_aist.Manager.init(sys.argv) 182 mgr.setModuleInitProc(MyModuleInit) 183 mgr.activateManager() 186 if __name__ == "__main__": 199 acts = ((
"onFinalize",
""), \
200 (
"onStartup",
"RTC::UniqueId ec_id"), \
201 (
"onShutdown",
"RTC::UniqueId ec_id"), \
202 (
"onActivated",
"RTC::UniqueId ec_id"), \
203 (
"onDeactivated",
"RTC::UniqueId ec_id"), \
204 (
"onExecute",
"RTC::UniqueId ec_id"), \
205 (
"onAborting",
"RTC::UniqueId ec_id"), \
206 (
"onError",
"RTC::UniqueId ec_id"), \
207 (
"onReset",
"RTC::UniqueId ec_id"), \
208 (
"onStateUpdate",
"RTC::UniqueId ec_id"), \
209 (
"onRateChanged",
"RTC::UniqueId ec_id"))
211 for name, args
in acts:
217 dict[
"activity"] = actlist
221 impl_suffix =
"SVC_impl" 224 for opt, arg
in opts:
225 if opt.find(
"--svc-impl-suffix") == 0:
227 if opt.find(
"--svc-skel-suffix") == 0:
229 if opt.find(
"--svc-stub-suffix") == 0:
231 dict[
"impl_suffix"] = impl_suffix
232 dict[
"skel_suffix"] = skel_suffix
233 dict[
"stub_suffix"] = stub_suffix
238 Python component source code generation class 244 self.
data[
'fname_py'] = self.
data[
'fname'] +
".py" 245 self.
data[
"begin_brace"] =
"[" 246 self.
data[
"end_brace"] =
"]" 247 self.
data[
"u_name"] = self.
data[
"module"].name.upper()
248 self.
data[
"l_name"] = self.
data[
"module"].name.lower()
252 if self.
data[
"service_idl"]:
253 for svc
in self.
data[
"service_idl"]:
254 svc.impl_py = svc.idl_basename +
"_idl_example.py" 255 self.
tags[
"service_impl"] = service_impl
257 if self.
data[
"consumer_idl"]:
258 for cons
in self.
data[
"consumer_idl"]:
260 cons.modulename =
"_GlobalIDL" 261 f = open(cons.idl_fname,
'a+')
266 mod_idx = _str.find(
"module",0)
268 _str = _str[mod_idx+6:]
269 idx = _str.find(
"{",0)
272 cons.modulename = string.strip(_str)
276 print "Can't find file:", file
278 self.
tags[
"consumer_import"] = consumer_import
280 if self.
data[
"config"]:
281 for i
in range(len(self.
data[
"config"])):
282 split_data = self.
data[
"config"][i].default.split(
",")
283 if len(split_data) > 1:
287 _data.append(_type(d))
288 self.
data[
"config"][i].default_data = [_data]
291 self.
data[
"config"][i].default_data = [_type(self.
data[
"config"][i].default)]
293 self.
tags[
"init_conf_param"] = initialize_configuration_param
295 if self.
data[
"inport"]:
296 for inp
in self.
data[
"inport"]:
298 inp.data_type_args =
"RTC.Time(0,0),[]" 300 inp.data_type_args =
"RTC.Time(0,0),0" 302 if self.
data[
"outport"]:
303 for outp
in self.
data[
"outport"]:
305 outp.data_type_args =
"RTC.Time(0,0),[]" 307 outp.data_type_args =
"RTC.Time(0,0),0" 311 self.
tags[
"module_spec"] = module_spec
317 if str(_type)
in [
"TimedShortSeq",
"TimedLongSeq",
"TimedUShortSeq",
318 "TimedULongSeq",
"TimedFloatSeq",
"TimedDoubleSeq",
319 "TimedCharSeq",
"TimedBooleanSeq",
"TimedOctetSeq",
327 if str(_type) ==
"int":
329 elif str(_type) ==
"long":
331 elif str(_type) ==
"float":
333 elif str(_type) ==
"double":
335 elif str(_type) ==
"string":
342 for svc_idl
in self.
data[
"service_idl"]:
343 if not os.access(svc_idl.idl_fname, os.F_OK):
344 sys.stderr.write(
"Error: IDL file \"" \
345 + svc_idl.idl_fname \
350 cmd =
"omniidl -bpython -Wbexample "+svc_idl.idl_fname
353 sys.stderr.write(
"Generate error: " \
354 + svc_idl.impl_py +
"\n")
359 +
"\" was generated." 361 for cons
in self.
data[
"consumer_idl"]:
363 for svc
in self.
data[
"service_idl"]:
364 if cons.idl_fname == svc.idl_fname:
368 if not os.access(cons.idl_fname, os.F_OK):
369 sys.stderr.write(
"Error: IDL file \"" \
375 cmd =
"omniidl -bpython "+cons.idl_fname
378 sys.stderr.write(
"Generate error: omniidl -bpython "+cons.idl_fname)
383 Generate component class script
def MakeActivityFuncs(dict)
def gen(self, fname, temp_txt, data, tags)
def __init__(self, data, opts)
def get_type(self, _type)
def check_data_type(self, _type)
def MakeSuffix(opts, dict)