toolboxes.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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
  53. located. Checks whether it is needed to create new XML file (user
  54. changed toolboxes) 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::
  346. there is no mechanism yet to tell the gui to rebuild the
  347. menudata.xml file when new addons are added/removed.
  348. """
  349. # no addonsTag -> do nothing
  350. addonsTags = node.findall('.//addons')
  351. if not addonsTags:
  352. return
  353. # fetch addons
  354. addons = _getAddons()
  355. # no addons -> remove addons tag
  356. if not addons:
  357. _removeAddonsItem(node, addonsTags)
  358. return
  359. # create addons toolbox
  360. # keywords and desc are handled later automatically
  361. for n in addonsTags:
  362. # find parent is not possible with implementation of etree (in 2.7)
  363. items = node.find('./menubar')
  364. idx = items.getchildren().index(n)
  365. # do not set name since it is already in menudata file
  366. # attib={'name': 'AddonsList'}
  367. el = etree.Element('menu')
  368. items.insert(idx, el)
  369. label = etree.SubElement(el, tag='label')
  370. label.text = _("Addons")
  371. it = etree.SubElement(el, tag='items')
  372. for addon in addons:
  373. addonItem = etree.SubElement(it, tag='module-item')
  374. addonItem.attrib = {'name': addon}
  375. addonLabel = etree.SubElement(addonItem, tag='label')
  376. addonLabel.text = addon
  377. items.remove(n)
  378. def _expandItems(node, items, itemTag):
  379. """Expand items from file
  380. >>> tree = etree.fromstring('<items><module-item name="g.region"></module-item></items>')
  381. >>> items = etree.fromstring('<module-items><module-item name="g.region"><module>g.region</module><description>GRASS region management</description></module-item></module-items>')
  382. >>> _expandItems(tree, items, 'module-item')
  383. >>> etree.tostring(tree)
  384. '<items><module-item name="g.region"><module>g.region</module><description>GRASS region management</description></module-item></items>'
  385. """
  386. for moduleItem in node.findall('.//' + itemTag):
  387. itemName = moduleItem.get('name')
  388. if has_xpath:
  389. moduleNode = items.find('.//%s[@name="%s"]' % (itemTag, itemName))
  390. else:
  391. moduleNode = None
  392. potentialModuleNodes = items.findall('.//%s' % itemTag)
  393. for mNode in potentialModuleNodes:
  394. if mNode.get('name') == itemName:
  395. moduleNode = mNode
  396. break
  397. if moduleNode is None: # module not available in dist
  398. continue
  399. mItemChildren = moduleItem.getchildren()
  400. tagList = [n.tag for n in mItemChildren]
  401. for node in moduleNode.getchildren():
  402. if node.tag not in tagList:
  403. moduleItem.append(node)
  404. def _expandRuntimeModules(node):
  405. """Add information to modules (desc, keywords)
  406. by running them with --interface-description.
  407. >>> tree = etree.fromstring('<items>'
  408. ... '<module-item name="g.region"></module-item>'
  409. ... '</items>')
  410. >>> _expandRuntimeModules(tree)
  411. >>> etree.tostring(tree)
  412. '<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>'
  413. >>> tree = etree.fromstring('<items>'
  414. ... '<module-item name="m.proj"></module-item>'
  415. ... '</items>')
  416. >>> _expandRuntimeModules(tree)
  417. >>> etree.tostring(tree)
  418. '<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>'
  419. """
  420. modules = node.findall('.//module-item')
  421. for module in modules:
  422. name = module.get('name')
  423. if module.find('module') is None:
  424. n = etree.SubElement(parent=module, tag='module')
  425. n.text = name
  426. if module.find('description') is None:
  427. desc, keywords = _loadMetadata(name)
  428. n = etree.SubElement(parent=module, tag='description')
  429. n.text = _escapeXML(desc)
  430. n = etree.SubElement(parent=module, tag='keywords')
  431. n.text = _escapeXML(','.join(keywords))
  432. def _escapeXML(text):
  433. """Helper function for correct escaping characters for XML.
  434. Duplicate function in core/toolboxes and probably also in man compilation
  435. and some existing Python package.
  436. >>> _escapeXML('<>&')
  437. '&amp;lt;&gt;&amp;'
  438. """
  439. return text.replace('<', '&lt;').replace("&", '&amp;').replace(">", '&gt;')
  440. def _loadMetadata(module):
  441. """Load metadata to modules.
  442. :param module: module name
  443. :return: (description, keywords as a list)
  444. """
  445. try:
  446. task = gtask.parse_interface(module)
  447. except ScriptError:
  448. return '', ''
  449. return task.get_description(full=True), \
  450. task.get_keywords()
  451. def _addHandlers(node):
  452. """Add missing handlers to modules"""
  453. for n in node.findall('.//module-item'):
  454. if n.find('handler') is None:
  455. handlerNode = etree.SubElement(parent=n, tag='handler')
  456. handlerNode.text = 'OnMenuCmd'
  457. # e.g. g.region -p
  458. for n in node.findall('.//wxgui-item'):
  459. if n.find('command') is not None:
  460. handlerNode = etree.SubElement(parent=n, tag='handler')
  461. handlerNode.text = 'RunMenuCmd'
  462. def _convertTag(node, old, new):
  463. """Converts tag name.
  464. >>> tree = etree.fromstring('<toolboxes><toolbox><items><module-item/></items></toolbox></toolboxes>')
  465. >>> _convertTag(tree, 'toolbox', 'menu')
  466. >>> _convertTag(tree, 'module-item', 'menuitem')
  467. >>> etree.tostring(tree)
  468. '<toolboxes><menu><items><menuitem /></items></menu></toolboxes>'
  469. """
  470. for n in node.findall('.//%s' % old):
  471. n.tag = new
  472. def _convertTagAndRemoveAttrib(node, old, new):
  473. """Converts tag name and removes attributes.
  474. >>> tree = etree.fromstring('<toolboxes><toolbox name="Raster"><items><module-item name="g.region"/></items></toolbox></toolboxes>')
  475. >>> _convertTagAndRemoveAttrib(tree, 'toolbox', 'menu')
  476. >>> _convertTagAndRemoveAttrib(tree, 'module-item', 'menuitem')
  477. >>> etree.tostring(tree)
  478. '<toolboxes><menu><items><menuitem /></items></menu></toolboxes>'
  479. """
  480. for n in node.findall('.//%s' % old):
  481. n.tag = new
  482. n.attrib = {}
  483. def _convertTree(root):
  484. """Converts tree to be the form readable by core/menutree.py.
  485. >>> 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>')
  486. >>> _convertTree(tree)
  487. >>> etree.tostring(tree)
  488. '<menudata><menubar><menu><label>Raster</label><items><menuitem><command>g.region</command></menuitem></items></menu></menubar></menudata>'
  489. """
  490. root.attrib = {}
  491. label = root.find('label')
  492. # must check because of inconsistent XML menudata file
  493. if label is not None:
  494. root.remove(label)
  495. _convertTag(root, 'description', 'help')
  496. _convertTag(root, 'wx-id', 'id')
  497. _convertTag(root, 'module', 'command')
  498. _convertTag(root, 'related-module', 'command')
  499. _convertTagAndRemoveAttrib(root, 'wxgui-item', 'menuitem')
  500. _convertTagAndRemoveAttrib(root, 'module-item', 'menuitem')
  501. root.tag = 'menudata'
  502. i1 = root.find('./items')
  503. # must check because of inconsistent XML menudata file
  504. if i1 is not None:
  505. i1.tag = 'menubar'
  506. _convertTagAndRemoveAttrib(root, 'toolbox', 'menu')
  507. def _getXMLString(root):
  508. """Converts XML tree to string
  509. Since it is usually requier, this function adds a comment (about
  510. autogenerated file) to XML file.
  511. :return: XML as string
  512. """
  513. xml = etree.tostring(root, encoding='UTF-8')
  514. return xml.replace("<?xml version='1.0' encoding='UTF-8'?>\n",
  515. "<?xml version='1.0' encoding='UTF-8'?>\n"
  516. "<!--This is an auto-generated file-->\n")
  517. def do_doctest_gettext_workaround():
  518. """Setups environment for doing a doctest with gettext usage.
  519. When using gettext with dynamically defined underscore function
  520. (`_("For translation")`), doctest does not work properly.
  521. One option is to use `import as` instead of dynamically defined
  522. underscore function but this requires change all modules which are
  523. used by tested module.
  524. The second option is to define dummy underscore function and one
  525. other function which creates the right environment to satisfy all.
  526. This is done by this function. Moreover, `sys.displayhook` and also
  527. `sys.__displayhook__` needs to be redefined too (the later one probably
  528. should not be newer redefined but some cases just requires that).
  529. GRASS specific note is that wxGUI switched to use imported
  530. underscore function for translation. However, GRASS Python libraries
  531. still uses the dynamically defined underscore function, so this
  532. workaround function is still needed when you import something from
  533. GRASS Python libraries.
  534. """
  535. def new_displayhook(string):
  536. """A replacement for default `sys.displayhook`"""
  537. if string is not None:
  538. sys.stdout.write("%r\n" % (string,))
  539. def new_translator(string):
  540. """A fake gettext underscore function."""
  541. return string
  542. sys.displayhook = new_displayhook
  543. sys.__displayhook__ = new_displayhook
  544. import __builtin__
  545. __builtin__._ = new_translator
  546. def doc_test():
  547. """Tests the module using doctest
  548. :return: a number of failed tests
  549. """
  550. import doctest
  551. do_doctest_gettext_workaround()
  552. return doctest.testmod().failed
  553. def module_test():
  554. """Tests the module using test files included in the current
  555. directory and in files from distribution.
  556. """
  557. toolboxesFile = os.path.join(WXGUIDIR, 'xml', 'toolboxes.xml')
  558. userToolboxesFile = 'test.toolboxes_user_toolboxes.xml'
  559. menuFile = 'test.toolboxes_menu.xml'
  560. wxguiItemsFile = os.path.join(WXGUIDIR, 'xml', 'wxgui_items.xml')
  561. moduleItemsFile = os.path.join(WXGUIDIR, 'xml', 'module_items.xml')
  562. toolboxes = etree.parse(toolboxesFile)
  563. userToolboxes = etree.parse(userToolboxesFile)
  564. menu = etree.parse(menuFile)
  565. wxguiItems = etree.parse(wxguiItemsFile)
  566. moduleItems = etree.parse(moduleItemsFile)
  567. tree = toolboxes2menudata(mainMenu=menu,
  568. toolboxes=toolboxes,
  569. userToolboxes=userToolboxes,
  570. wxguiItems=wxguiItems,
  571. moduleItems=moduleItems)
  572. root = tree.getroot()
  573. tested = _getXMLString(root)
  574. # for generating correct test file supposing that the implementation
  575. # is now correct and working
  576. # run the normal test and check the difference before overwriting
  577. # the old correct test file
  578. if len(sys.argv) > 2 and sys.argv[2] == "generate-correct-file":
  579. sys.stdout.write(_getXMLString(root))
  580. return 0
  581. menudataFile = 'test.toolboxes_menudata.xml'
  582. with open(menudataFile) as correctMenudata:
  583. correct = str(correctMenudata.read())
  584. import difflib
  585. differ = difflib.Differ()
  586. result = list(differ.compare(correct.splitlines(True),
  587. tested.splitlines(True)))
  588. someDiff = False
  589. for line in result:
  590. if line.startswith('+') or line.startswith('-'):
  591. sys.stdout.write(line)
  592. someDiff = True
  593. if someDiff:
  594. print "Difference between files."
  595. return 1
  596. else:
  597. print "OK"
  598. return 0
  599. def main():
  600. """Converts the toolboxes files on standard paths to the menudata file
  601. File is written to the standard output.
  602. """
  603. # TODO: fix parameter handling
  604. if len(sys.argv) > 1:
  605. mainFile = os.path.join(WXGUIDIR, 'xml', 'module_tree.xml')
  606. else:
  607. mainFile = os.path.join(WXGUIDIR, 'xml', 'main_menu.xml')
  608. tree = createTree(distributionRootFile=mainFile, userRootFile=None,
  609. userDefined=False)
  610. root = tree.getroot()
  611. sys.stdout.write(_getXMLString(root))
  612. return 0
  613. if __name__ == '__main__':
  614. # TODO: fix parameter handling
  615. if len(sys.argv) > 1:
  616. if sys.argv[1] == 'doctest':
  617. sys.exit(doc_test())
  618. elif sys.argv[1] == 'test':
  619. sys.exit(module_test())
  620. sys.exit(main())