mavgenerate.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """\
3 mavgenerate.py is a GUI front-end for mavgen, a python based MAVLink
4 header generation tool.
5 
6 Notes:
7 -----
8 * 2012-7-16 -- dagoodman
9  Working on Mac 10.6.8 darwin, with Python 2.7.1
10 
11 * 2012-7-17 -- dagoodman
12  Only GUI code working on Mac 10.6.8 darwin, with Python 3.2.3
13  Working on Windows 7 SP1, with Python 2.7.3 and 3.2.3
14  Mavgen doesn't work with Python 3.x yet
15 
16 * 2012-9-25 -- dagoodman
17  Passing error limit into mavgen to make output cleaner.
18 
19 Copyright 2012 David Goodman (dagoodman@soe.ucsc.edu)
20 Released under GNU GPL version 3 or later
21 
22 """
23 import os
24 import re
25 import sys
26 
27 # Python 2.x and 3.x compatibility
28 if sys.version_info[0] == 3:
29  from tkinter import *
30  import tkinter.filedialog
31  import tkinter.messagebox
32 else:
33  # Must be using Python 2.x, import and rename
34  from Tkinter import *
35  import tkFileDialog
36  import tkMessageBox
37 
38  tkinter.filedialog = tkFileDialog
39  del tkFileDialog
40  tkinter.messagebox = tkMessageBox
41  del tkMessageBox
42 
43 
44 from pymavlink.generator import mavgen
45 from pymavlink.generator import mavparse
46 
47 title = "MAVLink Generator"
48 error_limit = 5
49 
50 
51 class Application(Frame):
52  def __init__(self, master=None):
53  Frame.__init__(self, master)
54  self.pack_propagate(0)
55  self.grid( sticky=N+S+E+W)
56  self.createWidgets()
57 
58  """\
59  Creates the gui and all of its content.
60  """
61  def createWidgets(self):
62 
63 
64  #----------------------------------------
65  # Create the XML entry
66 
67  self.xml_value = StringVar()
68  self.xml_label = Label( self, text="XML" )
69  self.xml_label.grid(row=0, column = 0)
70  self.xml_entry = Entry( self, width = 26, textvariable=self.xml_value )
71  self.xml_entry.grid(row=0, column = 1)
72  self.xml_button = Button (self, text="Browse", command=self.browseXMLFile)
73  self.xml_button.grid(row=0, column = 2)
74 
75  #----------------------------------------
76  # Create the Out entry
77 
78  self.out_value = StringVar()
79  self.out_label = Label( self, text="Out" )
80  self.out_label.grid(row=1,column = 0)
81  self.out_entry = Entry( self, width = 26, textvariable=self.out_value )
82  self.out_entry.grid(row=1, column = 1)
83  self.out_button = Button (self, text="Browse", command=self.browseOutDirectory)
84  self.out_button.grid(row=1, column = 2)
85 
86  #----------------------------------------
87  # Create the Lang box
88 
89  self.language_value = StringVar()
90  self.language_choices = mavgen.supportedLanguages
91  self.language_label = Label( self, text="Language" )
92  self.language_label.grid(row=2, column=0)
93  self.language_menu = OptionMenu(self,self.language_value,*self.language_choices)
94  self.language_value.set(mavgen.DEFAULT_LANGUAGE)
95  self.language_menu.config(width=10)
96  self.language_menu.grid(row=2, column=1,sticky=W)
97 
98  #----------------------------------------
99  # Create the Protocol box
100 
101  self.protocol_value = StringVar()
102  self.protocol_choices = [mavparse.PROTOCOL_1_0, mavparse.PROTOCOL_2_0]
103  self.protocol_label = Label( self, text="Protocol")
104  self.protocol_label.grid(row=3, column=0)
105  self.protocol_menu = OptionMenu(self,self.protocol_value,*self.protocol_choices)
106  self.protocol_value.set(mavgen.DEFAULT_WIRE_PROTOCOL)
107  self.protocol_menu.config(width=10)
108  self.protocol_menu.grid(row=3, column=1,sticky=W)
109 
110  #----------------------------------------
111  # Create the Validate box
112 
113  self.validate_value = BooleanVar()
114  self.validate_label = Label( self, text="Validate")
115  self.validate_label.grid(row=4, column=0)
116  self.validate_button = Checkbutton(self, variable=self.validate_value, onvalue=True, offvalue=False)
117  self.validate_value.set(mavgen.DEFAULT_VALIDATE)
118  self.validate_button.config(width=10)
119  self.validate_button.grid(row=4, column=1,sticky=W)
120 
121  #----------------------------------------
122  # Create the Validate Units box
123 
124  self.strict_units_value = BooleanVar()
125  self.strict_units_label = Label( self, text="Validate Units")
126  self.strict_units_label.grid(row=5, column=0)
127  self.strict_units_button = Checkbutton(self, variable=self.strict_units_value, onvalue=True, offvalue=False)
128  self.strict_units_value.set(mavgen.DEFAULT_STRICT_UNITS)
129  self.strict_units_button.config(width=10)
130  self.strict_units_button.grid(row=5, column=1,sticky=W)
131 
132  #----------------------------------------
133  # Create the generate button
134 
135  self.generate_button = Button ( self, text="Generate", command=self.generateHeaders)
136  self.generate_button.grid(row=6,column=1)
137 
138  """\
139  Open a file selection window to choose the XML message definition.
140  """
141  def browseXMLFile(self):
142  # TODO Allow specification of multiple XML definitions
143  xml_file = tkinter.filedialog.askopenfilename(parent=self, title='Choose a definition file')
144  if xml_file != None:
145  self.xml_value.set(xml_file)
146 
147  """\
148  Open a directory selection window to choose an output directory for
149  headers.
150  """
152  mavlinkFolder = os.path.dirname(os.path.realpath(__file__))
153  out_dir = tkinter.filedialog.askdirectory(parent=self,initialdir=mavlinkFolder,title='Please select an output directory')
154  if out_dir != None:
155  self.out_value.set(out_dir)
156 
157  """\
158  Generates the header files and place them in the output directory.
159  """
160  def generateHeaders(self):
161  # Verify settings
162  rex = re.compile(".*\\.xml$", re.IGNORECASE)
163  if not self.xml_value.get():
164  tkinter.messagebox.showerror('Error Generating Headers','An XML message definition file must be specified.')
165  return
166 
167  if not self.out_value.get():
168  tkinter.messagebox.showerror('Error Generating Headers', 'An output directory must be specified.')
169  return
170 
171 
172  if os.path.isdir(self.out_value.get()):
173  if not tkinter.messagebox.askokcancel('Overwrite Headers?','The output directory \'{0}\' already exists. Headers may be overwritten if they already exist.'.format(self.out_value.get())):
174  return
175 
176  # Generate headers
177  opts = mavgen.Opts(self.out_value.get(), wire_protocol=self.protocol_value.get(), language=self.language_value.get(), validate=self.validate_value.get(), error_limit=error_limit, strict_units=self.strict_units_value.get());
178  args = [self.xml_value.get()]
179  try:
180  mavgen.mavgen(opts,args)
181  tkinter.messagebox.showinfo('Successfully Generated Headers', 'Headers generated successfully.')
182 
183  except Exception as ex:
184  exStr = formatErrorMessage(str(ex));
185  tkinter.messagebox.showerror('Error Generating Headers','{0!s}'.format(exStr))
186  return
187 
188 """\
189 Format the mavgen exceptions by removing 'ERROR: '.
190 """
191 def formatErrorMessage(message):
192  reObj = re.compile(r'^(ERROR):\s+',re.M);
193  matches = re.findall(reObj, message);
194  prefix = ("An error occurred in mavgen:" if len(matches) == 1 else "Errors occurred in mavgen:\n")
195  message = re.sub(reObj, '\n', message);
196 
197  return prefix + message
198 
199 
200 # End of Application class
201 # ---------------------------------
202 
203 # ---------------------------------
204 # Start
205 
206 if __name__ == '__main__':
207  app = Application()
208  app.master.title(title)
209  app.mainloop()
def browseOutDirectory(self)
Definition: mavgenerate.py:151
def formatErrorMessage(message)
Definition: mavgenerate.py:191
def __init__(self, master=None)
Definition: mavgenerate.py:52


mavlink
Author(s): Lorenz Meier
autogenerated on Sun Apr 7 2019 02:06:02