[GRASS-SVN] r62374 - grass/trunk/lib/python/pygrass/modules/interface

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 24 02:50:38 PDT 2014


Author: zarch
Date: 2014-10-24 02:50:38 -0700 (Fri, 24 Oct 2014)
New Revision: 62374

Modified:
   grass/trunk/lib/python/pygrass/modules/interface/module.py
Log:
pygrass: back to a pickable Module instance

Modified: grass/trunk/lib/python/pygrass/modules/interface/module.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/module.py	2014-10-24 07:30:52 UTC (rev 62373)
+++ grass/trunk/lib/python/pygrass/modules/interface/module.py	2014-10-24 09:50:38 UTC (rev 62374)
@@ -3,6 +3,7 @@
                         with_statement, print_function, unicode_literals)
 import sys
 from multiprocessing import cpu_count
+from functools import wraps
 
 if sys.version_info[0] == 2:
     from itertools import izip_longest as zip_longest
@@ -19,8 +20,44 @@
 from grass.pygrass.modules.interface.flag import Flag
 from grass.pygrass.modules.interface.typedict import TypeDict
 from grass.pygrass.modules.interface.read import GETFROMTAG, DOC
-from grass.pygrass.messages import Messenger
+from grass.pygrass.messages import get_msgr
 
+
+def mdebug(level, msg='', extra=None):
+    """Debug decorators for class methods.
+
+    :param level: the debug level
+    :type level: int
+    :param msg: Debug message
+    :type msg: str
+    :param extra: Function that return a string
+    :type msg: func
+    """
+    msgr = get_msgr()
+
+    def decorator(method):
+
+        @wraps(method)
+        def wrapper(self, *args, **kargs):
+            sargs = ', ' + ' , '.join([repr(a) for a in args]) if args else ''
+            skargs = (' , '.join(['%s=%r' % (k, v) for k, v in kargs.items()])
+                      if kargs else '')
+            opts = "%s%s%s" % (sargs, ',' if sargs and skargs else '', skargs)
+            dmsg = "%s.%s(self%s): %s %s" % (self.__class__.__name__,
+                                             method.__name__,
+                                             opts, msg,
+                                             extra(self, *args, **kargs)
+                                             if extra else '')
+            msgr.debug(level, dmsg)
+            return method(self, *args, **kargs)
+        return wrapper
+    return decorator
+
+
+def _get_bash(self, *args, **kargs):
+    return self.get_bash()
+
+
 class ParallelModuleQueue(object):
     """This class is designed to run an arbitrary number of pygrass Module
     processes in parallel.
@@ -337,8 +374,6 @@
     and keyword arguments to the grass module.
     """
     def __init__(self, cmd, *args, **kargs):
-        self._msgr = Messenger()
-        
         if isinstance(cmd, unicode):
             self.name = str(cmd)
         elif isinstance(cmd, str):
@@ -421,7 +456,7 @@
         if not args and not kargs:
             self.run()
             return self
-        
+
         #
         # check for extra kargs, set attribute and remove from dictionary
         #
@@ -458,11 +493,6 @@
                 raise ParameterError('%s is not a valid parameter.' % key)
 
         #
-        # print debug message
-        #
-        self._msgr.debug(1, "Module.__call__(): %s" % (self.get_bash()))
-
-        #
         # check if execute
         #
         if self.run_:
@@ -558,7 +588,8 @@
                 args.append(str(self.flags[flg]))
         return args
 
-    def run(self, node=None):
+    @mdebug(1, extra=_get_bash)
+    def run(self):
         """Run the module
 
         :param node:



More information about the grass-commit mailing list