Browse Source

uorb_to_ros_msgs: minor cleanup

master
TSC21 4 years ago committed by Nuno Marques
parent
commit
74557c9071
  1. 131
      msg/tools/uorb_to_ros_msgs.py

131
msg/tools/uorb_to_ros_msgs.py

@ -53,62 +53,75 @@ __status__ = 'Development'
input_dir = sys.argv[1] input_dir = sys.argv[1]
output_dir = sys.argv[2] output_dir = sys.argv[2]
if not os.path.exists(os.path.abspath(output_dir)):
os.mkdir(os.path.abspath(output_dir)) def main():
else: print("----------------------- \033[1mmicroRTPS bridge uORB to ROS messages\033[0m -----------------------")
ros_msg_dir = os.path.abspath(output_dir) print("-------------------------------------------------------------------------------------------------------")
msg_files = os.listdir(ros_msg_dir)
for msg in msg_files: if not os.path.exists(os.path.abspath(output_dir)):
if msg.endswith(".msg"): os.mkdir(os.path.abspath(output_dir))
os.remove(os.path.join(ros_msg_dir, msg)) else:
ros_msg_dir = os.path.abspath(output_dir)
msg_list = list() msg_files = os.listdir(ros_msg_dir)
for msg in msg_files:
for filename in os.listdir(input_dir): if msg.endswith(".msg"):
if '.msg' in filename: os.remove(os.path.join(ros_msg_dir, msg))
msg_list.append(filename.rstrip('.msg'))
input_file = input_dir + filename msg_list = list()
output_file = output_dir + \ for filename in os.listdir(input_dir):
filename.partition(".")[0].title().replace('_', '') + ".msg" if '.msg' in filename:
copyfile(input_file, output_file) msg_list.append(filename.rstrip('.msg'))
input_file = input_dir + filename
for filename in os.listdir(output_dir):
if '.msg' in filename: output_file = output_dir + \
input_file = output_dir + filename filename.partition(".")[0].title().replace('_', '') + ".msg"
copyfile(input_file, output_file)
fileUpdated = False
for filename in os.listdir(output_dir):
with open(input_file, 'r') as f: if '.msg' in filename:
lines = f.readlines() input_file = output_dir + filename
newlines = []
alias_msgs = [] fileUpdated = False
alias_msg_files = []
with open(input_file, 'r') as f:
for line in lines: lines = f.readlines()
for msg_type in msg_list: newlines = []
if ('px4/' + msg_type + ' ') in line: alias_msgs = []
fileUpdated = True alias_msg_files = []
line = line.replace(('px4/' + msg_type),
msg_type.partition(".")[0].title().replace('_', '')) for line in lines:
if re.findall('^' + msg_type + '[\s\[]', line.partition('#')[0]): for msg_type in msg_list:
fileUpdated = True if ('px4/' + msg_type + ' ') in line:
line = line.replace(msg_type, fileUpdated = True
msg_type.partition(".")[0].title().replace('_', '')) line = line.replace(('px4/' + msg_type),
if '# TOPICS' in line: msg_type.partition(".")[0].title().replace('_', ''))
fileUpdated = True
alias_msgs += line.split() if re.findall('^' + msg_type + '[\s\[]', line.partition('#')[0]):
alias_msgs.remove('#') fileUpdated = True
alias_msgs.remove('TOPICS') line = line.replace(msg_type,
line = line.replace(line, '') msg_type.partition(".")[0].title().replace('_', ''))
newlines.append(line) if '# TOPICS' in line:
fileUpdated = True
for msg_file in alias_msgs: alias_msgs += line.split()
with open(output_dir + msg_file.partition(".")[0].title().replace('_', '') + ".msg", 'w+') as f: alias_msgs.remove('#')
for line in newlines: alias_msgs.remove('TOPICS')
f.write(line) line = line.replace(line, '')
newlines.append(line)
if fileUpdated:
with open(input_file, 'w+') as f: for msg_file in alias_msgs:
for line in newlines: with open(output_dir + msg_file.partition(".")[0].title().replace('_', '') + ".msg", 'w+') as f:
f.write(line) for line in newlines:
f.write(line)
if fileUpdated:
with open(input_file, 'w+') as f:
for line in newlines:
f.write(line)
print("--\t\t- Generated {} ROS message files in '{}'".format(len(os.listdir(output_dir)), os.path.abspath(output_dir)))
print("-------------------------------------------------------------------------------------------------------")
if __name__ == '__main__':
main()

Loading…
Cancel
Save