update_menudata.py 5.1 KB

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