[GRASS-SVN] r68394 - grass/trunk/gui/wxpython/gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Sat May 7 10:04:16 PDT 2016


Author: wenzeslaus
Date: 2016-05-07 10:04:16 -0700 (Sat, 07 May 2016)
New Revision: 68394

Modified:
   grass/trunk/gui/wxpython/gui_core/pyedit.py
Log:
pyedit: provide error handling example

The module example doesn't need any error handling because
it is already done in the mapcalc function, so new
example is needed.

Removing error handling from a script example (from r68319)
to keep it simple. Traceback is enough for a script.


Modified: grass/trunk/gui/wxpython/gui_core/pyedit.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/pyedit.py	2016-05-07 16:52:41 UTC (rev 68393)
+++ grass/trunk/gui/wxpython/gui_core/pyedit.py	2016-05-07 17:04:16 UTC (rev 68394)
@@ -146,15 +146,11 @@
     return r"""#!/usr/bin/env python
 
 import grass.script as gscript
-from grass.exceptions import CalledModuleError
 
 def main():
     input_raster = 'elevation'
     output_raster = 'high_areas'
-    try:
-        stats = gscript.parse_command('r.univar', map='elevation', flags='g')
-    except CalledModuleError as e:
-        gscript.fatal('{}'.format(e))
+    stats = gscript.parse_command('r.univar', map='elevation', flags='g')
     raster_mean = float(stats['mean'])
     raster_stddev = float(stats['stddev'])
     raster_high = raster_mean + raster_stddev
@@ -208,7 +204,50 @@
     sys.exit(main())
 """
 
+def module_error_handling_example():
+    """Example of a GRASS module"""
+    return r"""#!/usr/bin/env python
 
+#%module
+#% description: Selects values from raster above value of mean plus standard deviation
+#% keyword: raster
+#% keyword: select
+#% keyword: standard deviation
+#%end
+#%option G_OPT_R_INPUT
+#%end
+#%option G_OPT_R_OUTPUT
+#%end
+
+
+import sys
+
+import grass.script as gscript
+from grass.exceptions import CalledModuleError
+
+
+def main():
+    options, flags = gscript.parser()
+    input_raster = options['input']
+    output_raster = options['output']
+
+    try:
+        stats = gscript.parse_command('r.univar', map=input_raster, flags='g')
+    except CalledModuleError as e:
+        gscript.fatal('{}'.format(e))
+    raster_mean = float(stats['mean'])
+    raster_stddev = float(stats['stddev'])
+    raster_high = raster_mean + raster_stddev
+    gscript.mapcalc('{r} = {i} > {v}'.format(r=output_raster, i=input_raster,
+                                             v=raster_high))
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
+"""
+
+
 class PyEditController(object):
     # using the naming GUI convention, change for controller?
     # pylint: disable=invalid-name
@@ -377,6 +416,10 @@
         if self.CanReplaceContent('template'):
             self.body.SetText(module_example())
 
+    def SetModuleErrorHandlingExample(self, event):
+        if self.CanReplaceContent('template'):
+            self.body.SetText(module_error_handling_example())
+
     def CanReplaceContent(self, by_message):
         if by_message == 'template':
             message = _("Replace the content by the template?")
@@ -531,7 +574,6 @@
         self.controller.OnRun(*args, **kwargs)
 
     def OnHelp(self, *args, **kwargs):
-        # save without asking
         self.controller.OnHelp(*args, **kwargs)
 
     def OnSimpleScriptTemplate(self, *args, **kwargs):
@@ -546,6 +588,9 @@
     def OnGrassModuleExample(self, *args, **kwargs):
         self.controller.SetModuleExample(*args, **kwargs)
 
+    def OnGrassModuleErrorHandlingExample(self, *args, **kwargs):
+        self.controller.SetModuleErrorHandlingExample(*args, **kwargs)
+
     def OnPythonHelp(self, *args, **kwargs):
         self.controller.OnPythonHelp(*args, **kwargs)
 



More information about the grass-commit mailing list