|
@@ -15,23 +15,45 @@ This program is free software under the GNU General Public License
|
|
import sys
|
|
import sys
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
+import grass.script.core as gcore
|
|
|
|
+import grass.script.task as gtask
|
|
|
|
+
|
|
|
|
|
|
def escapeXML(text):
|
|
def escapeXML(text):
|
|
- """!Helper function for correct escaping characters for XML
|
|
|
|
|
|
+ """!This is a duplicate of function in core/toolboxes.
|
|
|
|
|
|
- Duplicate function in core/toolboxes.
|
|
|
|
|
|
+ >>> escapeXML('<>&')
|
|
|
|
+ '&lt;>&'
|
|
"""
|
|
"""
|
|
return text.replace('<', '<').replace("&", '&').replace(">", '>')
|
|
return text.replace('<', '<').replace("&", '&').replace(">", '>')
|
|
|
|
|
|
|
|
|
|
|
|
+def do_doctest_gettext_workaround():
|
|
|
|
+ """This is a duplicate of function in core/toolboxes."""
|
|
|
|
+ def new_displayhook(string):
|
|
|
|
+ """A replacement for default `sys.displayhook`"""
|
|
|
|
+ if string is not None:
|
|
|
|
+ sys.stdout.write("%r\n" % (string,))
|
|
|
|
+
|
|
|
|
+ def new_translator(string):
|
|
|
|
+ """A fake gettext underscore function."""
|
|
|
|
+ return string
|
|
|
|
+
|
|
|
|
+ sys.displayhook = new_displayhook
|
|
|
|
+ sys.__displayhook__ = new_displayhook
|
|
|
|
+
|
|
|
|
+ import __builtin__
|
|
|
|
+ __builtin__._ = new_translator
|
|
|
|
+
|
|
|
|
+
|
|
def parse_modules(fd):
|
|
def parse_modules(fd):
|
|
"""!Writes metadata to xml file."""
|
|
"""!Writes metadata to xml file."""
|
|
- import grass.script as grass
|
|
|
|
- mlist = list(grass.get_commands()[0]) # what about windows?
|
|
|
|
|
|
+ # TODO: what about ms windows? does gtask handle this?
|
|
|
|
+ mlist = list(gcore.get_commands()[0])
|
|
indent = 4
|
|
indent = 4
|
|
for m in mlist:
|
|
for m in mlist:
|
|
# TODO: get rid of g.mapsets_picker.py
|
|
# TODO: get rid of g.mapsets_picker.py
|
|
- if m == 'g.mapsets_picker.py':
|
|
|
|
|
|
+ if m == 'g.mapsets_picker.py' or m == 'g.parser':
|
|
continue
|
|
continue
|
|
desc, keyw = get_module_metadata(m)
|
|
desc, keyw = get_module_metadata(m)
|
|
fd.write('%s<module-item name="%s">\n' % (' ' * indent, m))
|
|
fd.write('%s<module-item name="%s">\n' % (' ' * indent, m))
|
|
@@ -44,10 +66,20 @@ def parse_modules(fd):
|
|
|
|
|
|
|
|
|
|
def get_module_metadata(name):
|
|
def get_module_metadata(name):
|
|
- import grass.script.task as gtask
|
|
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ >>> get_module_metadata('g.region')
|
|
|
|
+ ('Manages the boundary definitions for the geographic region.', ['general', 'settings'])
|
|
|
|
+ >>> get_module_metadata('m.proj')
|
|
|
|
+ ('Converts coordinates from one projection to another (cs2cs frontend).', ['miscellaneous', 'projection'])
|
|
|
|
+ """
|
|
|
|
+ task = gtask.parse_interface(name)
|
|
try:
|
|
try:
|
|
task = gtask.parse_interface(name)
|
|
task = gtask.parse_interface(name)
|
|
except:
|
|
except:
|
|
|
|
+ sys.stderr.write("Cannot parse interface for module %s. Empty strings"
|
|
|
|
+ " will be placed instead of description and keywords."
|
|
|
|
+ "\n" % name)
|
|
return '', ''
|
|
return '', ''
|
|
|
|
|
|
return task.get_description(full=True), \
|
|
return task.get_description(full=True), \
|
|
@@ -55,11 +87,10 @@ def get_module_metadata(name):
|
|
|
|
|
|
|
|
|
|
def header(fd):
|
|
def header(fd):
|
|
- import grass.script.core as grass
|
|
|
|
fd.write('<?xml version="1.0" encoding="UTF-8"?>\n')
|
|
fd.write('<?xml version="1.0" encoding="UTF-8"?>\n')
|
|
fd.write('<!DOCTYPE module-items SYSTEM "module_items.dtd">\n')
|
|
fd.write('<!DOCTYPE module-items SYSTEM "module_items.dtd">\n')
|
|
fd.write('<!--This file is automatically generated using %s-->\n' % sys.argv[0])
|
|
fd.write('<!--This file is automatically generated using %s-->\n' % sys.argv[0])
|
|
- vInfo = grass.version()
|
|
|
|
|
|
+ vInfo = gcore.version()
|
|
fd.write('<!--version="%s" revision="%s" date="%s"-->\n' % \
|
|
fd.write('<!--version="%s" revision="%s" date="%s"-->\n' % \
|
|
(vInfo['version'].split('.')[0],
|
|
(vInfo['version'].split('.')[0],
|
|
vInfo['revision'],
|
|
vInfo['revision'],
|
|
@@ -71,6 +102,32 @@ def footer(fd):
|
|
fd.write('</module-items>\n')
|
|
fd.write('</module-items>\n')
|
|
|
|
|
|
|
|
|
|
|
|
+def doc_test():
|
|
|
|
+ """Tests the module using doctest
|
|
|
|
+
|
|
|
|
+ @return a number of failed tests
|
|
|
|
+ """
|
|
|
|
+ import doctest
|
|
|
|
+ do_doctest_gettext_workaround()
|
|
|
|
+ return doctest.testmod().failed
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def module_test():
|
|
|
|
+ grass_commands = gcore.get_commands()[0]
|
|
|
|
+ if not 'g.region' in grass_commands:
|
|
|
|
+ print "No g.region"
|
|
|
|
+ return 1
|
|
|
|
+ if not 'm.proj' in grass_commands:
|
|
|
|
+ print "No m.proj"
|
|
|
|
+ return 1
|
|
|
|
+ if not 't.rast.univar' in grass_commands:
|
|
|
|
+ print "No t.rast.univar"
|
|
|
|
+ return 1
|
|
|
|
+ print get_module_metadata('g.region')
|
|
|
|
+ print get_module_metadata('m.proj')
|
|
|
|
+ print get_module_metadata('t.rast.univar')
|
|
|
|
+
|
|
|
|
+
|
|
def main():
|
|
def main():
|
|
fh = sys.stdout
|
|
fh = sys.stdout
|
|
|
|
|
|
@@ -82,4 +139,11 @@ def main():
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
|
+ if len(sys.argv) > 1:
|
|
|
|
+ if sys.argv[1] == 'doctest':
|
|
|
|
+ sys.exit(doc_test())
|
|
|
|
+ elif sys.argv[1] == 'test':
|
|
|
|
+ sys.exit(module_test())
|
|
|
|
+ else:
|
|
|
|
+ gcore.fatal('Unrecognized parameter.')
|
|
sys.exit(main())
|
|
sys.exit(main())
|