toolboxes.py 27 KB

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