4 @brief Complex list for menu entries for wxGUI
11 python menudata.py [action] [manager|modeler]
20 (C) 2007-2011 by the GRASS Development Team
22 This program is free software under the GNU General Public License
23 (>=v2). Read the file COPYING that comes with GRASS for details.
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
35 import xml.etree.ElementTree
as etree
37 import elementtree.ElementTree
as etree
41 if not os.getenv(
"GISBASE"):
42 sys.exit(
"GRASS is not running. Exiting...")
45 """!Abstract menu data class"""
47 self.
tree = etree.parse(filename)
49 def _getMenuItem(self, mi):
52 @param mi menu item instance
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')
61 keywords = mi.find(
'keywords')
62 shortcut = mi.find(
'shortcut')
69 keywords = keywords.text
73 shortcut = shortcut.text
77 wxId = eval(
'wx.' + wxId.text)
80 return (label, help, handler, gcmd, keywords, shortcut, wxId)
81 elif mi.tag ==
'menu':
82 return self._getMenu(mi)
84 raise Exception(_(
"Unknow tag"))
86 def _getMenu(self, m):
91 @return label, menu items
93 label = _(m.find(
'label').text)
94 items = m.find(
'items')
95 return (label, tuple(map(self._getMenuItem, items)))
97 def _getMenuBar(self, mb):
100 @param mb menu bar instance
104 return tuple(map(self._getMenu, mb.findall(
'menu')))
106 def _getMenuData(self, md):
109 @param md menu data instace
113 return list(map(self._getMenuBar, md.findall(
'menubar')))
120 return self._getMenuData(self.tree.getroot())
123 """!Print menu strings to file (used for localization)
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)
134 """!Print menu tree to file
136 @param fh file descriptor"""
138 for eachMenuData
in self.
GetMenu():
139 for label, items
in eachMenuData:
140 fh.write(
'- %s\n' % label.replace(
'&',
''))
143 def _PrintTreeItems(self, fh, level, menuData):
144 """!Print menu tree items to file (used by PrintTree)
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:
152 fh.write(
'%s - %s\n' % (
' ' * level, eachItem[0]))
156 fh.write(
'%s - %s\n' % (
' ' * level, eachItem[0]))
159 """!Print commands list (command | menu item > menu item)
161 @param fh file descriptor
164 for eachMenuData
in self.
GetMenu():
165 for label, items
in eachMenuData:
166 menuItems = [label, ]
168 menuItems, itemSep, menuSep)
170 def _PrintCommandsItems(self, fh, level, menuData,
171 menuItems, itemSep, menuSep):
172 """!Print commands item (used by PrintCommands)
174 @param fh file descriptor
175 @param menuItems list of menu items
177 for eachItem
in menuData:
178 if len(eachItem) == 2:
181 menuItems[level] = eachItem[0]
183 menuItems.append(eachItem[0])
185 menuItems, itemSep, menuSep)
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]))
198 if __name__ ==
"__main__":
204 gettext.install(
'grasswxpy', os.path.join(os.getenv(
"GISBASE"),
'locale'), unicode=
True)
210 if arg
in (
'strings',
'tree',
'commands',
'dump'):
212 elif arg
in (
'manager',
'modeler'):
215 sys.path.append(os.path.join(os.getenv(
"GISBASE"),
"etc",
"wxpython"))
217 if menu ==
'manager':
218 from lmgr.menudata
import ManagerData
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)
def split
Platform spefic shlex.split.