update_menudata.py 5.1 KB

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