update_menudata.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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", "v.type_wrapper.py", "g.parser", "vcolors"]
  32. count = len(grassCmd)
  33. i = 0
  34. for module in grassCmd:
  35. i += 1
  36. if i % 10 == 0:
  37. grass.info("* %d/%d" % (i, count))
  38. if module in ignore:
  39. continue
  40. try:
  41. interface = gtask.parse_interface(module)
  42. except Exception as e:
  43. grass.error(module + ": " + str(e))
  44. continue
  45. modules[interface.name] = {
  46. "label": interface.label,
  47. "desc": interface.description,
  48. "keywords": interface.keywords,
  49. }
  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", "vcolors"]
  55. menu_modules = list()
  56. for node in data.tree.iter():
  57. if node.tag != "menuitem":
  58. continue
  59. item = dict()
  60. for child in node:
  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(
  95. "'%s' not found." " Please run the script from 'gui/wxpython'." % file,
  96. file=sys.stderr,
  97. )
  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("ERROR: Unable to write to menudata file.", file=sys.stderr)
  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__, file=sys.stderr)
  116. return 1
  117. nuldev = open(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 = LayerManagerMenuData()
  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(
  132. ["diff", "-u", os.path.join("xml", "menudata.xml"), tempFile.name],
  133. stderr=nuldev,
  134. )
  135. else:
  136. grass.info("Step 5: writing menu data (menudata.xml)...")
  137. writeData(data)
  138. return 0
  139. if __name__ == "__main__":
  140. if os.getenv("GISBASE") is None:
  141. sys.exit("You must be in GRASS GIS to run this program.")
  142. sys.exit(main())