Author: dmeyer
Date: Sat Jan 19 05:02:11 2008
New Revision: 2984

Log:
make YieldCallback an InProgress object

Modified:
   trunk/base/src/notifier/yieldfunc.py
   trunk/base/test/asynctest.py

Modified: trunk/base/src/notifier/yieldfunc.py
==============================================================================
--- trunk/base/src/notifier/yieldfunc.py        (original)
+++ trunk/base/src/notifier/yieldfunc.py        Sat Jan 19 05:02:11 2008
@@ -79,16 +79,18 @@
 # by yield_execution and the deferrer only handles one connect!
 
 
-class YieldCallback(object):
+class YieldCallback(InProgress):
     """
     Callback class that can be used as a callback for a function that is
     async. Return this object using 'yield' and use the member function
     'get' later to get the result.
     """
     def __init__(self, func=None):
+        InProgress.__init__(self)
         if func is not None:
             if isinstance(func, Signal):
                 func = func.connect_once
+            # connect self as callback
             func(self)
 
 
@@ -97,50 +99,35 @@
         Call the YieldCallback by the external function. This will resume
         the calling YieldFunction.
         """
-        self._args = args
-        self._kwargs = kwargs
-        self._callback()
-        self._callback = None
-        return False
-
-
-    def _connect(self, callback):
-        """
-        Connect a callback that will be called when the YieldCallback
-        function is called. This callback will resume the calling
-        YieldFunction.
-        """
-        self._callback = callback
-
-
-    def get(self):
-        """
-        Return the result of the callback.
-        """
         # try to get the results as the caller excepts them
-        if self._args and self._kwargs:
+        if args and kwargs:
             # no idea how to merge them
-            return self._args, self._kwargs
-        if self._kwargs and len(self._kwargs) == 1:
+            return self.finished((args, kwargs))
+        if kwargs and len(kwargs) == 1:
             # return the value
-            return self._kwargs.values()[0]
-        if self._kwargs:
+            return self.finished(kwargs.values()[0])
+        if kwargs:
             # return as dict
-            return self._kwargs
-        if len(self._args) == 1:
+            return self.finished(kwargs)
+        if len(args) == 1:
             # return value
-            return self._args[0]
-        if len(self._args) > 1:
+            return self.finished(args[0])
+        if len(args) > 1:
             # return as list
-            return self._args
-        return None
+            return self.finished(args)
+        return self.finished(None)
 
 
+    def get(self):
+        log.warning('Deprecated call to YieldCallback.get(); use get_result() 
instead')
+        return InProgress.get_result(self)
+        
+
 def yield_execution(interval=0, lock=False):
     """
     Functions with this decorator uses yield to break and to return the
     results. Special yield values for break are YieldContinue or
-    YieldCallback or InProgress objects. In lock is True the function will
+    InProgress objects. In lock is True the function will
     be locked against parallel calls. If locked the call will delayed.
     A function decorated with this decorator will always return a
     YieldFunction (which is an InProgress object) or the result.
@@ -174,8 +161,7 @@
                 # XXX result After that, return that InProgress object
                 # XXX to always return an InProgress object.
                 return None
-            if not (result == YieldContinue or \
-                    isinstance(result, (YieldCallback, InProgress))):
+            if not (result == YieldContinue or isinstance(result, InProgress)):
                 # everything went fine, return result
                 # XXX YIELD CHANGES NOTES
                 # XXX Create InProgress object here and emit delayed
@@ -183,7 +169,7 @@
                 # XXX to always return an InProgress object.
                 return result
             # we need a step callback to finish this later
-            # result is one of YieldContinue, YieldCallback, InProgress
+            # result is one of YieldContinue, InProgress
             progress = YieldFunction(function, interval, result)
             if lock:
                 func._lock = progress
@@ -205,8 +191,8 @@
     """
     InProgress class that runs a generator function. This is also the return 
value
     for yield_execution if it takes some more time. status can be either None
-    (not started yet), YieldContinue (iterate now), YieldCallback (wait until 
the
-    callback is called) or InProgress (wait until InProgress is done).
+    (not started yet), YieldContinue (iterate now) or InProgress (wait until
+    InProgress is done).
     """
     def __init__(self, function, interval, status=None):
         InProgress.__init__(self)
@@ -228,12 +214,6 @@
             # XXX Be careful with already finished InProgress
             # XXX Remember status for Python 2.5 to send back
             status.connect_both(self._continue, self._continue)
-        elif isinstance(status, YieldCallback):
-            # Set _continue as callback to resume the generator when status is 
done.
-            # XXX YIELD CHANGES NOTES
-            # XXX Be careful with already finished callbacks
-            # XXX Remember status for Python 2.5 to send back
-            status._connect(self._continue)
         else:
             raise RuntimeError('YieldFunction with bad status %s' % status)
 
@@ -250,7 +230,7 @@
             self._continue()
             return True
         # return the result
-        # DEPRECATED!!!!!!!!!!!!!!!!!
+        log.warning('Deprecated call to InProgress(); use get_result() 
instead')
         return InProgress.get_result(self)
 
 
@@ -288,7 +268,7 @@
             # schedule next interation with the timer
             return True
         # We have to stop the timer because we either have a result
-        # or have to wait for an InProgress or YieldCallback
+        # or have to wait for an InProgress
         self._timer.stop()
         if isinstance(result, InProgress):
             # continue when InProgress is done
@@ -297,13 +277,6 @@
             # XXX Be careful with already finished InProgress
             result.connect_both(self._continue, self._continue)
             return False
-        if isinstance(result, YieldCallback):
-            # Set _continue as callback to resume the generator when result is 
done.
-            # XXX YIELD CHANGES NOTES
-            # XXX Remember result for Python 2.5 to send back
-            # XXX Be careful with already finished callbacks
-            result._connect(self._continue)
-            return False
         # YieldFunction is done
         self._timer = None
         self.finished(result)

Modified: trunk/base/test/asynctest.py
==============================================================================
--- trunk/base/test/asynctest.py        (original)
+++ trunk/base/test/asynctest.py        Sat Jan 19 05:02:11 2008
@@ -106,27 +106,27 @@
     callback = kaa.YieldCallback()
     async(callback, 7, 8)
     yield callback
-    print callback.get()                # (7, 8)
+    print callback.get_result()                # (7, 8)
 
     callback = kaa.YieldCallback()
     async(callback)
     yield callback
-    print callback.get()                # None
+    print callback.get_result()                # None
 
     callback = kaa.YieldCallback()
     async(callback, 9)
     yield callback
-    print callback.get()                # 9
+    print callback.get_result()                # 9
 
     callback = kaa.YieldCallback()
     async(callback, foo=10)
     yield callback
-    print callback.get()                # 10
+    print callback.get_result()                # 10
 
     callback = kaa.YieldCallback()
     async(callback, foo=11, bar=12)
     yield callback
-    print callback.get()                # {'foo': 11, 'bar': 12}
+    print callback.get_result()                # {'foo': 11, 'bar': 12}
 
     x = thread(13)
     # this is also an InProgress object

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to