Преглед изворни кода

Better handling of fatal error behavior in temporal framework and the pygrass messenger

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@58653 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert пре 11 година
родитељ
комит
1079434857
3 измењених фајлова са 111 додато и 0 уклоњено
  1. 35 0
      lib/python/pygrass/messages/__init__.py
  2. 8 0
      lib/python/script/core.py
  3. 68 0
      lib/python/temporal/core.py

+ 35 - 0
lib/python/pygrass/messages/__init__.py

@@ -157,6 +157,22 @@ class Messenger(object):
          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
     """
@@ -272,6 +288,25 @@ class Messenger(object):
         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()
         """

+ 8 - 0
lib/python/script/core.py

@@ -588,6 +588,14 @@ def set_raise_on_error(raise_exp=True):
     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
 
 

+ 68 - 0
lib/python/temporal/core.py

@@ -262,6 +262,68 @@ def get_tgis_c_library_interface():
 
 ###############################################################################
 
+# 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 @@ def init():
     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 @@ def init():
     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