update_menudata.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. from grass.script import core as grass
  21. def parseModules():
  22. """!Parse modules' interface"""
  23. modules = dict()
  24. # list of modules to be ignored
  25. ignore = [ 'g.mapsets_picker.py',
  26. 'v.type_wrapper.py',
  27. 'g.parser',
  28. 'vcolors' ]
  29. count = len(globalvar.grassCmd['all'])
  30. i = 0
  31. for module in globalvar.grassCmd['all']:
  32. i += 1
  33. if i % 10 == 0:
  34. grass.info('* %d/%d' % (i, count))
  35. if module in ignore:
  36. continue
  37. try:
  38. interface = menuform.GUI().ParseInterface(cmd = [module])
  39. except IOError, e:
  40. grass.error(e)
  41. continue
  42. modules[interface.name] = { 'label' : interface.label,
  43. 'desc' : interface.description,
  44. 'keywords': interface.keywords }
  45. return modules
  46. def updateData(data, modules):
  47. """!Update menu data tree"""
  48. # list of modules to be ignored
  49. ignore = [ 'v.type_wrapper.py',
  50. 'vcolors' ]
  51. for node in data.tree.getiterator():
  52. if node.tag != 'menuitem':
  53. continue
  54. item = dict()
  55. for child in node.getchildren():
  56. item[child.tag] = child.text
  57. if not item.has_key('command'):
  58. continue
  59. if item['command'] in ignore:
  60. continue
  61. module = item['command'].split(' ')[0]
  62. if not modules.has_key(module):
  63. grass.warning("'%s' not found in modules" % item['command'])
  64. continue
  65. if modules[module]['label']:
  66. desc = modules[module]['label']
  67. else:
  68. desc = modules[module]['desc']
  69. node.find('help').text = desc
  70. if node.find('keywords') is not None:
  71. node.find('keywords').text = ','.join(modules[module]['keywords'])
  72. else:
  73. grass.warning('%s: keywords missing' % module)
  74. def writeData(data):
  75. """!Write updated menudata.xml"""
  76. file = os.path.join('..', 'xml', 'menudata.xml')
  77. try:
  78. data.tree.write(file)
  79. except IOError:
  80. print >> sys.stderr, "'menudata.xml' not found. Please run the script from 'gui/wxpython/support'."
  81. def main(argv = None):
  82. if argv is None:
  83. argv = sys.argv
  84. if len(argv) != 1:
  85. print >> sys.stderr, __doc__
  86. return 1
  87. grass.info("Step 1: parsing modules...")
  88. modules = dict()
  89. modules = parseModules()
  90. grass.info("Step 2: reading menu data...")
  91. data = menudata.Data()
  92. grass.info("Step 3: updating menu data...")
  93. updateData(data, modules)
  94. grass.info("Step 4: writing menu data (menudata.xml)...")
  95. writeData(data)
  96. return 0
  97. if __name__ == '__main__':
  98. if os.getenv("GISBASE") is None:
  99. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  100. sys.exit(1)
  101. sys.path.append(os.path.join(os.getenv("GISBASE"), 'etc', 'wxpython', 'gui_modules'))
  102. import menudata
  103. import menuform
  104. import globalvar
  105. sys.exit(main())