00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import sys
00032 import cStringIO
00033 import re
00034 import rosidl, rosidl.msgs
00035 import os, os.path
00036 import errno
00037 from optparse import OptionParser
00038
00039 IODELIM = '---'
00040 COMMENTCHAR = rosidl.msgs.COMMENTCHAR
00041 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM AN ACTION DEFINITION ======\n"
00042
00043 class ActionSpecException(Exception): pass
00044
00045 def parse_action_spec(text, package_context = ''):
00046 pieces = [cStringIO.StringIO()]
00047 for l in text.split('\n'):
00048 if l.startswith(IODELIM):
00049 pieces.append(cStringIO.StringIO())
00050 else:
00051 pieces[-1].write(l + '\n')
00052 return [p.getvalue() for p in pieces]
00053
00054 def write_file(filename, text):
00055 f = open(filename, 'w')
00056 f.write(text)
00057 f.close()
00058
00059 def main():
00060
00061 parser = OptionParser("Actionlib generator")
00062 parser.add_option('-o', dest='output_dir',
00063 help='output directory')
00064
00065 (options, args) = parser.parse_args(sys.argv)
00066
00067 pkg = os.path.abspath(sys.argv[1])
00068 filename = sys.argv[2]
00069
00070 output_dir = options.output_dir
00071
00072
00073 try:
00074 os.makedirs(output_dir)
00075 except OSError, e:
00076 if e.errno == errno.EEXIST:
00077 pass
00078 else:
00079 raise
00080
00081 action_file = args[1]
00082 if not action_file.endswith('.action'):
00083 print "The file specified has the wrong extension. It must end in .action"
00084 else:
00085 filename = action_file
00086
00087 f = open(filename)
00088 action_spec = f.read()
00089 f.close()
00090
00091 name = os.path.basename(filename)[:-7]
00092 print "Generating for action %s" % name
00093
00094 pieces = parse_action_spec(action_spec)
00095 if len(pieces) != 3:
00096 raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
00097 goal, result, feedback = pieces
00098
00099 action_msg = AUTOGEN + """
00100 %sActionGoal action_goal
00101 %sActionResult action_result
00102 %sActionFeedback action_feedback
00103 """ % (name, name, name)
00104
00105 goal_msg = AUTOGEN + goal
00106 action_goal_msg = AUTOGEN + """
00107 Header header
00108 actionlib_msgs/GoalID goal_id
00109 %sGoal goal
00110 """ % name
00111
00112 result_msg = AUTOGEN + result
00113 action_result_msg = AUTOGEN + """
00114 Header header
00115 actionlib_msgs/GoalStatus status
00116 %sResult result
00117 """ % name
00118
00119 feedback_msg = AUTOGEN + feedback
00120 action_feedback_msg = AUTOGEN + """
00121 Header header
00122 actionlib_msgs/GoalStatus status
00123 %sFeedback feedback
00124 """ % name
00125
00126 write_file(os.path.join(output_dir, "%sAction.msg"%name), action_msg)
00127 write_file(os.path.join(output_dir, "%sGoal.msg"%name), goal_msg)
00128 write_file(os.path.join(output_dir, "%sActionGoal.msg"%name), action_goal_msg)
00129 write_file(os.path.join(output_dir, "%sResult.msg"%name), result_msg)
00130 write_file(os.path.join(output_dir, "%sActionResult.msg"%name), action_result_msg)
00131 write_file(os.path.join(output_dir, "%sFeedback.msg"%name), feedback_msg)
00132 write_file(os.path.join(output_dir, "%sActionFeedback.msg"%name), action_feedback_msg)
00133
00134
00135 if __name__ == '__main__': main()