瀏覽代碼

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 https://trac.osgeo.org/grass/changeset/68319)
to keep it simple. Traceback is enough for a script.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68394 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 9 年之前
父節點
當前提交
898e2ddf24
共有 1 個文件被更改,包括 51 次插入6 次删除
  1. 51 6
      gui/wxpython/gui_core/pyedit.py

+ 51 - 6
gui/wxpython/gui_core/pyedit.py

@@ -146,15 +146,11 @@ def script_example():
     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,6 +204,49 @@ if __name__ == "__main__":
     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?
@@ -377,6 +416,10 @@ class PyEditController(object):
         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 @@ class PyEditFrame(wx.Frame):
         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 @@ class PyEditFrame(wx.Frame):
     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)