GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
core/menudata.py
Go to the documentation of this file.
1 """!
2 @package core.menudata
3 
4 @brief Complex list for menu entries for wxGUI
5 
6 Classes:
7  - menudata::MenuData
8 
9 Usage:
10 @code
11 python menudata.py [action] [manager|modeler]
12 @endcode
13 
14 where <i>action</i>:
15  - strings (default)
16  - tree
17  - commands
18  - dump
19 
20 (C) 2007-2011 by the GRASS Development Team
21 
22 This program is free software under the GNU General Public License
23 (>=v2). Read the file COPYING that comes with GRASS for details.
24 
25 @author Michael Barton (Arizona State University)
26 @author Yann Chemin <yann.chemin gmail.com>
27 @author Martin Landa <landa.martin gmail.com>
28 @author Glynn Clements
29 """
30 
31 import os
32 import sys
33 import pprint
34 try:
35  import xml.etree.ElementTree as etree
36 except ImportError:
37  import elementtree.ElementTree as etree # Python <= 2.4
38 
39 import wx
40 
41 if not os.getenv("GISBASE"):
42  sys.exit("GRASS is not running. Exiting...")
43 
44 class MenuData:
45  """!Abstract menu data class"""
46  def __init__(self, filename):
47  self.tree = etree.parse(filename)
48 
49  def _getMenuItem(self, mi):
50  """!Get menu item
51 
52  @param mi menu item instance
53  """
54  if mi.tag == 'separator':
55  return ('', '', '', '', '')
56  elif mi.tag == 'menuitem':
57  label = _(mi.find('label').text)
58  help = _(mi.find('help').text)
59  handler = mi.find('handler').text
60  gcmd = mi.find('command') # optional
61  keywords = mi.find('keywords') # optional
62  shortcut = mi.find('shortcut') # optional
63  wxId = mi.find('id') # optional
64  if gcmd != None:
65  gcmd = gcmd.text
66  else:
67  gcmd = ""
68  if keywords != None:
69  keywords = keywords.text
70  else:
71  keywords = ""
72  if shortcut != None:
73  shortcut = shortcut.text
74  else:
75  shortcut = ""
76  if wxId != None:
77  wxId = eval('wx.' + wxId.text)
78  else:
79  wxId = wx.ID_ANY
80  return (label, help, handler, gcmd, keywords, shortcut, wxId)
81  elif mi.tag == 'menu':
82  return self._getMenu(mi)
83  else:
84  raise Exception(_("Unknow tag"))
85 
86  def _getMenu(self, m):
87  """!Get menu
88 
89  @param m menu
90 
91  @return label, menu items
92  """
93  label = _(m.find('label').text)
94  items = m.find('items')
95  return (label, tuple(map(self._getMenuItem, items)))
96 
97  def _getMenuBar(self, mb):
98  """!Get menu bar
99 
100  @param mb menu bar instance
101 
102  @return menu items
103  """
104  return tuple(map(self._getMenu, mb.findall('menu')))
105 
106  def _getMenuData(self, md):
107  """!Get menu data
108 
109  @param md menu data instace
110 
111  @return menu data
112  """
113  return list(map(self._getMenuBar, md.findall('menubar')))
114 
115  def GetMenu(self):
116  """!Get menu
117 
118  @return menu data
119  """
120  return self._getMenuData(self.tree.getroot())
121 
122  def PrintStrings(self, fh):
123  """!Print menu strings to file (used for localization)
124 
125  @param fh file descriptor"""
126  className = str(self.__class__).split('.', 1)[1]
127  fh.write('menustrings_%s = [\n' % className)
128  for node in self.tree.getiterator():
129  if node.tag in ['label', 'help']:
130  fh.write(' _(%r),\n' % node.text)
131  fh.write(' \'\']\n')
132 
133  def PrintTree(self, fh):
134  """!Print menu tree to file
135 
136  @param fh file descriptor"""
137  level = 0
138  for eachMenuData in self.GetMenu():
139  for label, items in eachMenuData:
140  fh.write('- %s\n' % label.replace('&', ''))
141  self._PrintTreeItems(fh, level + 1, items)
142 
143  def _PrintTreeItems(self, fh, level, menuData):
144  """!Print menu tree items to file (used by PrintTree)
145 
146  @param fh file descriptor
147  @param level menu level
148  @param menuData menu data to print out"""
149  for eachItem in menuData:
150  if len(eachItem) == 2:
151  if eachItem[0]:
152  fh.write('%s - %s\n' % (' ' * level, eachItem[0]))
153  self._PrintTreeItems(fh, level + 1, eachItem[1])
154  else:
155  if eachItem[0]:
156  fh.write('%s - %s\n' % (' ' * level, eachItem[0]))
157 
158  def PrintCommands(self, fh, itemSep = ' | ', menuSep = ' > '):
159  """!Print commands list (command | menu item > menu item)
160 
161  @param fh file descriptor
162  """
163  level = 0
164  for eachMenuData in self.GetMenu():
165  for label, items in eachMenuData:
166  menuItems = [label, ]
167  self._PrintCommandsItems(fh, level + 1, items,
168  menuItems, itemSep, menuSep)
169 
170  def _PrintCommandsItems(self, fh, level, menuData,
171  menuItems, itemSep, menuSep):
172  """!Print commands item (used by PrintCommands)
173 
174  @param fh file descriptor
175  @param menuItems list of menu items
176  """
177  for eachItem in menuData:
178  if len(eachItem) == 2:
179  if eachItem[0]:
180  try:
181  menuItems[level] = eachItem[0]
182  except IndexError:
183  menuItems.append(eachItem[0])
184  self._PrintCommandsItems(fh, level + 1, eachItem[1],
185  menuItems, itemSep, menuSep)
186  else:
187  try:
188  del menuItems[level]
189  except IndexError:
190  pass
191 
192  if eachItem[3]:
193  fh.write('%s%s' % (eachItem[3], itemSep))
194  fh.write(menuSep.join(map(lambda x: x.replace('&', ''), menuItems)))
195  fh.write('%s%s' % (menuSep, eachItem[0]))
196  fh.write('\n')
197 
198 if __name__ == "__main__":
199  import os
200  import sys
201 
202  # i18N
203  import gettext
204  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
205 
206  action = 'strings'
207  menu = 'manager'
208 
209  for arg in sys.argv:
210  if arg in ('strings', 'tree', 'commands', 'dump'):
211  action = arg
212  elif arg in ('manager', 'modeler'):
213  menu = arg
214 
215  sys.path.append(os.path.join(os.getenv("GISBASE"), "etc", "wxpython"))
216 
217  if menu == 'manager':
218  from lmgr.menudata import ManagerData
219  data = ManagerData()
220  else:
221  from gmodeler.menudata import ModelerData
222  data = ModelerData()
223 
224  if action == 'strings':
225  data.PrintStrings(sys.stdout)
226  elif action == 'tree':
227  data.PrintTree(sys.stdout)
228  elif action == 'commands':
229  data.PrintCommands(sys.stdout)
230  elif action == 'dump':
231  pprint.pprint(data.GetMenu(), stream = sys.stdout, indent = 2)
232 
233  sys.exit(0)
def PrintCommands
Print commands list (command | menu item &gt; menu item)
wxGUI Graphical Modeler - menu data
def _PrintTreeItems
Print menu tree items to file (used by PrintTree)
def split
Platform spefic shlex.split.
Definition: core/utils.py:37
def _PrintCommandsItems
Print commands item (used by PrintCommands)
def PrintTree
Print menu tree to file.
def GetMenu
Get menu.
def PrintStrings
Print menu strings to file (used for localization)
Abstract menu data class.