genaction.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # Copyright (c) 2009, Willow Garage, Inc.
00003 # All rights reserved.
00004 # 
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 # 
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Willow Garage, Inc. nor the names of its
00014 #       contributors may be used to endorse or promote products derived from
00015 #       this software without specific prior written permission.
00016 # 
00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027 # POSSIBILITY OF SUCH DAMAGE.
00028 
00029 # Author: Stuart Glaser
00030 
00031 import sys
00032 try:
00033     from cStringIO import StringIO
00034 except ImportError:
00035     from io import StringIO
00036 import re
00037 import os, os.path
00038 import errno
00039 from optparse import OptionParser
00040 
00041 IODELIM   = '---'
00042 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM AN ACTION DEFINITION ======\n"
00043 
00044 class ActionSpecException(Exception): pass
00045 
00046 def parse_action_spec(text, package_context = ''):
00047     pieces = [StringIO()]
00048     for l in text.split('\n'):
00049         if l.startswith(IODELIM):
00050             pieces.append(StringIO())
00051         else:
00052             pieces[-1].write(l + '\n')
00053     return [p.getvalue() for p in pieces]
00054 
00055 def write_file(filename, text):
00056     f = open(filename, 'w')
00057     f.write(text)
00058     f.close()
00059 
00060 def main():
00061 
00062     parser = OptionParser("Actionlib generator")
00063     parser.add_option('-o', dest='output_dir',
00064                       help='output directory')
00065 
00066     (options, args) = parser.parse_args(sys.argv)
00067 
00068     pkg = os.path.abspath(sys.argv[1])
00069     filename = sys.argv[2]
00070 
00071     output_dir = options.output_dir
00072 
00073     # Try to make the directory, but silently continue if it already exists
00074     try:
00075         os.makedirs(output_dir)
00076     except OSError as e:
00077         if e.errno == errno.EEXIST:
00078             pass
00079         else:
00080             raise
00081 
00082     action_file = args[1]
00083     if not action_file.endswith('.action'):
00084         print("The file specified has the wrong extension. It must end in .action")
00085     else:
00086         filename = action_file
00087 
00088         f = open(filename)
00089         action_spec = f.read()
00090         f.close()
00091 
00092         name = os.path.basename(filename)[:-7]
00093         print("Generating for action %s" % name)
00094 
00095         pieces = parse_action_spec(action_spec)
00096         if len(pieces) != 3:
00097             raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
00098         goal, result, feedback = pieces
00099 
00100         action_msg = AUTOGEN + """
00101 %sActionGoal action_goal
00102 %sActionResult action_result
00103 %sActionFeedback action_feedback
00104 """ % (name, name, name)
00105 
00106         goal_msg = AUTOGEN + goal
00107         action_goal_msg = AUTOGEN + """
00108 Header header
00109 actionlib_msgs/GoalID goal_id
00110 %sGoal goal
00111 """ % name
00112 
00113         result_msg = AUTOGEN + result
00114         action_result_msg = AUTOGEN + """
00115 Header header
00116 actionlib_msgs/GoalStatus status
00117 %sResult result
00118 """ % name
00119 
00120         feedback_msg = AUTOGEN + feedback
00121         action_feedback_msg = AUTOGEN + """
00122 Header header
00123 actionlib_msgs/GoalStatus status
00124 %sFeedback feedback
00125 """ % name
00126 
00127         write_file(os.path.join(output_dir, "%sAction.msg"%name), action_msg)
00128         write_file(os.path.join(output_dir, "%sGoal.msg"%name), goal_msg)
00129         write_file(os.path.join(output_dir, "%sActionGoal.msg"%name), action_goal_msg)
00130         write_file(os.path.join(output_dir, "%sResult.msg"%name), result_msg)
00131         write_file(os.path.join(output_dir, "%sActionResult.msg"%name), action_result_msg)
00132         write_file(os.path.join(output_dir, "%sFeedback.msg"%name), feedback_msg)
00133         write_file(os.path.join(output_dir, "%sActionFeedback.msg"%name), action_feedback_msg)
00134 
00135 
00136 if __name__ == '__main__': main()


actionlib_msgs
Author(s): Vijay Pradeep
autogenerated on Wed Aug 26 2015 11:01:23