def process_all_messages(out_dir, packages_to_process=[]): '''Process all available ROS messages and generate ASN.1 types. For each package, one .asn file is created with one type per message. Additionally, if the message contains variable-sized elements, a userdefs-*.asn file is created with parametrized size defaults. ''' rospack = rospkg.RosPack() asn_template = load_template('package.asn.mako') userdefs_template = load_template('userdefs.asn.mako') if not os.path.exists(out_dir): os.makedirs(out_dir) # Find packages with messages and services existing_packages = [ pkg for pkg, _ in rosmsg.iterate_packages(rospack, rosmsg.MODE_MSG) ] for pkg, _ in rosmsg.iterate_packages(rospack, rosmsg.MODE_SRV): if pkg not in existing_packages: existing_packages.append(pkg) existing_packages = sorted(existing_packages) #check that packages given in input exists if packages_to_process: for pkg in packages_to_process: if pkg not in existing_packages: print("process_all_messages : input package {} does not exist". format(pkg)) sys.exit(-1) #if no package specified, process all of them else: print("Processing all packages") packages_to_process = existing_packages for pkg in packages_to_process: try: print('Creating ASN.1 types for {}'.format(pkg)) pkg_obj = RosAsn1Generator(rospack, pkg) # ASN.1 types "pkg.asn" asn_txt = asn_template.render(pkg=pkg_obj) except: print( "Error occured while processing {} package. Unabled to convert to ASN1" .format(pkg)) continue out_file1 = os.path.join(out_dir, pkg + '.asn') with open(out_file1, 'w') as fd: fd.write(asn_txt) # Size constants for variable-sized types "userdefs-pkg.asn" userdefs_txt = userdefs_template.render(pkg=pkg_obj) out_file2 = os.path.join(out_dir, 'userdefs-' + pkg + '.asn') with open(out_file2, 'w') as fd: fd.write(userdefs_txt) return
def _update_thread_run(self): # update type_combo_box message_type_names = [] try: # this only works on fuerte and up packages = sorted([ pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages( self._rospack, rosmsg.MODE_MSG) ]) except: # this works up to electric packages = sorted(rosmsg.list_packages()) for package in packages: for base_type_str in rosmsg.list_msgs(package, rospack=self._rospack): message_class = roslib.message.get_message_class(base_type_str) if message_class is not None: message_type_names.append(base_type_str) # TODO: get all ROS packages and launch files here instead #self.type_combo_box.setItems.emit(sorted(message_type_names)) packages = sorted([ pkg_tuple[0] for pkg_tuple in RqtRoscommUtil.iterate_packages('launch') ]) self.package_combo_box.setItems.emit(packages)
def _update_thread_run(self): # update type_combo_box message_type_names = [] try: # this only works on fuerte and up packages = sorted([ pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages( self._rospack, rosmsg.MODE_MSG) ]) except: # this works up to electric packages = sorted(rosmsg.list_packages()) for package in packages: for base_type_str in rosmsg.list_msgs(package, rospack=self._rospack): message_class = roslib.message.get_message_class(base_type_str) if message_class is not None: message_type_names.append(base_type_str) self.type_combo_box.setItems.emit(sorted(message_type_names)) # update topic_combo_box _, _, topic_types = rospy.get_master().getTopicTypes() self._topic_dict = dict(topic_types) self.topic_combo_box.setItems.emit(sorted(self._topic_dict.keys()))
def _refresh_packages(self): rospack = rospkg.RosPack() packages = sorted([pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages(rospack, self._mode)]) self._package_list = packages self.package_combo.clear() self.package_combo.addItems(self._package_list) self.package_combo.setCurrentIndex(0)
def test_iterate_packages(self): from rosmsg import iterate_packages, MODE_MSG, MODE_SRV import rospkg rospack = rospkg.RosPack() found = {} for p, path in iterate_packages(rospack, MODE_MSG): found[p] = path assert os.path.basename(path) == 'msg', path # make sure it's a package assert rospack.get_path(p) assert found for p, path in iterate_packages(rospack, MODE_SRV): found[p] = path assert os.path.basename(path) == 'srv', path # make sure it's a package assert rospack.get_path(p) assert found
def get_ros1_services(rospack=None): if not rospack: rospack = rospkg.RosPack() srvs = [] pkgs = sorted(x for x in rosmsg.iterate_packages(rospack, rosmsg.MODE_SRV)) for package_name, path in pkgs: for message_name in rosmsg._list_types(path, 'srv', rosmsg.MODE_SRV): srvs.append(Message(package_name, message_name, path)) return srvs
def get_ros1_messages(rospack=None): if not rospack: rospack = rospkg.RosPack() msgs = [] pkgs = sorted(x for x in rosmsg.iterate_packages(rospack, rosmsg.MODE_MSG)) for package_name, path in pkgs: for message_name in rosmsg._list_types(path, 'msg', rosmsg.MODE_MSG): msgs.append(Message(package_name, message_name, path)) return msgs
def handle_rwt_srv(req): srv = rosmsg.MODE_SRV pkg = RosPack() package = rosmsg.iterate_packages(pkg, srv) messageList = [] for pack in package: service, path = pack message = rosmsg.list_srvs(service) messageList.append(str(message)) return SrvListResponse(messageList)
def _refresh_packages(self, mode=rosmsg.MODE_MSG): if (self._mode == rosmsg.MODE_MSG) or self._mode == rosmsg.MODE_SRV: packages = sorted([pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages(self._rospack, self._mode)]) elif self._mode == rosaction.MODE_ACTION: packages = sorted([pkg_tuple[0] for pkg_tuple in rosaction.iterate_packages( self._rospack, self._mode)]) self._package_list = packages rospy.logdebug('pkgs={}'.format(self._package_list)) self._package_combo.clear() self._package_combo.addItems(self._package_list) self._package_combo.setCurrentIndex(0)
def get_rospkg_paths(): """ Gets a map of ROS package paths. Return: Map of map[package_name] = path """ rospack = rospkg.RosPack() found_pkgs = rosmsg.iterate_packages(rospack, '.msg') dir_map = {} for p, path in found_pkgs: # Must put path in array otherwise it shits itself. dir_map[p] = [path] return dir_map
def find_ros_type(rospack, asn1Type): ''' Finds the ROS type corresponding to an ASN.1 type. Receives a rospack object with the ROS packages information and the name of the ASN.1 type. Returns (found, package, message), where found is a bool indicating success or failure, and if True, then package and message are the ROS package name and message names corresponding to the input asn1Type. ''' packages = [ pkg for pkg, _ in rosmsg.iterate_packages(rospack, rosmsg.MODE_MSG) ] for pkg in packages: for msg in rosmsg.list_msgs(pkg, rospack): if type_name_matches(asn1Type, msg): return (True, pkg, msg) return (False, None, None)
def _update_thread_run(self): # update type_combo_box message_type_names = [] try: # this only works on fuerte and up packages = sorted([pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages(self._rospack, rosmsg.MODE_MSG)]) except: # this works up to electric packages = sorted(rosmsg.list_packages()) for package in packages: for base_type_str in rosmsg.list_msgs(package, rospack=self._rospack): message_class = roslib.message.get_message_class(base_type_str) if message_class is not None: message_type_names.append(base_type_str) self.type_combo_box.setItems.emit(sorted(message_type_names)) # update topic_combo_box _, _, topic_types = rospy.get_master().getTopicTypes() self._topic_dict = dict(topic_types) self.topic_combo_box.setItems.emit(sorted(self._topic_dict.keys()))
def get_rosmsg_paths(types): """ Gets a list of ROS message paths as they exist on your system Args: types: A list of ROS Message types in the format package/Type Return: A list of ROS message types as a map of map[type] = path """ rospack = rospkg.RosPack() found_pkgs = rosmsg.iterate_packages(rospack, '.msg') rosmsg_type_path_map = {} dir_map = {} type_map = {} # This associates a type with a package name. for full_type in types: package_name = full_type.split('/')[0] print "Searching package %s ... " % package_name base_type = full_type.split('/')[1] type_map[base_type] = package_name # This associates a package name with a msg path for p, path in found_pkgs: #print "Msg path for package %s found at %s" % (p, path) dir_map[p] = path for msg_type in types: msg_type = msg_type.split('/')[1] package_name = type_map[msg_type] rosmsg_type_path_map[msg_type] = dir_map[package_name] return rosmsg_type_path_map