diff --git a/msg/tools/uorb_to_ros_urtps_topics.py b/msg/tools/uorb_to_ros_urtps_topics.py index b790ce531c..8ff5f0e59d 100755 --- a/msg/tools/uorb_to_ros_urtps_topics.py +++ b/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 """ -import errno -import os -import yaml -import sys import argparse +import errno +from pathlib import Path +import os import six +import yaml __author__ = 'PX4 Development Team' __copyright__ = \ @@ -51,10 +51,7 @@ __maintainer__ = 'Nuno Marques' __email__ = 'nuno.marques@dronesolution.io' __status__ = 'Development' -list = [] -in_file = "" -out_file = "" -verbose = True +verbose = False class IndenterDumper(yaml.Dumper): @@ -75,7 +72,7 @@ def load_yaml_file(file): try: with open(file, 'r') as f: if verbose: - print(("--\t[Step 1] %s yaml file loaded!" % file)) + print(("--\t\t- '%s' file loaded" % file)) return yaml.safe_load(f) except OSError as e: if e.errno == errno.ENOENT: @@ -98,10 +95,10 @@ def update_dict(list): if verbose: num_of_msgs += 1 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. @@ -110,7 +107,7 @@ def update_yaml_file(list, file): :raises IOError: raises and error when the file is not found """ try: - with open(file, 'w') as f: + with open(out_file, 'w') as f: f.write("# AUTOGENERATED-FILE! DO NOT MODIFY IT DIRECTLY.\n#" " Edit instead the same file under PX4-Autopilot/msg/tools and" " 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) if verbose: if in_file == out_file: - print(("--\t[Step 3] %s updated!" % in_file)) + print(("--\t\t- '%s' updated" % in_file)) else: - print(("--\t[Step 3] %s created!" % out_file)) + print(("--\t\t- '%s' created" % out_file)) - except OSError as e: - if e.errno == errno.ENOENT: - raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file) - else: - raise + except OSError as err: + if err.errno == errno.ENOENT: + raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), out_file) + raise -if __name__ == "__main__": +def main(): parser = argparse.ArgumentParser( description='Read an yaml file containing the microRTPS topics and update the naming convention to PascalCase') optional = parser._action_groups.pop() @@ -143,14 +139,22 @@ if __name__ == "__main__": default=True, help="Don't print status messages to stdout") args = parser.parse_args() + global verbose verbose = args.verbose - in_file = args.input_file - out_file = args.output_file if ( - args.output_file != in_file and args.output_file != "") else in_file + in_file = Path(args.input_file) + out_file = Path(args.output_file) if ( + Path(args.output_file) != in_file and Path(args.output_file) != "") else in_file 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) update_dict(list) - update_yaml_file(list, out_file) + update_yaml_file(list, in_file, out_file) + if verbose: + print("-------------------------------------------------------------------------------------------------------") + + +if __name__ == "__main__": + main()