rename_rviz_topics.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Tue Jun 13 17:21:40 EEST 2017, Nikos Koukis
3 
4 """
5 Current script generates rviz files based on the templates files of
6 rviz/templates and usign the current computer's hostname for determining the
7 namespaces of the topics. This is a required step for visualizing multi-robot
8 graphSLAM when running based on rosbags or based on measurements from Gazebo
9 since each multi-robot agent runs on a separate ROS core to simulate as much as
10 possible a real-time setup.
11 
12 """
13 
14 import os
15 import sys
16 import logging
17 import socket
18 
19 from colorlog import ColoredFormatter
20 
21 LOG_LEVEL = logging.DEBUG
22 LOGFORMAT = ("%(log_color)s%(levelname)-5s%(reset)s "
23  "| %(log_color)s%(message)s%(reset)s")
24 
25 logging.root.setLevel(LOG_LEVEL)
26 formatter = ColoredFormatter(LOGFORMAT)
27 stream = logging.StreamHandler()
28 stream.setLevel(LOG_LEVEL)
29 stream.setFormatter(formatter)
30 
31 logger = logging.getLogger("RvizRenamer")
32 logger.setLevel(LOG_LEVEL)
33 logger.addHandler(stream)
34 
35 
36 
37 def rename_topics_in_rviz_file(templ_file, replace_dict):
38  """Read from a single template file and write the modified contents to an
39  actual rviz file one directory prior to the templates directory.
40  """
41 
42  with open(templ_file, 'r') as templ:
43  templ_lines = templ.readlines()
44  head, tail = os.path.split(templ_file)
45 
46  # create the actual rviz files in the parent dir of templates
47  actual_file = os.path.join(os.path.dirname(head), tail)
48  logger.info("Writing file: %s" % os.path.abspath(actual_file))
49  with open(actual_file, 'w') as f:
50  # rewrite its line of the templ file by making the appropriate
51  # substitution
52  f.writelines([l.format(**replace_dict) for l in templ_lines])
53 
54 
56  """Make sure that the user understands what the script does."""
57 
58  if len(sys.argv) > 1:
59  logger.warning("Current script modifies the template rviz files "
60  "found in rviz/templates so that their topics match the "
61  "running computer's hostname.\n"
62  "Run this without any additional arguments.")
63  logger.warning("Exiting...")
64  sys.exit(-1)
65 
66 
67 def main():
68  """Main."""
69 
70  logger.info("Initializing...")
72 
73  # fetch the rviz files that topics renaming is to happen
74  script_dir = os.path.dirname(os.path.realpath(__file__))
75  rviz_dir_path = os.path.join(script_dir, "..", "rviz", "templates")
76 
77  rviz_templ_files = [os.path.join(rviz_dir_path, i)
78  for i in os.listdir(rviz_dir_path)]
79 
80  # do the renaming only for files used for "gazebo" (suffix "gazebo") or
81  # recorded with rosbag (suffix "gazebo")
82 
83  rviz_templ_files = filter(lambda name: "bag" in name or "gazebo" in name,
84  rviz_templ_files)
85 
86  curr_hostname = socket.gethostname()
87  replace_dict = {"HOSTNAME_PLACEHOLDER": curr_hostname}
88 
89  logger.info("Rviz files to operate on:\n%s\n\n" %
90  os.linesep.join([os.path.abspath(f)
91  for f in rviz_templ_files]))
92 
93  # print the strings to be replaced
94  logger.info("Replacing: %s ==> %s" % replace_dict.items()[0])
95 
96  for templ_file in rviz_templ_files:
97  rename_topics_in_rviz_file(templ_file, replace_dict)
98  logger.info("All done.")
99 
100 
101 if __name__ == "__main__":
102  main()
103 
104 
105 
def rename_topics_in_rviz_file(templ_file, replace_dict)


mrpt_graphslam_2d
Author(s): Nikos Koukis
autogenerated on Thu Jun 6 2019 19:37:48