update_menudata.py 5.1 KB

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