def create_proxies(self): result = "" for iface, num in get_name_number(self.component.requires): if communication_is_ice(iface): module = self.component.idsl_pool.module_providing_interface( iface.name) proxy_type = iface.name if module is not None: proxy_type = f"{module['name']}::{iface.name}" if self.component.language.lower() == "cpp": result += f"{proxy_type}Prx {iface.name.lower()}{num}_proxy;\n" else: result += f"{proxy_type}PrxPtr {iface.name.lower()}{num}_proxy;\n" for iface, num in get_name_number(self.component.publishes): if communication_is_ice(iface): module = self.component.idsl_pool.module_providing_interface( iface.name) proxy_type = iface.name if module is not None: proxy_type = f"{module['name']}::{iface.name}" if self.component.language.lower() == "cpp": result += f"{proxy_type}Prx {iface.name.lower()}{num}_pubproxy;\n" else: result += f"{proxy_type}PrxPtr {iface.name.lower()}{num}_pubproxy;\n" return result
def require_and_publish_proxies_creation(self): result = "" cont = 0 for interface, num in get_name_number(self.component.requires): if communication_is_ice(interface): name = interface.name if self.component.language.lower() == 'cpp': prx_type = name if prx_type not in CPP_TYPES and '::' not in prx_type: module = self.component.idsl_pool.module_providing_interface(name) prx_type = f"{module['name']}::{prx_type}" result += name.lower() + num + "_proxy = (*(" + prx_type + "Prx*)mprx[\"" + name + "Proxy" + num + "\"]);\n" else: result += name.lower() + num + "_proxy = std::get<" + str(cont) + ">(tprx);\n" cont = cont + 1 for interface, num in get_name_number(self.component.publishes): if communication_is_ice(interface): name = interface.name prx_type = name if prx_type not in CPP_TYPES and '::' not in prx_type: module = self.component.idsl_pool.module_providing_interface(name) prx_type = f"{module['name']}::{prx_type}" if self.component.language.lower() == 'cpp': result += name.lower() + num + "_pubproxy = (*(" + prx_type + "Prx*)mprx[\"" + name + "Pub" + num + "\"]);\n" else: result += name.lower() + num + "_pubproxy = std::get<" + str(cont) + ">(tprx);\n" cont = cont + 1 return result
def test_communication_is_ice(self): self.assertTrue(parsing_utils.communication_is_ice("CameraSimple")) self.assertTrue(parsing_utils.communication_is_ice("Ca")) self.assertTrue( parsing_utils.communication_is_ice(["CameraSimple", "ice"])) self.assertFalse( parsing_utils.communication_is_ice(["CameraSimple", "ros"])) self.assertRaises(ValueError, parsing_utils.communication_is_ice, ["CameraSimple", "nus"]) self.assertTrue(parsing_utils.communication_is_ice(["CameraSimple"]))
def storm_topic_manager_creation(self): result = "" need_storm = False for pub in self.component.publishes: if communication_is_ice(pub): need_storm = True for sub in self.component.subscribesTo: if communication_is_ice(sub): need_storm = True if need_storm: result += TOPIC_MANAGER_STR return result
def subscribes_to(self): result = "" for interface, num in get_name_number(self.component.subscribesTo): name = interface.name if communication_is_ice(interface): if self.component.language.lower() == "cpp": change1 = "IceStorm::TopicPrx" change2 = "Ice::ObjectPrx" change3 = " new <NORMAL>I" change4 = "Ice::ObjectPrx" else: change1 = "std::shared_ptr<IceStorm::TopicPrx>" change2 = "Ice::ObjectPrxPtr" change3 = " std::make_shared <<NORMAL>I>" change4 = "auto" module = self.component.idsl_pool.module_providing_interface( name) proxy_type = utils.get_type_string(name, module['name']) result += SUBSCRIBESTO_STR.replace( "<CHANGE1>", change1).replace("<CHANGE2>", change2).replace( "<CHANGE3>", change3).replace("<CHANGE4>", change4).replace( "<NORMAL>", name).replace("<LOWER>", name.lower()).replace( "<PROXYNAME>", name.lower() + num).replace( "<PROXYNUMBER>", num).replace("<PTR_TYPE>", proxy_type) return result
def publish(self): result = "" for pba in self.component.publishes: if type(pba) == str: pb = pba else: pb = pba[0] if communication_is_ice(pba): if self.component.language.lower() == "cpp": result += "IceStorm::TopicPrx " + pb.lower() + "_topic;\n" else: result += "std::shared_ptr<IceStorm::TopicPrx> " + pb.lower( ) + "_topic;\n" result += PUBLISHES_STR.replace("<NORMAL>", pb).replace( "<LOWER>", pb.lower()) module = self.component.idsl_pool.module_providing_interface( pb) proxy_type = utils.get_type_string(pb, module['name']) if self.component.language.lower() == "cpp": result += "Ice::ObjectPrx " + pb.lower( ) + "_pub = " + pb.lower( ) + "_topic->getPublisher()->ice_oneway();\n" result += "" + pb.lower( ) + "_pubproxy = " + proxy_type + "Prx::uncheckedCast(" + pb.lower( ) + "_pub);\n" result += "mprx[\"" + pb + "Pub\"] = (::IceProxy::Ice::Object*)(&" + pb.lower( ) + "_pubproxy);\n" else: result += "auto " + pb.lower() + "_pub = " + pb.lower( ) + "_topic->getPublisher()->ice_oneway();\n" result += "" + pb.lower( ) + "_pubproxy = Ice::uncheckedCast<" + pb + "Prx>(" + pb.lower( ) + "_pub);\n" return result
def interface_sources(self): result = "" # TODO: refactor in one loop for ima in self.component.implements: if type(ima) == str: im = ima else: im = ima[0] if communication_is_ice(ima): result += im.lower() + 'I.cpp\n' for subscribe in self.component.subscribesTo: interface_name = subscribe.name if communication_is_ice(subscribe): result += interface_name.lower() + 'I.cpp\n' return result
def subscribes_method_definitions(self): result = "" pool = self.component.idsl_pool for impa in self.component.subscribesTo: if type(impa) == str: imp = impa else: imp = impa.name module = pool.module_providing_interface(imp) for interface in module['interfaces']: if interface['name'] == imp: for mname in interface['methods']: method = interface['methods'][mname] param_str_a = '' if p_utils.communication_is_ice(impa): param_str_a = utils.get_parameters_string( method, module['name'], self.component.language) return_type = utils.get_type_string( method['return'], module['name']) result += return_type + ' ' + interface[ 'name'] + "_" + method[ 'name'] + '(' + param_str_a + ");\n" else: pass return result
def generate_files(self, output_path): # # Generate regular files # new_existing_files = {} for template_file in self.files['regular']: if self._pre_generation_check(template_file): continue if template_file == 'README-RCNODE.txt' and not self._need_storm(): continue if template_file == 'src/mainUI.ui' and self.ast.gui is None: continue ofile = self._output_file_rename(output_path, template_file) if template_file in self.files['avoid_overwrite'] and os.path.exists(ofile): console.print(':eye: Not overwriting specific file "' + ofile + '", saving it to ' + ofile + '.new', style='yellow') new_existing_files[os.path.abspath(ofile)] = os.path.abspath(ofile) + '.new' ofile += '.new' ifile = os.path.join(TEMPLATES_DIR, self.files['template_path'], template_file) console.print(f":thumbs_up: Generating {ofile}", style='green') try: self._template_to_file(ifile, ofile) except ValueError as e: console.print(e) self._post_generation_action(template_file, ofile) for interface in self.ast.implements + self.ast.subscribesTo: if communication_is_ice(interface): for template_file in self.files['servant_files']: ofile = os.path.join(output_path, 'src', interface.name.lower() + 'I.' + template_file.split('.')[ -1].lower()) console.print(':thumbs_up: Generating %s (servant for %s)' % (ofile, interface.name), style='green') ifile = os.path.join(TEMPLATES_DIR, self.files['template_path'], template_file) self._template_to_file(ifile, ofile, interface.name) return new_existing_files
def subscribes(self): result = "" pool = self.component.idsl_pool for subscribes in self.component.subscribesTo: module = pool.module_providing_interface(subscribes.name) if module is None: raise ValueError('\nCan\'t find module providing %s\n' % subscribes.name) for interface in module['interfaces']: if interface['name'] == subscribes.name: for mname in interface['methods']: method = interface['methods'][mname] param_str_a = '' body_code = self.body_code_from_name(method['name']) if p_utils.communication_is_ice(subscribes): param_str_a = utils.get_parameters_string( method, module['name'], self.component.language) result += "//SUBSCRIPTION to " + method[ 'name'] + " method from " + interface[ 'name'] + " interface\n" result += method['return'] + ' SpecificWorker::' + interface[ 'name'] + "_" + method[ 'name'] + '(' + param_str_a + ")\n{\n//subscribesToCODE\n" + body_code + "\n}\n\n" else: pass return result
def implements(self): result = "" pool = self.component.idsl_pool for impa in self.component.implements: if type(impa) == str: imp = impa else: imp = impa[0] module = pool.module_providing_interface(imp) for interface in module['interfaces']: if interface['name'] == imp: for mname in interface['methods']: method = interface['methods'][mname] param_str_a = '' body_code = self.body_code_from_name(method['name']) if p_utils.communication_is_ice(impa): param_str_a = utils.get_parameters_string( method, module['name'], self.component.language) return_type = utils.get_type_string( method['return'], module['name']) result += return_type + ' SpecificWorker::' + interface[ 'name'] + "_" + method[ 'name'] + '(' + param_str_a + ")\n{\n//implementCODE\n" + body_code + "\n}\n\n" else: pass return result
def unsubscribe_code(self): result = "\n" for interface in self.component.subscribesTo: if communication_is_ice(interface): result = Template(UNSUBSCRIBE_STR).substitute( name=interface.name.lower()) return result
def implements_adapters_creation(self): result = "" for iface in self.component.implements: if communication_is_ice(iface): name = iface[0] result += Template(IMPLEMENTS_STR).substitute( iface_name=name, iface_name_lower=name.lower()) return result
def subscribes_adapters_creation(self): result = "" for sut in self.component.subscribesTo: if communication_is_ice(sut): name = sut[0] result += Template(SUBSCRIBESTO_STR).substitute( iface_name=name, iface_name_lower=name.lower()) return result
def config_implements_endpoints(self): result = "" for interface in self.component.implements: if communication_is_ice(interface): result += interface.name + ".Endpoints=tcp -p 0\n" if result != "": result = '# Endpoints for implements interfaces\n' + result + '\n\n' return result
def config_subscribes_endpoints(self): result = "" for interface in self.component.subscribesTo: if communication_is_ice(interface): result += interface.name + "Topic.Endpoints=tcp -p 0\n" if result != "": result = '# Endpoints for subscriptions interfaces\n' + result + '\n\n' return result
def config_implements_endpoints(self): result = "" for interface in self.component.implements: if communication_is_ice(interface): port = get_existing_port(interface.name) result += interface.name + f".Endpoints=tcp -p {port}\n" if result != "": result = '# Endpoints for implements interfaces\n' + result + '\n\n' return result
def topic_manager_creation(self): result = "" need_topic = False for pub in self.component.publishes: if communication_is_ice(pub): need_topic = True for pub in self.component.subscribesTo: if communication_is_ice(pub): need_topic = True if need_topic: if self.component.language.lower() == "cpp": ptr = "" manager_type = "IceStorm::TopicManagerPrx::checkedCast" else: ptr = "Ptr" manager_type = "topicManager = Ice::checkedCast<IceStorm::TopicManagerPrx>" result += Template(TOPIC_MANAGER_STR).substitute(ptr=ptr, type=manager_type) return result
def config_requires_proxies(self): result = "" for interface, num in get_name_number(self.component.requires): if communication_is_ice(interface): port = 0 if interface.name == 'DifferentialRobot': port = 10004 if interface.name == 'Laser': port = 10003 result += interface.name + num + "Proxy = " + interface.name.lower( ) + ":tcp -h localhost -p " + str(port) + "\n" if result != "": result = '# Proxies for required interfaces\n' + result + '\n\n' return result
def requires_proxies(self): result = "" for req, num in get_name_number(self.component.requires): if isinstance(req, str): rq = req else: rq = req[0] if communication_is_ice(req): result += "self." + rq.lower() + num + "_proxy = mprx[\"" + rq + "Proxy" + num + "\"]\n" else: result += "self." + rq.lower() + "_proxy = ServiceClient" + rq + "()\n" return result
def publishes_proxies(self): result = "" for pb, num in get_name_number(self.component.publishes): if isinstance(pb, str): pub = pb else: pub = pb[0] if communication_is_ice(pb): result += "self." + pub.lower() + num + "_proxy = mprx[\"" + pub + "Pub" + num + "\"]\n" else: result += "self." + pub.lower() + "_proxy = Publisher" + pub + "()\n" return result
def interface_includes(interfaces, suffix='', lower=False): result = "" for interface in sorted(interfaces): if communication_is_ice(interface): name = interface if isinstance(interface, str) else interface.name name = name.split('/')[-1].split('.')[0] if lower: name = name.lower() result += Template(INCLUDE_STR).substitute(iface_name=name, suffix=suffix) return result
def config_subscribes_endpoints(self): result = "" for interface in self.component.subscribesTo: if communication_is_ice(interface): if RCPortChecker: port = get_existing_port(interface.name) else: port = random.randint(10001, 19000) result += interface.name + f"Topic.Endpoints=tcp -p {port}\n" if result != "": result = '# Endpoints for subscriptions interfaces\n' + result + '\n\n' return result
def publish_proxy_creation(self): result = "" for iface, num in get_name_number(self.component.publishes): if communication_is_ice(iface): name = iface.name module = self.component.idsl_pool.module_providing_interface( iface.name) result += Template(PUBLISHES_STR).substitute( iface_name=name, iface_name_lower=name.lower(), module_name=module['name']) return result
def require_proxy_creation(self): result = "" for iface, num in get_name_number(self.component.requires): if communication_is_ice(iface): name = iface.name module = self.component.idsl_pool.module_providing_interface( iface.name) result += Template(REQUIRE_STR).substitute( iface_name=name, module_name=module['name'], iface_name_lower=name.lower(), num=num) return result
def config_requires_proxies(self): result = "" for interface, num in get_name_number(self.component.requires): if communication_is_ice(interface): port = 0 if interface.name == 'DifferentialRobot': port = 10004 elif interface.name == 'Laser': port = 10003 elif RCPortChecker: port = get_existing_port(interface.name) else: port = random.randint(10001, 19000) result += interface.name + num + "Proxy = " + interface.name.lower( ) + ":tcp -h localhost -p " + str(port) + "\n" if result != "": result = '# Proxies for required interfaces\n' + result + '\n\n' return result
def implements(self): result = "" for ima in self.component.implements: if type(ima) == str: im = ima else: im = ima[0] if communication_is_ice(ima): if self.component.language.lower() == "cpp": cpp = "<NORMAL>I *<LOWER> = new <NORMAL>I(worker);" else: cpp = "auto <LOWER> = std::make_shared<<NORMAL>I>(worker);" result += IMPLEMENTS_STR.replace("<C++_VERSION>", cpp).replace( "<NORMAL>", im).replace("<LOWER>", im.lower()) return result
def implements(self): result = "" for iface in self.component.implements: pool = self.component.idsl_pool module = pool.module_providing_interface(iface.name) for interface in module['interfaces']: if interface['name'] == iface.name: for mname in interface['methods']: method = interface['methods'][mname] param_str_a = '' if communication_is_ice(iface): param_str_a = utils.get_parameters_string(method, module['name'], self.component.language) return_type = utils.get_type_string(method['return'], module['name']) result += f"virtual {return_type} {interface['name']}_{method['name']}({param_str_a}) = 0;\n" else: pass return result
def proxy_ptr(self, interfaces, prefix=''): result = "" for interface, num in get_name_number(interfaces): if communication_is_ice(interface): ptr = "" if self.component.language.lower() != "cpp": ptr = "Ptr" name = interface.name module = self.component.idsl_pool.module_providing_interface( name) proxy_type = utils.get_type_string(name, module['name']) result += Template(PROXY_PTR_STR).substitute( prx_type=proxy_type, ptr=ptr, lower=name.lower(), num=num, prefix=prefix) return result
def subscribes(self): result = "" for iface in self.component.subscribesTo: pool = self.component.idsl_pool module = pool.module_providing_interface(iface.name) if module is None: raise ValueError('\nCan\'t find module providing %s \n' % iface.name) for interface in module['interfaces']: if interface['name'] == iface.name: for mname in interface['methods']: method = interface['methods'][mname] param_str_a = '' if communication_is_ice(iface): param_str_a = utils.get_parameters_string(method, module['name'], self.component.language) return_type = utils.get_type_string(method['return'], module['name']) result += f"virtual {return_type} {interface['name']}_{method['name']} ({param_str_a}) = 0;\n" else: pass return result