[GRASS-SVN] r57815 - in grass/trunk/gui/wxpython: core tools

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Sep 22 20:28:30 PDT 2013


Author: wenzeslaus
Date: 2013-09-22 20:28:30 -0700 (Sun, 22 Sep 2013)
New Revision: 57815

Modified:
   grass/trunk/gui/wxpython/core/toolboxes.py
   grass/trunk/gui/wxpython/tools/build_modules_xml.py
Log:
wxGUI/toolboxes: improving error handling in module items generation and doctest workaround

Modified: grass/trunk/gui/wxpython/core/toolboxes.py
===================================================================
--- grass/trunk/gui/wxpython/core/toolboxes.py	2013-09-23 02:49:17 UTC (rev 57814)
+++ grass/trunk/gui/wxpython/core/toolboxes.py	2013-09-23 03:28:30 UTC (rev 57815)
@@ -502,6 +502,12 @@
     >>> _expandRuntimeModules(tree)
     >>> etree.tostring(tree)
     '<items><module-item name="g.region"><module>g.region</module><description>Manages the boundary definitions for the geographic region.</description><keywords>general,settings</keywords></module-item></items>'
+    >>> tree = etree.fromstring('<items>'
+    ...                         '<module-item name="m.proj"></module-item>'
+    ...                         '</items>')
+    >>> _expandRuntimeModules(tree)
+    >>> etree.tostring(tree)
+    '<items><module-item name="m.proj"><module>m.proj</module><description>Converts coordinates from one projection to another (cs2cs frontend).</description><keywords>miscellaneous,projection</keywords></module-item></items>'
     """
     modules = node.findall('.//module-item')
     for module in modules:
@@ -632,12 +638,22 @@
     """Setups environment for doing a doctest with gettext usage.
 
     When using gettext with dynamically defined underscore function
-    (`_("For translation")`), doctest does not work properly. One option is to
-    use `import as` instead of dynamically defined underscore function but this
-    would require change all modules which are used by tested module. This
-    should be considered for the future. The second option is to define dummy
-    underscore function and one other function which creates the right
-    environment to satisfy all. This is done by this function.
+    (`_("For translation")`), doctest does not work properly.
+
+    One option is to use `import as` instead of dynamically defined underscore
+    function but this requires change all modules which are used by tested
+    module.
+
+    The second option is to define dummy underscore function and one other
+    function which creates the right environment to satisfy all. This is done
+    by this function. Moreover, `sys.displayhook` and also
+    `sys.__displayhook__` needs to be redefined too (the later one probably
+    should not be newer redefined but some cases just requires that).
+
+    GRASS specific note is that wxGUI switched to use imported underscore
+    function for translation. However, GRASS Python libraries still uses the
+    dynamically defined underscore function, so this workaround function is
+    still needed when you import something from GRASS Python libraries.
     """
     def new_displayhook(string):
         """A replacement for default `sys.displayhook`"""
@@ -649,6 +665,7 @@
         return string
 
     sys.displayhook = new_displayhook
+    sys.__displayhook__ = new_displayhook
 
     import __builtin__
     __builtin__._ = new_translator
@@ -689,7 +706,7 @@
     root = tree.getroot()
     tested = _getXMLString(root)
 
-    # for generatiing correct test file supposing that the implementation
+    # for generating correct test file supposing that the implementation
     # is now correct and working
     # run the normal test and check the difference before overwriting
     # the old correct test file

Modified: grass/trunk/gui/wxpython/tools/build_modules_xml.py
===================================================================
--- grass/trunk/gui/wxpython/tools/build_modules_xml.py	2013-09-23 02:49:17 UTC (rev 57814)
+++ grass/trunk/gui/wxpython/tools/build_modules_xml.py	2013-09-23 03:28:30 UTC (rev 57815)
@@ -15,23 +15,45 @@
 import sys
 from datetime import datetime
 
+import grass.script.core as gcore
+import grass.script.task as gtask
 
+
 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(">", '>')
 
 
+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):
     """!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
     for m in mlist:
         # 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
         desc, keyw = get_module_metadata(m)
         fd.write('%s<module-item name="%s">\n' % (' ' * indent, m))
@@ -44,10 +66,20 @@
 
 
 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:
         task = gtask.parse_interface(name)
     except:
+        sys.stderr.write("Cannot parse interface for module %s. Empty strings"
+                         " will be placed instead of description and keywords."
+                         "\n" % name)
         return '', ''
 
     return task.get_description(full=True), \
@@ -55,11 +87,10 @@
 
 
 def header(fd):
-    import grass.script.core as grass
     fd.write('<?xml version="1.0" encoding="UTF-8"?>\n')
     fd.write('<!DOCTYPE module-items SYSTEM "module_items.dtd">\n')
     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' % \
                  (vInfo['version'].split('.')[0],
                   vInfo['revision'],
@@ -71,6 +102,32 @@
     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():
     fh = sys.stdout
 
@@ -82,4 +139,11 @@
 
 
 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())



More information about the grass-commit mailing list