update_menudata.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. @brief Support script for wxGUI - only for developers needs. Updates
  3. menudata.xml file.
  4. Parse all GRASS modules in the search path ('bin' & 'script') and
  5. updates: - description (i.e. help) - keywords
  6. Prints warning for missing modules.
  7. (C) 2008-2009 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. Usage: python update_menudata.py
  12. @author Martin Landa <landa.martin gmail.com>
  13. """
  14. import os
  15. import sys
  16. try:
  17. import xml.etree.ElementTree as etree
  18. except ImportError:
  19. import elementtree.ElementTree as etree # Python <= 2.4
  20. def parseModules():
  21. """!Parse modules' interface"""
  22. modules = dict()
  23. # list of modules to be ignored
  24. ignore = [ 'g.mapsets_picker.py',
  25. 'v.type_wrapper.py' ]
  26. count = len(globalvar.grassCmd['all'])
  27. i = 0
  28. for module in globalvar.grassCmd['all']:
  29. i += 1
  30. if i % 10 == 0:
  31. print ' %d/%d' % (i, count)
  32. if module in ignore:
  33. continue
  34. try:
  35. interface = menuform.GUI().ParseInterface(cmd = [module])
  36. except IOError, e:
  37. print >> sys.stderr, e
  38. continue
  39. modules[interface.name] = { 'label' : interface.label,
  40. 'desc' : interface.description,
  41. 'keywords': interface.keywords }
  42. return modules
  43. def updateData(data, modules):
  44. """!Update menu data tree"""
  45. for node in data.tree.getiterator():
  46. if node.tag != 'menuitem':
  47. continue
  48. item = dict()
  49. for child in node.getchildren():
  50. item[child.tag] = child.text
  51. if not item.has_key('command'):
  52. continue
  53. module = item['command'].split(' ')[0]
  54. if not modules.has_key(module):
  55. print 'WARNING: \'%s\' not found in modules' % item['command']
  56. continue
  57. if modules[module]['label']:
  58. desc = modules[module]['label']
  59. else:
  60. desc = modules[module]['desc']
  61. node.find('help').text = desc
  62. node.find('keywords').text = ','.join(modules[module]['keywords'])
  63. def writeData(data):
  64. """!Write updated menudata.xml"""
  65. file = os.path.join('..', 'xml', 'menudata.xml')
  66. data.tree.write(file)
  67. def main(argv = None):
  68. if argv is None:
  69. argv = sys.argv
  70. if len(argv) != 1:
  71. print >> sys.stderr, __doc__
  72. return 1
  73. print "Step 1: parse modules..."
  74. modules = dict()
  75. modules = parseModules()
  76. print "Step 2: read menu data..."
  77. data = menudata.Data()
  78. print "Step 3: update menu data..."
  79. updateData(data, modules)
  80. print "Step 4: write menu data (menudata.xml)..."
  81. writeData(data)
  82. return 0
  83. if __name__ == '__main__':
  84. if os.getenv("GISBASE") is None:
  85. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  86. sys.exit(1)
  87. sys.path.append('../gui_modules')
  88. import menudata
  89. import menuform
  90. import globalvar
  91. sys.exit(main())