|
| 1 | +# -*- test-case-name: effect.test_twisted -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Twisted integration for the Effect library. |
| 5 | +
|
| 6 | +This is largely concerned with bridging the gap between Effects and Deferreds, |
| 7 | +and also implementing Twisted-specific performers for standard Intents. |
| 8 | +
|
| 9 | +The most important functions here are :func:`perform`, |
| 10 | +:func:`make_twisted_dispatcher`, and :func:`deferred_performer`. |
| 11 | +
|
| 12 | +Note that the core effect library does *not* depend on Twisted, but this module |
| 13 | +does. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | + |
| 18 | +from functools import partial |
| 19 | +import sys |
| 20 | + |
| 21 | +from twisted.internet.defer import Deferred |
| 22 | +from twisted.python.failure import Failure |
| 23 | +from twisted.internet.task import deferLater |
| 24 | + |
| 25 | +from ._intents import Delay, ParallelEffects |
| 26 | +from ._base import perform as base_perform |
| 27 | +from ._dispatcher import TypeDispatcher |
| 28 | +from ._utils import wraps |
| 29 | +from .async import perform_parallel_async |
| 30 | + |
| 31 | + |
| 32 | +def deferred_to_box(d, box): |
| 33 | + """ |
| 34 | + Make a Deferred pass its success or fail events on to the given box. |
| 35 | + """ |
| 36 | + d.addCallbacks(box.succeed, lambda f: box.fail((f.type, f.value, f.tb))) |
| 37 | + |
| 38 | + |
| 39 | +def make_twisted_dispatcher(reactor): |
| 40 | + """ |
| 41 | + Create a dispatcher that knows how to perform certain built-in Intents |
| 42 | + with Twisted-specific implementations. |
| 43 | + """ |
| 44 | + return TypeDispatcher({ |
| 45 | + ParallelEffects: perform_parallel_async, |
| 46 | + Delay: deferred_performer(partial(perform_delay, reactor)), |
| 47 | + }) |
| 48 | + |
| 49 | + |
| 50 | +def deferred_performer(f): |
| 51 | + """ |
| 52 | + A decorator for performers that return Deferreds. |
| 53 | +
|
| 54 | + The function being decorated is expected to take a dispatcher and an intent |
| 55 | + (and not a box), and may return a Deferred. The wrapper function |
| 56 | + that this decorator returns will accept a dispatcher, an intent, and a box |
| 57 | + (conforming to the performer interface). The wrapper deals with |
| 58 | + putting the Deferred's result into the box. |
| 59 | +
|
| 60 | + Example:: |
| 61 | +
|
| 62 | + @deferred_performer |
| 63 | + def perform_foo(dispatcher, foo): |
| 64 | + return do_side_effecting_deferred_operation(foo) |
| 65 | + """ |
| 66 | + @wraps(f) |
| 67 | + def deferred_wrapper(*args): |
| 68 | + box = args[-1] |
| 69 | + pass_args = args[:-1] |
| 70 | + try: |
| 71 | + result = f(*pass_args) |
| 72 | + except: |
| 73 | + box.fail(sys.exc_info()) |
| 74 | + else: |
| 75 | + if isinstance(result, Deferred): |
| 76 | + deferred_to_box(result, box) |
| 77 | + else: |
| 78 | + box.succeed(result) |
| 79 | + return deferred_wrapper |
| 80 | + |
| 81 | + |
| 82 | +def perform_delay(reactor, dispatcher, delay): |
| 83 | + """ |
| 84 | + Perform a :obj:`effect.Delay` with Twisted's |
| 85 | + :func:`twisted.internet.task.deferLater`. |
| 86 | + """ |
| 87 | + return deferLater(reactor, delay.delay, lambda: None) |
| 88 | + |
| 89 | + |
| 90 | +def perform(dispatcher, effect): |
| 91 | + """ |
| 92 | + Perform an effect, returning a Deferred that will fire with the effect's |
| 93 | + ultimate result. |
| 94 | + """ |
| 95 | + d = Deferred() |
| 96 | + eff = effect.on( |
| 97 | + success=d.callback, |
| 98 | + error=lambda e: d.errback(exc_info_to_failure(e))) |
| 99 | + base_perform(dispatcher, eff) |
| 100 | + return d |
| 101 | + |
| 102 | + |
| 103 | +def exc_info_to_failure(exc_info): |
| 104 | + """Convert an exc_info tuple to a :class:`Failure`.""" |
| 105 | + return Failure(exc_info[1], exc_info[0], exc_info[2]) |
0 commit comments