update_menudata.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-2010 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 [-d]
  12. -d - dry run (prints diff, file is not updated)
  13. @author Martin Landa <landa.martin gmail.com>
  14. """
  15. import os
  16. import sys
  17. import tempfile
  18. try:
  19. import xml.etree.ElementTree as etree
  20. except ImportError:
  21. import elementtree.ElementTree as etree # Python <= 2.4
  22. from grass.script import core as grass
  23. def parseModules():
  24. """!Parse modules' interface"""
  25. modules = dict()
  26. # list of modules to be ignored
  27. ignore = [ 'g.mapsets_picker.py',
  28. 'v.type_wrapper.py',
  29. 'g.parser',
  30. 'vcolors' ]
  31. count = len(globalvar.grassCmd['all'])
  32. i = 0
  33. for module in globalvar.grassCmd['all']:
  34. i += 1
  35. if i % 10 == 0:
  36. grass.info('* %d/%d' % (i, count))
  37. if module in ignore:
  38. continue
  39. try:
  40. interface = menuform.GUI().ParseInterface(cmd = [module])
  41. except IOError, e:
  42. grass.error(e)
  43. continue
  44. modules[interface.name] = { 'label' : interface.label,
  45. 'desc' : interface.description,
  46. 'keywords': interface.keywords }
  47. return modules
  48. def updateData(data, modules):
  49. """!Update menu data tree"""
  50. # list of modules to be ignored
  51. ignore = [ 'v.type_wrapper.py',
  52. 'vcolors' ]
  53. menu_modules = []
  54. for node in data.tree.getiterator():
  55. if node.tag != 'menuitem':
  56. continue
  57. item = dict()
  58. for child in node.getchildren():
  59. item[child.tag] = child.text
  60. if not item.has_key('command'):
  61. continue
  62. if item['command'] in ignore:
  63. continue
  64. module = item['command'].split(' ')[0]
  65. if not modules.has_key(module):
  66. grass.warning("'%s' not found in modules" % item['command'])
  67. continue
  68. if modules[module]['label']:
  69. desc = modules[module]['label']
  70. else:
  71. desc = modules[module]['desc']
  72. node.find('help').text = desc
  73. if not modules[module].has_key('keywords'):
  74. grass.warning('%s: keywords missing' % module)
  75. else:
  76. if node.find('keywords') is None:
  77. node.insert(2, etree.Element('keywords'))
  78. grass.warning("Adding tag 'keywords' to '%s'" % module)
  79. node.find('keywords').text = ','.join(modules[module]['keywords'])
  80. menu_modules.append(item['command'])
  81. for module in modules.keys():
  82. if module not in menu_modules:
  83. grass.warning("'%s' not available from the menu" % module)
  84. def writeData(data, file = None):
  85. """!Write updated menudata.xml"""
  86. if file is None:
  87. file = os.path.join('xml', 'menudata.xml')
  88. try:
  89. data.tree.write(file)
  90. except IOError:
  91. print >> sys.stderr, "'%s' not found. Please run the script from 'gui/wxpython'." % file
  92. return
  93. try:
  94. f = open(file, 'a')
  95. try:
  96. f.write('\n')
  97. finally:
  98. f.close()
  99. except IOError:
  100. print >> sys.stderr, "ERROR: Unable to write to menudata file."
  101. def main(argv = None):
  102. if argv is None:
  103. argv = sys.argv
  104. if len(argv) > 1 and argv[1] == '-d':
  105. printDiff = True
  106. else:
  107. printDiff = False
  108. if len(argv) > 1 and argv[1] == '-h':
  109. print >> sys.stderr, __doc__
  110. return 1
  111. grass.info("Step 1: parsing modules...")
  112. modules = dict()
  113. modules = parseModules()
  114. grass.info("Step 2: reading menu data...")
  115. data = menudata.Data()
  116. grass.info("Step 3: updating menu data...")
  117. updateData(data, modules)
  118. if printDiff:
  119. tempFile = tempfile.NamedTemporaryFile()
  120. grass.info("Step 4: writing menu data...")
  121. writeData(data, tempFile.name)
  122. nuldev = file(os.devnull, 'w+')
  123. grass.call(['diff', '-u',
  124. os.path.join('xml', 'menudata.xml'),
  125. tempFile.name], stderr = nuldev)
  126. else:
  127. grass.info("Step 4: writing menu data (menudata.xml)...")
  128. writeData(data)
  129. return 0
  130. if __name__ == '__main__':
  131. if os.getenv("GISBASE") is None:
  132. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  133. sys.exit(1)
  134. sys.path.append(os.path.join(os.getenv("GISBASE"), 'etc', 'wxpython', 'gui_modules'))
  135. import menudata
  136. import menuform
  137. import globalvar
  138. sys.exit(main())