toolboxes.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. """!
  2. @package core.toolboxes
  3. @brief Functions for modifying menu from default/user toolboxes specified in XML files
  4. (C) 2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Vaclav Petras <wenzeslaus gmail.com>
  8. @author Anna Petrasova <kratochanna gmail.com>
  9. """
  10. import os
  11. import sys
  12. import copy
  13. import xml.etree.ElementTree as etree
  14. from xml.parsers import expat
  15. # Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7
  16. # and ElementTree 1.3.
  17. if hasattr(etree, 'ParseError'):
  18. ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
  19. else:
  20. ETREE_EXCEPTIONS = (expat.ExpatError)
  21. if sys.version_info[0:2] > (2, 6):
  22. has_xpath = True
  23. else:
  24. has_xpath = False
  25. from core.globalvar import WXGUIDIR
  26. from core.utils import GetSettingsPath, _
  27. from core.gcmd import GError, RunCommand
  28. import grass.script.task as gtask
  29. import grass.script.core as gcore
  30. from grass.script.utils import try_remove
  31. from grass.script.core import ScriptError
  32. from core.debug import Debug
  33. # this could be placed to functions
  34. mainMenuFile = os.path.join(WXGUIDIR, 'xml', 'main_menu.xml')
  35. toolboxesFile = os.path.join(WXGUIDIR, 'xml', 'toolboxes.xml')
  36. wxguiItemsFile = os.path.join(WXGUIDIR, 'xml', 'wxgui_items.xml')
  37. moduleItemsFile = os.path.join(WXGUIDIR, 'xml', 'module_items.xml')
  38. userToolboxesFile = os.path.join(GetSettingsPath(), 'toolboxes', 'toolboxes.xml')
  39. userMainMenuFile = os.path.join(GetSettingsPath(), 'toolboxes', 'main_menu.xml')
  40. if not os.path.exists(userToolboxesFile):
  41. userToolboxesFile = None
  42. if not os.path.exists(userMainMenuFile):
  43. userMainMenuFile = None
  44. def toolboxesOutdated():
  45. """!Removes auto-generated menudata.xml
  46. to let gui regenerate it next time it starts."""
  47. path = os.path.join(GetSettingsPath(), 'toolboxes', 'menudata.xml')
  48. if os.path.exists(path):
  49. try_remove(path)
  50. def getMenudataFile(userRootFile, newFile, fallback):
  51. """!Returns path to XML file for building menu or another tree.
  52. Creates toolbox directory where user defined toolboxes should be located.
  53. Checks whether it is needed to create new XML file (user changed toolboxes)
  54. or the already generated file could be used.
  55. If something goes wrong during building or user doesn't modify menu,
  56. default file (from distribution) is returned.
  57. """
  58. Debug.msg(1, "toolboxes.getMenudataFile: {userRootFile}, {newFile}, {fallback}".format(**locals()))
  59. distributionRootFile = os.path.join(WXGUIDIR, 'xml', userRootFile)
  60. userRootFile = os.path.join(GetSettingsPath(), 'toolboxes', userRootFile)
  61. if not os.path.exists(userRootFile):
  62. userRootFile = None
  63. # always create toolboxes directory if does not exist yet
  64. tbDir = _setupToolboxes()
  65. if tbDir:
  66. menudataFile = os.path.join(tbDir, newFile)
  67. generateNew = False
  68. # when any of main_menu.xml or toolboxes.xml are changed,
  69. # generate new menudata.xml
  70. if os.path.exists(menudataFile):
  71. # remove menu file when there is no main_menu and toolboxes
  72. if not userToolboxesFile and not userRootFile:
  73. os.remove(menudataFile)
  74. Debug.msg(2, "toolboxes.getMenudataFile: no user defined files, menudata deleted")
  75. return fallback
  76. if bool(userToolboxesFile) != bool(userRootFile):
  77. # always generate new because we don't know if there has been any change
  78. generateNew = True
  79. Debug.msg(2, "toolboxes.getMenudataFile: only one of the user defined files")
  80. else:
  81. # if newer files -> generate new
  82. menudataTime = os.path.getmtime(menudataFile)
  83. if userToolboxesFile:
  84. if os.path.getmtime(userToolboxesFile) > menudataTime:
  85. Debug.msg(2, "toolboxes.getMenudataFile: user toolboxes is newer than menudata")
  86. generateNew = True
  87. if userRootFile:
  88. if os.path.getmtime(userRootFile) > menudataTime:
  89. Debug.msg(2, "toolboxes.getMenudataFile: user root file is newer than menudata")
  90. generateNew = True
  91. elif userToolboxesFile or userRootFile:
  92. Debug.msg(2, "toolboxes.getMenudataFile: no menudata")
  93. generateNew = True
  94. else:
  95. Debug.msg(2, "toolboxes.getMenudataFile: no user defined files")
  96. return fallback
  97. if generateNew:
  98. try:
  99. # The case when user does not have custom root
  100. # file but has toolboxes requieres regeneration.
  101. # Unfortunately, this is the case can be often: defined
  102. # toolboxes but undefined module tree file.
  103. Debug.msg(2, "toolboxes.getMenudataFile: creating a tree")
  104. tree = createTree(distributionRootFile=distributionRootFile, userRootFile=userRootFile)
  105. except ETREE_EXCEPTIONS:
  106. GError(_("Unable to parse user toolboxes XML files. "
  107. "Default files will be loaded."))
  108. return fallback
  109. try:
  110. xml = _getXMLString(tree.getroot())
  111. fh = open(menudataFile, 'w')
  112. fh.write(xml)
  113. fh.close()
  114. return menudataFile
  115. except:
  116. Debug.msg(2, "toolboxes.getMenudataFile: writing menudata failed, returning fallback file")
  117. return fallback
  118. else:
  119. return menudataFile
  120. else:
  121. Debug.msg(2, "toolboxes.getMenudataFile: returning menudata fallback file")
  122. return fallback
  123. def _setupToolboxes():
  124. """!Create 'toolboxes' directory if doesn't exist."""
  125. basePath = GetSettingsPath()
  126. path = os.path.join(basePath, 'toolboxes')
  127. if not os.path.exists(basePath):
  128. return None
  129. if _createPath(path):
  130. return path
  131. return None
  132. def _createPath(path):
  133. """!Creates path (for toolboxes) if it doesn't exist'"""
  134. if not os.path.exists(path):
  135. try:
  136. os.mkdir(path)
  137. except OSError as e:
  138. # we cannot use GError or similar because the gui doesn''t start at all
  139. gcore.warning('%(reason)s\n%(detail)s' %
  140. ({'reason':_('Unable to create toolboxes directory.'),
  141. 'detail': str(e)}))
  142. return False
  143. return True
  144. def createTree(distributionRootFile, userRootFile, userDefined=True):
  145. """!Creates XML file with data for menu.
  146. Parses toolboxes files from distribution and from users,
  147. puts them together, adds metadata to modules and convert
  148. tree to previous format used for loading menu.
  149. @param userDefined use toolboxes defined by user or not (during compilation)
  150. @return ElementTree instance
  151. """
  152. if userDefined and userRootFile:
  153. mainMenu = etree.parse(userRootFile)
  154. else:
  155. mainMenu = etree.parse(distributionRootFile)
  156. toolboxes = etree.parse(toolboxesFile)
  157. if userDefined and userToolboxesFile:
  158. userToolboxes = etree.parse(userToolboxesFile)
  159. else:
  160. userToolboxes = None
  161. wxguiItems = etree.parse(wxguiItemsFile)
  162. moduleItems = etree.parse(moduleItemsFile)
  163. return toolboxes2menudata(mainMenu=mainMenu,
  164. toolboxes=toolboxes,
  165. userToolboxes=userToolboxes,
  166. wxguiItems=wxguiItems,
  167. moduleItems=moduleItems)
  168. def toolboxes2menudata(mainMenu, toolboxes, userToolboxes,
  169. wxguiItems, moduleItems):
  170. """!Creates XML file with data for menu.
  171. Parses toolboxes files from distribution and from users,
  172. puts them together, adds metadata to modules and convert
  173. tree to previous format used for loading menu.
  174. @param userDefined use toolboxes defined by user or not (during compilation)
  175. @return ElementTree instance
  176. """
  177. root = mainMenu.getroot()
  178. userHasToolboxes = False
  179. # in case user has empty toolboxes file (to avoid genereation)
  180. if userToolboxes and userToolboxes.findall('.//toolbox'):
  181. _expandUserToolboxesItem(root, userToolboxes)
  182. _expandToolboxes(root, userToolboxes)
  183. userHasToolboxes = True
  184. if not userHasToolboxes:
  185. _removeUserToolboxesItem(root)
  186. _expandToolboxes(root, toolboxes)
  187. # we do not expand addons here since they need to be expanded in runtime
  188. _expandItems(root, moduleItems, 'module-item')
  189. _expandItems(root, wxguiItems, 'wxgui-item')
  190. # in case of compilation there are no additional runtime modules
  191. # but we need to create empty elements
  192. _expandRuntimeModules(root)
  193. _addHandlers(root)
  194. _convertTree(root)
  195. _indent(root)
  196. return mainMenu
  197. def _indent(elem, level=0):
  198. """!Helper function to fix indentation of XML files."""
  199. i = "\n" + level * " "
  200. if len(elem):
  201. if not elem.text or not elem.text.strip():
  202. elem.text = i + " "
  203. if not elem.tail or not elem.tail.strip():
  204. elem.tail = i
  205. for elem in elem:
  206. _indent(elem, level + 1)
  207. if not elem.tail or not elem.tail.strip():
  208. elem.tail = i
  209. else:
  210. if level and (not elem.tail or not elem.tail.strip()):
  211. elem.tail = i
  212. def expandAddons(tree):
  213. """!Expands addons element.
  214. """
  215. root = tree.getroot()
  216. _expandAddonsItem(root)
  217. # expanding and converting is done twice, so there is some overhead
  218. _expandRuntimeModules(root)
  219. _addHandlers(root)
  220. _convertTree(root)
  221. def _expandToolboxes(node, toolboxes):
  222. """!Expands tree with toolboxes.
  223. Function is called recursively.
  224. @param node tree node where to look for subtoolboxes to be expanded
  225. @param toolboxes tree of toolboxes to be used for expansion
  226. >>> menu = etree.fromstring('''
  227. ... <toolbox name="Raster">
  228. ... <label>&amp;Raster</label>
  229. ... <items>
  230. ... <module-item name="r.mask"/>
  231. ... <wxgui-item name="RasterMapCalculator"/>
  232. ... <subtoolbox name="NeighborhoodAnalysis"/>
  233. ... <subtoolbox name="OverlayRasters"/>
  234. ... </items>
  235. ... </toolbox>''')
  236. >>> toolboxes = etree.fromstring('''
  237. ... <toolboxes>
  238. ... <toolbox name="NeighborhoodAnalysis">
  239. ... <label>Neighborhood analysis</label>
  240. ... <items>
  241. ... <module-item name="r.neighbors"/>
  242. ... <module-item name="v.neighbors"/>
  243. ... </items>
  244. ... </toolbox>
  245. ... <toolbox name="OverlayRasters">
  246. ... <label>Overlay rasters</label>
  247. ... <items>
  248. ... <module-item name="r.cross"/>
  249. ... </items>
  250. ... </toolbox>
  251. ... </toolboxes>''')
  252. >>> _expandToolboxes(menu, toolboxes)
  253. >>> print etree.tostring(menu)
  254. <toolbox name="Raster">
  255. <label>&amp;Raster</label>
  256. <items>
  257. <module-item name="r.mask" />
  258. <wxgui-item name="RasterMapCalculator" />
  259. <toolbox name="NeighborhoodAnalysis">
  260. <label>Neighborhood analysis</label>
  261. <items>
  262. <module-item name="r.neighbors" />
  263. <module-item name="v.neighbors" />
  264. </items>
  265. </toolbox>
  266. <toolbox name="OverlayRasters">
  267. <label>Overlay rasters</label>
  268. <items>
  269. <module-item name="r.cross" />
  270. </items>
  271. </toolbox>
  272. </items>
  273. </toolbox>
  274. """
  275. nodes = node.findall('.//toolbox')
  276. if node.tag == 'toolbox': # root
  277. nodes.append(node)
  278. for n in nodes:
  279. if n.find('items') is None:
  280. continue
  281. for subtoolbox in n.findall('./items/subtoolbox'):
  282. items = n.find('./items')
  283. idx = items.getchildren().index(subtoolbox)
  284. if has_xpath:
  285. toolbox = toolboxes.find('.//toolbox[@name="%s"]' % subtoolbox.get('name'))
  286. else:
  287. toolbox = None
  288. potentialToolboxes = toolboxes.findall('.//toolbox')
  289. sName = subtoolbox.get('name')
  290. for pToolbox in potentialToolboxes:
  291. if pToolbox.get('name') == sName:
  292. toolbox = pToolbox
  293. break
  294. if toolbox is None: # not in file
  295. continue
  296. _expandToolboxes(toolbox, toolboxes)
  297. items.insert(idx, toolbox)
  298. items.remove(subtoolbox)
  299. def _expandUserToolboxesItem(node, toolboxes):
  300. """!Expand tag 'user-toolboxes-list'.
  301. Include all user toolboxes.
  302. >>> tree = etree.fromstring('<toolbox><items><user-toolboxes-list/></items></toolbox>')
  303. >>> toolboxes = etree.fromstring('<toolboxes><toolbox name="UserToolbox"><items><module-item name="g.region"/></items></toolbox></toolboxes>')
  304. >>> _expandUserToolboxesItem(tree, toolboxes)
  305. >>> etree.tostring(tree)
  306. '<toolbox><items><toolbox name="GeneratedUserToolboxesList"><label>Custom toolboxes</label><items><toolbox name="UserToolbox"><items><module-item name="g.region" /></items></toolbox></items></toolbox></items></toolbox>'
  307. """
  308. tboxes = toolboxes.findall('.//toolbox')
  309. for n in node.findall('./items/user-toolboxes-list'):
  310. items = node.find('./items')
  311. idx = items.getchildren().index(n)
  312. el = etree.Element('toolbox', attrib={'name': 'GeneratedUserToolboxesList'})
  313. items.insert(idx, el)
  314. label = etree.SubElement(el, tag='label')
  315. label.text = _("Custom toolboxes")
  316. it = etree.SubElement(el, tag='items')
  317. for toolbox in tboxes:
  318. it.append(copy.deepcopy(toolbox))
  319. items.remove(n)
  320. def _removeUserToolboxesItem(root):
  321. """!Removes tag 'user-toolboxes-list' if there are no user toolboxes.
  322. >>> tree = etree.fromstring('<toolbox><items><user-toolboxes-list/></items></toolbox>')
  323. >>> _removeUserToolboxesItem(tree)
  324. >>> etree.tostring(tree)
  325. '<toolbox><items /></toolbox>'
  326. """
  327. for n in root.findall('./items/user-toolboxes-list'):
  328. items = root.find('./items')
  329. items.remove(n)
  330. def _getAddons():
  331. return sorted(RunCommand('g.extension', quiet=True, read=True,
  332. flags='a').splitlines())
  333. def _removeAddonsItem(node, addonsNodes):
  334. # TODO: change impl to be similar with the remove toolboxes
  335. for n in addonsNodes:
  336. items = node.find('./items')
  337. if items is not None:
  338. items.remove(n)
  339. # because of inconsistent menudata file
  340. items = node.find('./menubar')
  341. if items is not None:
  342. items.remove(n)
  343. def _expandAddonsItem(node):
  344. """!Expands addons element with currently installed addons.
  345. Note: there is no mechanism yet to tell the gui to rebuild the menudata.xml
  346. file when new addons are added/removed.
  347. """
  348. # no addonsTag -> do nothing
  349. addonsTags = node.findall('.//addons')
  350. if not addonsTags:
  351. return
  352. # fetch addons
  353. addons = _getAddons()
  354. # no addons -> remove addons tag
  355. if not addons:
  356. _removeAddonsItem(node, addonsTags)
  357. return
  358. # create addons toolbox
  359. # keywords and desc are handled later automatically
  360. for n in addonsTags:
  361. # find parent is not possible with implementation of etree (in 2.7)
  362. items = node.find('./menubar')
  363. idx = items.getchildren().index(n)
  364. # do not set name since it is already in menudata file
  365. # attib={'name': 'AddonsList'}
  366. el = etree.Element('menu')
  367. items.insert(idx, el)
  368. label = etree.SubElement(el, tag='label')
  369. label.text = _("Addons")
  370. it = etree.SubElement(el, tag='items')
  371. for addon in addons:
  372. addonItem = etree.SubElement(it, tag='module-item')
  373. addonItem.attrib = {'name': addon}
  374. addonLabel = etree.SubElement(addonItem, tag='label')
  375. addonLabel.text = addon
  376. items.remove(n)
  377. def _expandItems(node, items, itemTag):
  378. """!Expand items from file
  379. >>> tree = etree.fromstring('<items><module-item name="g.region"></module-item></items>')
  380. >>> items = etree.fromstring('<module-items><module-item name="g.region"><module>g.region</module><description>GRASS region management</description></module-item></module-items>')
  381. >>> _expandItems(tree, items, 'module-item')
  382. >>> etree.tostring(tree)
  383. '<items><module-item name="g.region"><module>g.region</module><description>GRASS region management</description></module-item></items>'
  384. """
  385. for moduleItem in node.findall('.//' + itemTag):
  386. itemName = moduleItem.get('name')
  387. if has_xpath:
  388. moduleNode = items.find('.//%s[@name="%s"]' % (itemTag, itemName))
  389. else:
  390. moduleNode = None
  391. potentialModuleNodes = items.findall('.//%s' % itemTag)
  392. for mNode in potentialModuleNodes:
  393. if mNode.get('name') == itemName:
  394. moduleNode = mNode
  395. break
  396. if moduleNode is None: # module not available in dist
  397. continue
  398. mItemChildren = moduleItem.getchildren()
  399. tagList = [n.tag for n in mItemChildren]
  400. for node in moduleNode.getchildren():
  401. if node.tag not in tagList:
  402. moduleItem.append(node)
  403. def _expandRuntimeModules(node):
  404. """!Add information to modules (desc, keywords)
  405. by running them with --interface-description.
  406. >>> tree = etree.fromstring('<items>'
  407. ... '<module-item name="g.region"></module-item>'
  408. ... '</items>')
  409. >>> _expandRuntimeModules(tree)
  410. >>> etree.tostring(tree)
  411. '<items><module-item name="g.region"><module>g.region</module><description>Manages the boundary definitions for the geographic region.</description><keywords>general,settings</keywords></module-item></items>'
  412. >>> tree = etree.fromstring('<items>'
  413. ... '<module-item name="m.proj"></module-item>'
  414. ... '</items>')
  415. >>> _expandRuntimeModules(tree)
  416. >>> etree.tostring(tree)
  417. '<items><module-item name="m.proj"><module>m.proj</module><description>Converts coordinates from one projection to another (cs2cs frontend).</description><keywords>miscellaneous,projection</keywords></module-item></items>'
  418. """
  419. modules = node.findall('.//module-item')
  420. for module in modules:
  421. name = module.get('name')
  422. if module.find('module') is None:
  423. n = etree.SubElement(parent=module, tag='module')
  424. n.text = name
  425. if module.find('description') is None:
  426. desc, keywords = _loadMetadata(name)
  427. n = etree.SubElement(parent=module, tag='description')
  428. n.text = _escapeXML(desc)
  429. n = etree.SubElement(parent=module, tag='keywords')
  430. n.text = _escapeXML(','.join(keywords))
  431. def _escapeXML(text):
  432. """!Helper function for correct escaping characters for XML.
  433. Duplicate function in core/toolboxes and probably also in man compilation
  434. and some existing Python package.
  435. >>> _escapeXML('<>&')
  436. '&amp;lt;&gt;&amp;'
  437. """
  438. return text.replace('<', '&lt;').replace("&", '&amp;').replace(">", '&gt;')
  439. def _loadMetadata(module):
  440. """!Load metadata to modules.
  441. @param module module name
  442. @return (description, keywords as a list)
  443. """
  444. try:
  445. task = gtask.parse_interface(module)
  446. except ScriptError:
  447. return '', ''
  448. return task.get_description(full=True), \
  449. task.get_keywords()
  450. def _addHandlers(node):
  451. """!Add missing handlers to modules"""
  452. for n in node.findall('.//module-item'):
  453. if n.find('handler') is None:
  454. handlerNode = etree.SubElement(parent=n, tag='handler')
  455. handlerNode.text = 'OnMenuCmd'
  456. # e.g. g.region -p
  457. for n in node.findall('.//wxgui-item'):
  458. if n.find('command') is not None:
  459. handlerNode = etree.SubElement(parent=n, tag='handler')
  460. handlerNode.text = 'RunMenuCmd'
  461. def _convertTag(node, old, new):
  462. """!Converts tag name.
  463. >>> tree = etree.fromstring('<toolboxes><toolbox><items><module-item/></items></toolbox></toolboxes>')
  464. >>> _convertTag(tree, 'toolbox', 'menu')
  465. >>> _convertTag(tree, 'module-item', 'menuitem')
  466. >>> etree.tostring(tree)
  467. '<toolboxes><menu><items><menuitem /></items></menu></toolboxes>'
  468. """
  469. for n in node.findall('.//%s' % old):
  470. n.tag = new
  471. def _convertTagAndRemoveAttrib(node, old, new):
  472. """Converts tag name and removes attributes.
  473. >>> tree = etree.fromstring('<toolboxes><toolbox name="Raster"><items><module-item name="g.region"/></items></toolbox></toolboxes>')
  474. >>> _convertTagAndRemoveAttrib(tree, 'toolbox', 'menu')
  475. >>> _convertTagAndRemoveAttrib(tree, 'module-item', 'menuitem')
  476. >>> etree.tostring(tree)
  477. '<toolboxes><menu><items><menuitem /></items></menu></toolboxes>'
  478. """
  479. for n in node.findall('.//%s' % old):
  480. n.tag = new
  481. n.attrib = {}
  482. def _convertTree(root):
  483. """!Converts tree to be the form readable by core/menutree.py.
  484. >>> tree = etree.fromstring('<toolbox name="MainMenu"><label>Main menu</label><items><toolbox><label>Raster</label><items><module-item name="g.region"><module>g.region</module></module-item></items></toolbox></items></toolbox>')
  485. >>> _convertTree(tree)
  486. >>> etree.tostring(tree)
  487. '<menudata><menubar><menu><label>Raster</label><items><menuitem><command>g.region</command></menuitem></items></menu></menubar></menudata>'
  488. """
  489. root.attrib = {}
  490. label = root.find('label')
  491. # must check because of inconsistent XML menudata file
  492. if label is not None:
  493. root.remove(label)
  494. _convertTag(root, 'description', 'help')
  495. _convertTag(root, 'wx-id', 'id')
  496. _convertTag(root, 'module', 'command')
  497. _convertTag(root, 'related-module', 'command')
  498. _convertTagAndRemoveAttrib(root, 'wxgui-item', 'menuitem')
  499. _convertTagAndRemoveAttrib(root, 'module-item', 'menuitem')
  500. root.tag = 'menudata'
  501. i1 = root.find('./items')
  502. # must check because of inconsistent XML menudata file
  503. if i1 is not None:
  504. i1.tag = 'menubar'
  505. _convertTagAndRemoveAttrib(root, 'toolbox', 'menu')
  506. def _getXMLString(root):
  507. """!Converts XML tree to string
  508. Since it is usually requier, this function adds a comment (about
  509. autogenerated file) to XML file.
  510. @return XML as string
  511. """
  512. xml = etree.tostring(root, encoding='UTF-8')
  513. return xml.replace("<?xml version='1.0' encoding='UTF-8'?>\n",
  514. "<?xml version='1.0' encoding='UTF-8'?>\n"
  515. "<!--This is an auto-generated file-->\n")
  516. def do_doctest_gettext_workaround():
  517. """Setups environment for doing a doctest with gettext usage.
  518. When using gettext with dynamically defined underscore function
  519. (`_("For translation")`), doctest does not work properly.
  520. One option is to use `import as` instead of dynamically defined underscore
  521. function but this requires change all modules which are used by tested
  522. module.
  523. The second option is to define dummy underscore function and one other
  524. function which creates the right environment to satisfy all. This is done
  525. by this function. Moreover, `sys.displayhook` and also
  526. `sys.__displayhook__` needs to be redefined too (the later one probably
  527. should not be newer redefined but some cases just requires that).
  528. GRASS specific note is that wxGUI switched to use imported underscore
  529. function for translation. However, GRASS Python libraries still uses the
  530. dynamically defined underscore function, so this workaround function is
  531. still needed when you import something from GRASS Python libraries.
  532. """
  533. def new_displayhook(string):
  534. """A replacement for default `sys.displayhook`"""
  535. if string is not None:
  536. sys.stdout.write("%r\n" % (string,))
  537. def new_translator(string):
  538. """A fake gettext underscore function."""
  539. return string
  540. sys.displayhook = new_displayhook
  541. sys.__displayhook__ = new_displayhook
  542. import __builtin__
  543. __builtin__._ = new_translator
  544. def doc_test():
  545. """Tests the module using doctest
  546. @return a number of failed tests
  547. """
  548. import doctest
  549. do_doctest_gettext_workaround()
  550. return doctest.testmod().failed
  551. def module_test():
  552. """Tests the module using test files included in the current directory and
  553. in files from distribution.
  554. """
  555. toolboxesFile = os.path.join(WXGUIDIR, 'xml', 'toolboxes.xml')
  556. userToolboxesFile = 'test.toolboxes_user_toolboxes.xml'
  557. menuFile = 'test.toolboxes_menu.xml'
  558. wxguiItemsFile = os.path.join(WXGUIDIR, 'xml', 'wxgui_items.xml')
  559. moduleItemsFile = os.path.join(WXGUIDIR, 'xml', 'module_items.xml')
  560. toolboxes = etree.parse(toolboxesFile)
  561. userToolboxes = etree.parse(userToolboxesFile)
  562. menu = etree.parse(menuFile)
  563. wxguiItems = etree.parse(wxguiItemsFile)
  564. moduleItems = etree.parse(moduleItemsFile)
  565. tree = toolboxes2menudata(mainMenu=menu,
  566. toolboxes=toolboxes,
  567. userToolboxes=userToolboxes,
  568. wxguiItems=wxguiItems,
  569. moduleItems=moduleItems)
  570. root = tree.getroot()
  571. tested = _getXMLString(root)
  572. # for generating correct test file supposing that the implementation
  573. # is now correct and working
  574. # run the normal test and check the difference before overwriting
  575. # the old correct test file
  576. if len(sys.argv) > 2 and sys.argv[2] == "generate-correct-file":
  577. sys.stdout.write(_getXMLString(root))
  578. return 0
  579. menudataFile = 'test.toolboxes_menudata.xml'
  580. with open(menudataFile) as correctMenudata:
  581. correct = str(correctMenudata.read())
  582. import difflib
  583. differ = difflib.Differ()
  584. result = list(differ.compare(correct.splitlines(True),
  585. tested.splitlines(True)))
  586. someDiff = False
  587. for line in result:
  588. if line.startswith('+') or line.startswith('-'):
  589. sys.stdout.write(line)
  590. someDiff = True
  591. if someDiff:
  592. print "Difference between files."
  593. return 1
  594. else:
  595. print "OK"
  596. return 0
  597. def main():
  598. """Converts the toolboxes files on standard paths to the menudata file
  599. File is written to the standard output.
  600. """
  601. # TODO: fix parameter handling
  602. if len(sys.argv) > 1:
  603. mainFile = os.path.join(WXGUIDIR, 'xml', 'module_tree.xml')
  604. else:
  605. mainFile = os.path.join(WXGUIDIR, 'xml', 'main_menu.xml')
  606. tree = createTree(distributionRootFile=mainFile, userRootFile=None,
  607. userDefined=False)
  608. root = tree.getroot()
  609. sys.stdout.write(_getXMLString(root))
  610. return 0
  611. if __name__ == '__main__':
  612. # TODO: fix parameter handling
  613. if len(sys.argv) > 1:
  614. if sys.argv[1] == 'doctest':
  615. sys.exit(doc_test())
  616. elif sys.argv[1] == 'test':
  617. sys.exit(module_test())
  618. sys.exit(main())