update_menudata.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. for node in data.tree.getiterator():
  49. if node.tag != 'menuitem':
  50. continue
  51. item = dict()
  52. for child in node.getchildren():
  53. item[child.tag] = child.text
  54. if not item.has_key('command'):
  55. continue
  56. module = item['command'].split(' ')[0]
  57. if not modules.has_key(module):
  58. grass.warning("'%s' not found in modules" % item['command'])
  59. continue
  60. if modules[module]['label']:
  61. desc = modules[module]['label']
  62. else:
  63. desc = modules[module]['desc']
  64. node.find('help').text = desc
  65. if node.find('keywords') is not None:
  66. node.find('keywords').text = ','.join(modules[module]['keywords'])
  67. else:
  68. grass.warning('%s: keywords missing' % module)
  69. def writeData(data):
  70. """!Write updated menudata.xml"""
  71. file = os.path.join('..', 'xml', 'menudata.xml')
  72. data.tree.write(file)
  73. def main(argv = None):
  74. if argv is None:
  75. argv = sys.argv
  76. if len(argv) != 1:
  77. print >> sys.stderr, __doc__
  78. return 1
  79. grass.info("Step 1: parsing modules...")
  80. modules = dict()
  81. modules = parseModules()
  82. grass.info("Step 2: reading menu data...")
  83. data = menudata.Data()
  84. grass.info("Step 3: updating menu data...")
  85. updateData(data, modules)
  86. grass.info("Step 4: writing menu data (menudata.xml)...")
  87. writeData(data)
  88. return 0
  89. if __name__ == '__main__':
  90. if os.getenv("GISBASE") is None:
  91. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  92. sys.exit(1)
  93. sys.path.append('../gui_modules')
  94. import menudata
  95. import menuform
  96. import globalvar
  97. sys.exit(main())