build_modules_xml.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """!
  2. @package tools.build_modules_xml
  3. @brief Builds XML metadata of GRASS modules. Runs only during compilation.
  4. (C) 2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Vaclav Petras <wenzeslaus gmail.com>
  8. @author Anna Petrasova <kratochanna gmail.com>
  9. """
  10. import sys
  11. from datetime import datetime
  12. def escapeXML(text):
  13. """!Helper function for correct escaping characters for XML
  14. Duplicate function in core/toolboxes.
  15. """
  16. return text.replace('<', '&lt;').replace("&", '&amp;').replace(">", '&gt;')
  17. def parse_modules(fd):
  18. """!Writes metadata to xml file."""
  19. import grass.script as grass
  20. mlist = list(grass.get_commands()[0]) # what about windows?
  21. indent = 4
  22. for m in mlist:
  23. # TODO: get rid of g.mapsets_picker.py
  24. if m == 'g.mapsets_picker.py':
  25. continue
  26. desc, keyw = get_module_metadata(m)
  27. fd.write('%s<module-item name="%s">\n' % (' ' * indent, m))
  28. indent += 4
  29. fd.write('%s<module>%s</module>\n' % (' ' * indent, m))
  30. fd.write('%s<description>%s</description>\n' % (' ' * indent, escapeXML(desc)))
  31. fd.write('%s<keywords>%s</keywords>\n' % (' ' * indent, escapeXML(','.join(keyw))))
  32. indent -= 4
  33. fd.write('%s</module-item>\n' % (' ' * indent))
  34. def get_module_metadata(name):
  35. import grass.script.task as gtask
  36. try:
  37. task = gtask.parse_interface(name)
  38. except:
  39. return '', ''
  40. return task.get_description(full=True), \
  41. task.get_keywords()
  42. def header(fd):
  43. import grass.script.core as grass
  44. fd.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  45. fd.write('<!DOCTYPE module-items SYSTEM "module_items.dtd">\n')
  46. fd.write('<!--This file is automatically generated using %s-->\n' % sys.argv[0])
  47. vInfo = grass.version()
  48. fd.write('<!--version="%s" revision="%s" date="%s"-->\n' % \
  49. (vInfo['version'].split('.')[0],
  50. vInfo['revision'],
  51. datetime.now()))
  52. fd.write('<module-items>\n')
  53. def footer(fd):
  54. fd.write('</module-items>\n')
  55. def main():
  56. fh = sys.stdout
  57. header(fh)
  58. parse_modules(fh)
  59. footer(fh)
  60. return 0
  61. if __name__ == "__main__":
  62. sys.exit(main())