update_menudata.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 support/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. menu_modules = []
  52. for node in data.tree.getiterator():
  53. if node.tag != 'menuitem':
  54. continue
  55. item = dict()
  56. for child in node.getchildren():
  57. item[child.tag] = child.text
  58. if not item.has_key('command'):
  59. continue
  60. if item['command'] in ignore:
  61. continue
  62. module = item['command'].split(' ')[0]
  63. if not modules.has_key(module):
  64. grass.warning("'%s' not found in modules" % item['command'])
  65. continue
  66. if modules[module]['label']:
  67. desc = modules[module]['label']
  68. else:
  69. desc = modules[module]['desc']
  70. node.find('help').text = desc
  71. if not modules[module].has_key('keywords'):
  72. grass.warning('%s: keywords missing' % module)
  73. else:
  74. if node.find('keywords') is None:
  75. node.insert(2, etree.Element('keywords'))
  76. grass.warning("Adding tag 'keywords' to '%s'" % module)
  77. node.find('keywords').text = ','.join(modules[module]['keywords'])
  78. menu_modules.append(item['command'])
  79. for module in modules.keys():
  80. if module not in menu_modules:
  81. grass.warning("'%s' not available from the menu" % module)
  82. def writeData(data):
  83. """!Write updated menudata.xml"""
  84. file = os.path.join('xml', 'menudata.xml')
  85. try:
  86. if not os.path.exists(file):
  87. raise IOError
  88. data.tree.write(file)
  89. except IOError:
  90. print >> sys.stderr, "'%s' not found. Please run the script from 'gui/wxpython'." % file
  91. def main(argv = None):
  92. if argv is None:
  93. argv = sys.argv
  94. if len(argv) != 1:
  95. print >> sys.stderr, __doc__
  96. return 1
  97. grass.info("Step 1: parsing modules...")
  98. modules = dict()
  99. modules = parseModules()
  100. grass.info("Step 2: reading menu data...")
  101. data = menudata.Data()
  102. grass.info("Step 3: updating menu data...")
  103. updateData(data, modules)
  104. grass.info("Step 4: writing menu data (menudata.xml)...")
  105. writeData(data)
  106. return 0
  107. if __name__ == '__main__':
  108. if os.getenv("GISBASE") is None:
  109. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  110. sys.exit(1)
  111. sys.path.append(os.path.join(os.getenv("GISBASE"), 'etc', 'wxpython', 'gui_modules'))
  112. import menudata
  113. import menuform
  114. import globalvar
  115. sys.exit(main())