test/data/vclamp_jpk/README: Document sample versions
[hooke.git] / hooke / util / callback.py
index 3a8f92789f59ea0bfdd2c1bb15f56efb21abcec9..d4c870ce28a7685c369ec8beb4b8bf49aa7554e7 100644 (file)
@@ -1,26 +1,27 @@
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
 #
 # This file is part of Hooke.
 #
-# Hooke is free software: you can redistribute it and/or modify it
-# under the terms of the GNU Lesser General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
+# Hooke is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
 #
-# Hooke is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
-# Public License for more details.
+# Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
 #
-# You should have received a copy of the GNU Lesser General Public
-# License along with Hooke.  If not, see
-# <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU Lesser General Public License
+# along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
 
 """Define the `@callback` decorator.
 
 See :pep:`318` for an introduction to decorators.
 """
 
+import logging
+
 from .caller import caller_name
 
 
@@ -63,11 +64,11 @@ def callback(method):
     returned arguments of the method they're attached to.
 
     >>> def c(self, method, *args):
-    ...     print '\\n  '.join([
+    ...     print('\\n  '.join([
     ...             'callback:',
     ...             'class:    %s' % self,
     ...             'method:   %s' % method,
-    ...             'returned: %s' % args])
+    ...             'returned: %s' % args]))
 
     For some class, decorate any functions you're interested in
     attaching callbacks too.  Also, add a `_callbacks` attribute
@@ -80,13 +81,13 @@ def callback(method):
     ...     @callback
     ...     def xyz(self):
     ...         "xyz's docstring"
-    ...         print 'usual xyz business'
+    ...         print('usual xyz business')
     ...         return (0, 1, 1, 2, 3, 5)
     ...
     ...     @callback
     ...     def abc(self):
     ...         "abc's docstring"
-    ...         print 'usual abc business'
+    ...         print('usual abc business')
     ...
     >>> x = X()
 
@@ -103,7 +104,7 @@ def callback(method):
 
     The decorated method preserves the original docstring.
 
-    >>> print x.xyz.__doc__
+    >>> print(x.xyz.__doc__)
     xyz's docstring
 
     So far, we haven't attached a callback to `abc`.
@@ -125,7 +126,7 @@ def callback(method):
     array of callbacks in series.
 
     >>> def d(self, method, *args):
-    ...     print 'callback d'
+    ...     print('callback d')
     >>> x._callbacks['abc'] = [d, c, d]
     >>> r = x.abc()  # doctest: +ELLIPSIS
     usual abc business
@@ -139,12 +140,14 @@ def callback(method):
     def new_m(self, *args, **kwargs):
         result = method(self, *args, **kwargs)
         callback = self._callbacks.get(method.func_name, None)
-        nm = getattr(self, method.func_name)
+        mn = getattr(self, method.func_name)
+        log = logging.getLogger('hooke')
+        log.debug('callback: %s (%s) calling %s' % (method.func_name, mn, callback))
         if is_iterable(callback):
             for cb in callback:
-                cb(self, nm, result)
+                cb(self, mn, result)
         elif callback != None:
-            callback(self, nm, result)
+            callback(self, mn, result)
         return result
     new_m.func_name = method.func_name
     new_m.func_doc = method.func_doc
@@ -166,12 +169,12 @@ def in_callback(self, *args, **kwargs):
     returned arguments of the method they're attached to.
 
     >>> def c(self, method, *args, **kwargs):
-    ...     print '\\n  '.join([
+    ...     print('\\n  '.join([
     ...             'callback:',
     ...             'class:    %s' % self,
     ...             'method:   %s' % method,
     ...             'args:     %s' % (args,),
-    ...             'kwargs:   %s' % kwargs])
+    ...             'kwargs:   %s' % kwargs]))
 
     Now place `in_callback` calls inside any interesting methods.
 
@@ -181,14 +184,14 @@ def in_callback(self, *args, **kwargs):
     ...
     ...     def xyz(self):
     ...         "xyz's docstring"
-    ...         print 'usual xyz business'
+    ...         print('usual xyz business')
     ...         in_callback(self, 5, my_kw=17)
     ...         return (0, 1, 1, 2, 3, 5)
     ...
     ...     def abc(self):
     ...         "abc's docstring"
     ...         in_callback(self, p1=3.14, p2=159)
-    ...         print 'usual abc business'
+    ...         print('usual abc business')
     ...
     >>> x = X()
 
@@ -224,7 +227,7 @@ def in_callback(self, *args, **kwargs):
     array of callbacks in series.
 
     >>> def d(self, method, *args, **kwargs):
-    ...     print 'callback d'
+    ...     print('callback d')
     >>> x._callbacks['abc'] = [d, c, d]
     >>> r = x.abc()  # doctest: +ELLIPSIS
     callback d
@@ -238,9 +241,11 @@ def in_callback(self, *args, **kwargs):
     """
     method_name = caller_name(depth=2)
     callback = self._callbacks.get(method_name, None)
-    nm = getattr(self, method_name)
+    mn = getattr(self, method_name)
+    log = logging.getLogger('hooke')
+    log.debug('callback: %s (%s) calling %s' % (method_name, mn, callback))
     if is_iterable(callback):
         for cb in callback:
-            cb(self, nm, *args, **kwargs)
+            cb(self, mn, *args, **kwargs)
     elif callback != None:
-        callback(self, nm, *args, **kwargs)
+        callback(self, mn, *args, **kwargs)