update_menudata.py 4.9 KB

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