[GRASS-SVN] r58799 - grass/trunk/lib/python/pygrass/messages

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jan 29 08:50:24 PST 2014


Author: zarch
Date: 2014-01-29 08:50:23 -0800 (Wed, 29 Jan 2014)
New Revision: 58799

Modified:
   grass/trunk/lib/python/pygrass/messages/__init__.py
Log:
Add a function that return an instance of the Messenger class and remove trailing spaces

Modified: grass/trunk/lib/python/pygrass/messages/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/messages/__init__.py	2014-01-29 16:19:59 UTC (rev 58798)
+++ grass/trunk/lib/python/pygrass/messages/__init__.py	2014-01-29 16:50:23 UTC (rev 58799)
@@ -13,12 +13,11 @@
 
 @author Soeren Gebbert
 """
-
-import logging
 import sys
 import grass.lib.gis as libgis
 from multiprocessing import Process, Lock, Pipe
 
+
 class FatalError(Exception):
     """!This error will be raised in case raise_on_error was set True
        when creating the messenger object.
@@ -29,6 +28,7 @@
     def __str__(self):
         return self.value
 
+
 def message_server(lock, conn):
     """!The GRASS message server function designed to be a target for
        multiprocessing.Process
@@ -64,7 +64,7 @@
        - Percent: ["PERCENT", n, d, s]
     """
     libgis.G_debug(1, "Start messenger server")
-    
+
     while True:
         # Avoid busy waiting
         conn.poll(None)
@@ -108,6 +108,7 @@
 
         lock.release()
 
+
 class Messenger(object):
     """!Fast and exit-safe interface to GRASS C-library message functions
 
@@ -157,7 +158,7 @@
          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!")
@@ -190,7 +191,7 @@
         self.client_conn, self.server_conn = Pipe()
         self.lock = Lock()
         self.server = Process(target=message_server, args=(self.lock,
-                                                          self.server_conn))
+                                                           self.server_conn))
         self.server.daemon = True
         self.server.start()
 
@@ -246,11 +247,11 @@
         self.client_conn.send(["ERROR", message])
 
     def fatal(self, message):
-        """!Send an error message to stderr, call sys.exit(1) or raise FatalError 
+        """!Send an error message to stderr, call sys.exit(1) or raise FatalError
 
            This function emulates the behavior of G_fatal_error(). It prints
            an error message to stderr and calls sys.exit(1). If raise_on_error
-           is set True while creating the messenger object, a FatalError 
+           is set True while creating the messenger object, a FatalError
            exception will be raised instead of calling sys.exit(1).
         """
         self._check_restart_server()
@@ -282,7 +283,7 @@
         """!Stop the messenger server and close the pipe
         """
         if self.server is not None and self.server.is_alive():
-            self.client_conn.send(["STOP",])
+            self.client_conn.send(["STOP", ])
             self.server.join(5)
             self.server.terminate()
         if self.client_conn is not None:
@@ -290,23 +291,23 @@
 
     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 
+
+           @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 
+
+           @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()
         """
@@ -315,6 +316,26 @@
         self.client_conn.send(["FATAL", message])
         time.sleep(1)
 
+
+def get_msgr(_instance=[None, ]):
+    """!Return a Messenger instance. ::
+
+    @return the Messenger instance.
+
+        @code
+        >>> msgr0 = get_msgr()
+        >>> msgr1 = get_msgr()
+        >>> msgr2 = Messenger()
+        >>> msgr0 is msgr1
+        True
+        >>> msgr0 is msgr2
+        False
+    """
+    if not _instance[0]:
+        _instance[0] = Messenger()
+    return _instance[0]
+
+
 if __name__ == "__main__":
     import doctest
     doctest.testmod()



More information about the grass-commit mailing list