toolboxes.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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
  52. located. Checks whether it is needed to create new XML file (user
  53. changed toolboxes) 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 as 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::
  345. there is no mechanism yet to tell the gui to rebuild the
  346. menudata.xml 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
  521. underscore function but this requires change all modules which are
  522. used by tested module.
  523. The second option is to define dummy underscore function and one
  524. other function which creates the right environment to satisfy all.
  525. This is done 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
  529. underscore function for translation. However, GRASS Python libraries
  530. still uses the dynamically defined underscore function, so this
  531. workaround function is still needed when you import something from
  532. GRASS Python libraries.
  533. """
  534. def new_displayhook(string):
  535. """A replacement for default `sys.displayhook`"""
  536. if string is not None:
  537. sys.stdout.write("%r\n" % (string,))
  538. def new_translator(string):
  539. """A fake gettext underscore function."""
  540. return string
  541. sys.displayhook = new_displayhook
  542. sys.__displayhook__ = new_displayhook
  543. import __builtin__
  544. __builtin__._ = new_translator
  545. def doc_test():
  546. """Tests the module using doctest
  547. :return: a number of failed tests
  548. """
  549. import doctest
  550. do_doctest_gettext_workaround()
  551. return doctest.testmod().failed
  552. def module_test():
  553. """Tests the module using test files included in the current
  554. directory and in files from distribution.
  555. """
  556. toolboxesFile = os.path.join(WXGUIDIR, 'xml', 'toolboxes.xml')
  557. userToolboxesFile = 'test.toolboxes_user_toolboxes.xml'
  558. menuFile = 'test.toolboxes_menu.xml'
  559. wxguiItemsFile = os.path.join(WXGUIDIR, 'xml', 'wxgui_items.xml')
  560. moduleItemsFile = os.path.join(WXGUIDIR, 'xml', 'module_items.xml')
  561. toolboxes = etree.parse(toolboxesFile)
  562. userToolboxes = etree.parse(userToolboxesFile)
  563. menu = etree.parse(menuFile)
  564. wxguiItems = etree.parse(wxguiItemsFile)
  565. moduleItems = etree.parse(moduleItemsFile)
  566. tree = toolboxes2menudata(mainMenu=menu,
  567. toolboxes=toolboxes,
  568. userToolboxes=userToolboxes,
  569. wxguiItems=wxguiItems,
  570. moduleItems=moduleItems)
  571. root = tree.getroot()
  572. tested = _getXMLString(root)
  573. # for generating correct test file supposing that the implementation
  574. # is now correct and working
  575. # run the normal test and check the difference before overwriting
  576. # the old correct test file
  577. if len(sys.argv) > 2 and sys.argv[2] == "generate-correct-file":
  578. sys.stdout.write(_getXMLString(root))
  579. return 0
  580. menudataFile = 'test.toolboxes_menudata.xml'
  581. with open(menudataFile) as correctMenudata:
  582. correct = str(correctMenudata.read())
  583. import difflib
  584. differ = difflib.Differ()
  585. result = list(differ.compare(correct.splitlines(True),
  586. tested.splitlines(True)))
  587. someDiff = False
  588. for line in result:
  589. if line.startswith('+') or line.startswith('-'):
  590. sys.stdout.write(line)
  591. someDiff = True
  592. if someDiff:
  593. print "Difference between files."
  594. return 1
  595. else:
  596. print "OK"
  597. return 0
  598. def main():
  599. """Converts the toolboxes files on standard paths to the menudata file
  600. File is written to the standard output.
  601. """
  602. # TODO: fix parameter handling
  603. if len(sys.argv) > 1:
  604. mainFile = os.path.join(WXGUIDIR, 'xml', 'module_tree.xml')
  605. else:
  606. mainFile = os.path.join(WXGUIDIR, 'xml', 'main_menu.xml')
  607. tree = createTree(distributionRootFile=mainFile, userRootFile=None,
  608. userDefined=False)
  609. root = tree.getroot()
  610. sys.stdout.write(_getXMLString(root))
  611. return 0
  612. if __name__ == '__main__':
  613. # TODO: fix parameter handling
  614. if len(sys.argv) > 1:
  615. if sys.argv[1] == 'doctest':
  616. sys.exit(doc_test())
  617. elif sys.argv[1] == 'test':
  618. sys.exit(module_test())
  619. sys.exit(main())