rename_rviz_topics.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Tue Jun 13 17:21:40 EEST 2017, Nikos Koukis
00003 
00004 """
00005 Current script generates rviz files based on the templates files of
00006 rviz/templates and usign the current computer's hostname for determining the
00007 namespaces of the topics. This is a required step for visualizing multi-robot
00008 graphSLAM when running based on rosbags or based on measurements from Gazebo
00009 since each multi-robot agent runs on a separate ROS core to simulate as much as
00010 possible a real-time setup.
00011 
00012 """
00013 
00014 import os
00015 import logging
00016 import socket
00017 
00018 logging.basicConfig(level=logging.DEBUG)
00019 logger = logging.getLogger("RvizRenamer")
00020 
00021 
00022 def rename_topics_in_rviz_file(templ_file, replace_dict):
00023     """Read from a single template file and write the modified contents to an
00024     actual rviz file one directory prior to the templates directory.
00025     """
00026 
00027     with open(templ_file, 'r') as templ:
00028         templ_lines = templ.readlines()
00029         head, tail = os.path.split(templ_file)
00030 
00031         # create the actual rviz files in the parent dir of templates
00032         actual_file = os.path.join(os.path.dirname(head), tail)
00033         logger.info("Writing file: %s" % os.path.abspath(actual_file))
00034         with open(actual_file, 'w') as f:
00035             # rewrite its line of the templ file by making the appropriate
00036             # substitution
00037             f.writelines([l.format(**replace_dict) for l in templ_lines])
00038 
00039 
00040 
00041 def main():
00042     """Main."""
00043 
00044     logger.info("Initializing...")
00045 
00046     # fetch the rviz files that topics renaming is to happen
00047     script_dir = os.path.dirname(os.path.realpath(__file__))
00048     rviz_dir_path = os.path.join(script_dir, "..", "rviz", "templates")
00049 
00050     rviz_templ_files = [os.path.join(rviz_dir_path, i)
00051                         for i in os.listdir(rviz_dir_path)]
00052 
00053     # do the renaming only for files used for "gazebo" (suffix "gazebo") or
00054     # recorded with rosbag (suffix "gazebo")
00055 
00056     rviz_templ_files = filter(lambda name: "bag" in name or "gazebo" in name,
00057                               rviz_templ_files)
00058 
00059     curr_hostname = socket.gethostname()
00060     replace_dict = {"HOSTNAME_PLACEHOLDER": curr_hostname}
00061 
00062     logger.info("Rviz files to operate on:\n%s\n\n" %
00063                 os.linesep.join([os.path.abspath(f)
00064                                  for f in rviz_templ_files]))
00065 
00066     # print the strings to be replaced
00067     logger.info("Replacing: %s ==> %s" % replace_dict.items()[0])
00068 
00069     for templ_file in rviz_templ_files:
00070         rename_topics_in_rviz_file(templ_file, replace_dict)
00071     logger.info("All done.")
00072 
00073 
00074 if __name__ == "__main__":
00075     main()
00076 
00077 
00078 


mrpt_graphslam_2d
Author(s): Nikos Koukis
autogenerated on Sun Sep 17 2017 03:02:04