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 sys
00016 import logging
00017 import socket
00018 
00019 from colorlog import ColoredFormatter
00020 
00021 LOG_LEVEL = logging.DEBUG
00022 LOGFORMAT = ("%(log_color)s%(levelname)-5s%(reset)s "
00023              "| %(log_color)s%(message)s%(reset)s")
00024 
00025 logging.root.setLevel(LOG_LEVEL)
00026 formatter = ColoredFormatter(LOGFORMAT)
00027 stream = logging.StreamHandler()
00028 stream.setLevel(LOG_LEVEL)
00029 stream.setFormatter(formatter)
00030 
00031 logger = logging.getLogger("RvizRenamer")
00032 logger.setLevel(LOG_LEVEL)
00033 logger.addHandler(stream)
00034 
00035 
00036 
00037 def rename_topics_in_rviz_file(templ_file, replace_dict):
00038     """Read from a single template file and write the modified contents to an
00039     actual rviz file one directory prior to the templates directory.
00040     """
00041 
00042     with open(templ_file, 'r') as templ:
00043         templ_lines = templ.readlines()
00044         head, tail = os.path.split(templ_file)
00045 
00046         # create the actual rviz files in the parent dir of templates
00047         actual_file = os.path.join(os.path.dirname(head), tail)
00048         logger.info("Writing file: %s" % os.path.abspath(actual_file))
00049         with open(actual_file, 'w') as f:
00050             # rewrite its line of the templ file by making the appropriate
00051             # substitution
00052             f.writelines([l.format(**replace_dict) for l in templ_lines])
00053 
00054 
00055 def validate_args():
00056     """Make sure that the user understands what the script does."""
00057 
00058     if len(sys.argv) > 1:
00059         logger.warning("Current script modifies the template rviz files "
00060                        "found in rviz/templates so that their topics match the "
00061                        "running computer's hostname.\n"
00062                        "Run this without any additional arguments.")
00063         logger.warning("Exiting...")
00064         sys.exit(-1)
00065 
00066 
00067 def main():
00068     """Main."""
00069 
00070     logger.info("Initializing...")
00071     validate_args()
00072 
00073     # fetch the rviz files that topics renaming is to happen
00074     script_dir = os.path.dirname(os.path.realpath(__file__))
00075     rviz_dir_path = os.path.join(script_dir, "..", "rviz", "templates")
00076 
00077     rviz_templ_files = [os.path.join(rviz_dir_path, i)
00078                         for i in os.listdir(rviz_dir_path)]
00079 
00080     # do the renaming only for files used for "gazebo" (suffix "gazebo") or
00081     # recorded with rosbag (suffix "gazebo")
00082 
00083     rviz_templ_files = filter(lambda name: "bag" in name or "gazebo" in name,
00084                               rviz_templ_files)
00085 
00086     curr_hostname = socket.gethostname()
00087     replace_dict = {"HOSTNAME_PLACEHOLDER": curr_hostname}
00088 
00089     logger.info("Rviz files to operate on:\n%s\n\n" %
00090                 os.linesep.join([os.path.abspath(f)
00091                                  for f in rviz_templ_files]))
00092 
00093     # print the strings to be replaced
00094     logger.info("Replacing: %s ==> %s" % replace_dict.items()[0])
00095 
00096     for templ_file in rviz_templ_files:
00097         rename_topics_in_rviz_file(templ_file, replace_dict)
00098     logger.info("All done.")
00099 
00100 
00101 if __name__ == "__main__":
00102     main()
00103 
00104 
00105 


mrpt_graphslam_2d
Author(s): Nikos Koukis
autogenerated on Thu Jun 6 2019 21:40:26