Browse Source

uorb_to_ros_urtps_topics: minor cleanup

master
TSC21 4 years ago committed by Nuno Marques
parent
commit
2a368b4db1
  1. 50
      msg/tools/uorb_to_ros_urtps_topics.py

50
msg/tools/uorb_to_ros_urtps_topics.py

@ -3,12 +3,12 @@
Script to read an yaml file containing the microRTPS topics and update the naming convention to PascalCase Script to read an yaml file containing the microRTPS topics and update the naming convention to PascalCase
""" """
import argparse
import errno import errno
from pathlib import Path
import os import os
import yaml
import sys
import argparse
import six import six
import yaml
__author__ = 'PX4 Development Team' __author__ = 'PX4 Development Team'
__copyright__ = \ __copyright__ = \
@ -51,10 +51,7 @@ __maintainer__ = 'Nuno Marques'
__email__ = 'nuno.marques@dronesolution.io' __email__ = 'nuno.marques@dronesolution.io'
__status__ = 'Development' __status__ = 'Development'
list = [] verbose = False
in_file = ""
out_file = ""
verbose = True
class IndenterDumper(yaml.Dumper): class IndenterDumper(yaml.Dumper):
@ -75,7 +72,7 @@ def load_yaml_file(file):
try: try:
with open(file, 'r') as f: with open(file, 'r') as f:
if verbose: if verbose:
print(("--\t[Step 1] %s yaml file loaded!" % file)) print(("--\t\t- '%s' file loaded" % file))
return yaml.safe_load(f) return yaml.safe_load(f)
except OSError as e: except OSError as e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
@ -98,10 +95,10 @@ def update_dict(list):
if verbose: if verbose:
num_of_msgs += 1 num_of_msgs += 1
if verbose: if verbose:
print(("--\t[Step 2] List: %d msg names updated!" % num_of_msgs)) print(("--\t\t- %d ROS message file names updated" % num_of_msgs))
def update_yaml_file(list, file): def update_yaml_file(list, in_file, out_file):
""" """
Open the yaml file to dump the new list of dict toself. Open the yaml file to dump the new list of dict toself.
@ -110,7 +107,7 @@ def update_yaml_file(list, file):
:raises IOError: raises and error when the file is not found :raises IOError: raises and error when the file is not found
""" """
try: try:
with open(file, 'w') as f: with open(out_file, 'w') as f:
f.write("# AUTOGENERATED-FILE! DO NOT MODIFY IT DIRECTLY.\n#" f.write("# AUTOGENERATED-FILE! DO NOT MODIFY IT DIRECTLY.\n#"
" Edit instead the same file under PX4-Autopilot/msg/tools and" " Edit instead the same file under PX4-Autopilot/msg/tools and"
" use the \n# PX4-Autopilot/msg/tools/uorb_to_ros_urtps_topics.py" " use the \n# PX4-Autopilot/msg/tools/uorb_to_ros_urtps_topics.py"
@ -118,18 +115,17 @@ def update_yaml_file(list, file):
yaml.dump(list, f, Dumper=IndenterDumper, default_flow_style=False) yaml.dump(list, f, Dumper=IndenterDumper, default_flow_style=False)
if verbose: if verbose:
if in_file == out_file: if in_file == out_file:
print(("--\t[Step 3] %s updated!" % in_file)) print(("--\t\t- '%s' updated" % in_file))
else: else:
print(("--\t[Step 3] %s created!" % out_file)) print(("--\t\t- '%s' created" % out_file))
except OSError as e: except OSError as err:
if e.errno == errno.ENOENT: if err.errno == errno.ENOENT:
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file) raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), out_file)
else:
raise raise
if __name__ == "__main__": def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Read an yaml file containing the microRTPS topics and update the naming convention to PascalCase') description='Read an yaml file containing the microRTPS topics and update the naming convention to PascalCase')
optional = parser._action_groups.pop() optional = parser._action_groups.pop()
@ -143,14 +139,22 @@ if __name__ == "__main__":
default=True, help="Don't print status messages to stdout") default=True, help="Don't print status messages to stdout")
args = parser.parse_args() args = parser.parse_args()
global verbose
verbose = args.verbose verbose = args.verbose
in_file = args.input_file in_file = Path(args.input_file)
out_file = args.output_file if ( out_file = Path(args.output_file) if (
args.output_file != in_file and args.output_file != "") else in_file Path(args.output_file) != in_file and Path(args.output_file) != "") else in_file
if verbose: if verbose:
print("-- PX4 to ROS topics --") print("---------------------- \033[1mmicroRTPS bridge yaml PX4 to ROS topics\033[0m ----------------------")
print("-------------------------------------------------------------------------------------------------------")
list = load_yaml_file(in_file) list = load_yaml_file(in_file)
update_dict(list) update_dict(list)
update_yaml_file(list, out_file) update_yaml_file(list, in_file, out_file)
if verbose:
print("-------------------------------------------------------------------------------------------------------")
if __name__ == "__main__":
main()

Loading…
Cancel
Save