genaction.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # Copyright (c) 2009, Willow Garage, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Willow Garage, Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 
29 # Author: Stuart Glaser
30 
31 import sys
32 try:
33  from cStringIO import StringIO
34 except ImportError:
35  from io import StringIO
36 import re
37 import os, os.path
38 import errno
39 from optparse import OptionParser
40 
41 IODELIM = '---'
42 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM AN ACTION DEFINITION ======\n"
43 
44 class ActionSpecException(Exception): pass
45 
46 def parse_action_spec(text, package_context = ''):
47  pieces = [StringIO()]
48  for l in text.split('\n'):
49  if l.startswith(IODELIM):
50  pieces.append(StringIO())
51  else:
52  pieces[-1].write(l + '\n')
53  return [p.getvalue() for p in pieces]
54 
55 def write_file(filename, text):
56  f = open(filename, 'w')
57  f.write(text)
58  f.close()
59 
60 def main():
61 
62  parser = OptionParser("Actionlib generator")
63  parser.add_option('-o', dest='output_dir',
64  help='output directory')
65 
66  (options, args) = parser.parse_args(sys.argv)
67 
68  pkg = os.path.abspath(sys.argv[1])
69  filename = sys.argv[2]
70 
71  output_dir = options.output_dir
72 
73  # Try to make the directory, but silently continue if it already exists
74  try:
75  os.makedirs(output_dir)
76  except OSError as e:
77  if e.errno == errno.EEXIST:
78  pass
79  else:
80  raise
81 
82  action_file = args[1]
83  if not action_file.endswith('.action'):
84  print("The file specified has the wrong extension. It must end in .action")
85  else:
86  filename = action_file
87 
88  f = open(filename)
89  action_spec = f.read()
90  f.close()
91 
92  name = os.path.basename(filename)[:-7]
93  print("Generating for action %s" % name)
94 
95  pieces = parse_action_spec(action_spec)
96  if len(pieces) != 3:
97  raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
98  goal, result, feedback = pieces
99 
100  action_msg = AUTOGEN + """
101 %sActionGoal action_goal
102 %sActionResult action_result
103 %sActionFeedback action_feedback
104 """ % (name, name, name)
105 
106  goal_msg = AUTOGEN + goal
107  action_goal_msg = AUTOGEN + """
108 Header header
109 actionlib_msgs/GoalID goal_id
110 %sGoal goal
111 """ % name
112 
113  result_msg = AUTOGEN + result
114  action_result_msg = AUTOGEN + """
115 Header header
116 actionlib_msgs/GoalStatus status
117 %sResult result
118 """ % name
119 
120  feedback_msg = AUTOGEN + feedback
121  action_feedback_msg = AUTOGEN + """
122 Header header
123 actionlib_msgs/GoalStatus status
124 %sFeedback feedback
125 """ % name
126 
127  write_file(os.path.join(output_dir, "%sAction.msg"%name), action_msg)
128  write_file(os.path.join(output_dir, "%sGoal.msg"%name), goal_msg)
129  write_file(os.path.join(output_dir, "%sActionGoal.msg"%name), action_goal_msg)
130  write_file(os.path.join(output_dir, "%sResult.msg"%name), result_msg)
131  write_file(os.path.join(output_dir, "%sActionResult.msg"%name), action_result_msg)
132  write_file(os.path.join(output_dir, "%sFeedback.msg"%name), feedback_msg)
133  write_file(os.path.join(output_dir, "%sActionFeedback.msg"%name), action_feedback_msg)
134 
135 
136 if __name__ == '__main__': main()
def main()
Definition: genaction.py:60
def parse_action_spec(text, package_context='')
Definition: genaction.py:46
def write_file(filename, text)
Definition: genaction.py:55


actionlib_msgs
Author(s): Vijay Pradeep
autogenerated on Fri Jun 7 2019 21:44:14