[GRASS-SVN] r58653 - in grass/trunk/lib/python: pygrass/messages script temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 9 13:18:47 PST 2014


Author: huhabla
Date: 2014-01-09 13:18:47 -0800 (Thu, 09 Jan 2014)
New Revision: 58653

Modified:
   grass/trunk/lib/python/pygrass/messages/__init__.py
   grass/trunk/lib/python/script/core.py
   grass/trunk/lib/python/temporal/core.py
Log:
Better handling of fatal error behavior in temporal framework and the pygrass messenger


Modified: grass/trunk/lib/python/pygrass/messages/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/messages/__init__.py	2014-01-09 20:34:13 UTC (rev 58652)
+++ grass/trunk/lib/python/pygrass/messages/__init__.py	2014-01-09 21:18:47 UTC (rev 58653)
@@ -157,7 +157,23 @@
          File "__init__.py", line 241, in fatal
            raise FatalError(message)
        FatalError: Ohh no no no!
+       
+       >>> msgr = Messenger(raise_on_error=True)
+       >>> msgr.set_raise_on_error(False)
+       >>> msgr.fatal("Ohh no no no!")
+       Traceback (most recent call last):
+         File "__init__.py", line 239, in fatal
+           sys.exit(1)
+       SystemExit: 1
 
+       >>> msgr = Messenger(raise_on_error=False)
+       >>> msgr.set_raise_on_error(True)
+       >>> msgr.fatal("Ohh no no no!")
+       Traceback (most recent call last):
+         File "__init__.py", line 241, in fatal
+           raise FatalError(message)
+       FatalError: Ohh no no no!
+
        @endcode
     """
     def __init__(self, raise_on_error=False):
@@ -272,6 +288,25 @@
         if self.client_conn is not None:
             self.client_conn.close()
 
+    def set_raise_on_error(self, raise_on_error=True):
+        """!Set the fatal error behavior
+        
+           - If raise_on_error == True, a FatalError exception will be raised if fatal() is called
+           - If raise_on_error == False, sys.exit(1) will be invoked if fatal() is called
+        
+           @param raise_on_error If True a FatalError exception will be raised instead 
+                 of calling sys.exit(1)
+        """
+        self.raise_on_error = raise_on_error
+    
+    def get_raise_on_error(self):
+        """!Get the fatal error behavior
+        
+           @return True if a FatalError exception will be raised 
+                   or False if sys.exit(1) will be called in case of invoking fatal()
+        """
+        return self.raise_on_error
+            
     def test_fatal_error(self, message):
         """!Force the messenger server to call G_fatal_error()
         """

Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2014-01-09 20:34:13 UTC (rev 58652)
+++ grass/trunk/lib/python/script/core.py	2014-01-09 21:18:47 UTC (rev 58653)
@@ -588,6 +588,14 @@
     raise_on_error = raise_exp
     return tmp_raise
 
+
+def get_raise_on_error():
+    """!Return True if a ScriptError exception is raised instead of calling
+       sys.exit(1) in case a fatal error was invoked with fatal()
+    """
+    global raise_on_error
+    return raise_on_error
+
 # interface to g.parser
 
 

Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2014-01-09 20:34:13 UTC (rev 58652)
+++ grass/trunk/lib/python/temporal/core.py	2014-01-09 21:18:47 UTC (rev 58653)
@@ -262,6 +262,68 @@
 
 ###############################################################################
 
+# Set this variable True to raise a FatalError exception 
+# in case a fatal error occurs using the messenger interface 
+raise_on_error = False
+
+def set_raise_on_error(raise_exp=True):
+    """!Define behaviour on fatal error, invoked using the tgis messenger
+    interface (msgr.fatal())
+    
+    The messenger interface will be restarted using the new error policy
+
+    @param raise_exp True to raise a FatalError exception instead of calling
+    sys.exit(1) when using the tgis messenger interface
+    
+    @code
+    
+    >>> import grass.temporal as tgis
+    >>> tgis.init()
+    >>> ignore = tgis.set_raise_on_error(False)
+    >>> msgr = tgis.get_tgis_message_interface()
+    >>> tgis.get_raise_on_error()
+    False
+    >>> msgr.fatal("Ohh no no no!")
+    Traceback (most recent call last):
+      File "__init__.py", line 239, in fatal
+        sys.exit(1)
+    SystemExit: 1
+       
+    >>> tgis.set_raise_on_error(True)
+    False
+    >>> msgr.fatal("Ohh no no no!")
+    Traceback (most recent call last):
+      File "__init__.py", line 241, in fatal
+        raise FatalError(message)
+    FatalError: Ohh no no no!
+    
+    @endcode
+
+    @return current status
+    """
+    global raise_on_error
+    tmp_raise = raise_on_error
+    raise_on_error = raise_exp
+
+    global message_interface
+    if message_interface:
+        message_interface.set_raise_on_error(raise_on_error)
+    else:
+        _init_tgis_message_interface(raise_on_error)
+
+    return tmp_raise
+
+
+def get_raise_on_error():
+    """!Return True if a FatalError exception is raised instead of calling
+       sys.exit(1) in case a fatal error was invoked with msgr.fatal()
+    """
+    global raise_on_error
+    return raise_on_error
+
+
+###############################################################################
+
 def get_tgis_version():
     """!Get the verion number of the temporal framework
        @return The version number of the temporal framework as string
@@ -377,6 +439,7 @@
     global tgis_database
     global tgis_database_string
     global tgis_dbmi_paramstyle
+    global raise_on_error
     global enable_mapset_check
     global enable_timestamp_write
     global current_mapset
@@ -399,6 +462,11 @@
     if os.getenv("GRASS_TGIS_RAISE_ON_ERROR") is not None:
         raise_on_error = True
 
+    # Check if the script library raises on error, 
+    # if so we do the same
+    if core.get_raise_on_error() is True:
+        raise_on_error = True
+        
     # Start the GRASS message interface server
     _init_tgis_message_interface(raise_on_error)
     # Start the C-library interface server



More information about the grass-commit mailing list