Repository navigation
Expand file tree
/
Copy path_sync.py
More file actions
90 lines (72 loc) · 2.62 KB
/
Copy path_sync.py
File metadata and controls
90 lines (72 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- test-case-name: effect.test_sync -*-
"""
Tools for dealing with Effects synchronously.
"""
import six
import sys
from ._base import perform
from ._utils import wraps
class NotSynchronousError(Exception):
"""Performing an effect did not immediately return a value."""
def sync_perform(dispatcher, effect):
"""
Perform an effect, and return its ultimate result. If the final result is
an error, the exception will be raised. This is useful for testing, and
also if you're using blocking effect implementations.
This requires that the effect (and all effects returned from any of its
callbacks) be synchronous. If the result is not available immediately,
:class:`NotSynchronousError` will be raised.
"""
successes = []
errors = []
effect = effect.on(success=successes.append, error=errors.append)
perform(dispatcher, effect)
if successes:
return successes[0]
elif errors:
six.reraise(*errors[0])
else:
raise NotSynchronousError("Performing %r was not synchronous!"
% (effect,))
def sync_performer(f):
"""
A decorator for performers that return a value synchronously.
The function being decorated is expected to take a dispatcher and an intent
(and not a box), and should return or raise normally. The wrapper function
that this decorator returns will accept a dispatcher, an intent, and a box
(conforming to the performer interface). The wrapper deals with putting the
return value or exception into the box.
Example::
@sync_performer
def perform_foo(dispatcher, foo):
return do_side_effect(foo)
"""
@wraps(f)
def sync_wrapper(*args):
box = args[-1]
pass_args = args[:-1]
try:
box.succeed(f(*pass_args))
except:
box.fail(sys.exc_info())
return sync_wrapper
def effect_performer(f):
"""
A decorator for performers that return effect
The function being decorated is expected to take a dispatcher and an intent
(and not a box), and should return (dispatcher, effect) tuple. The returned
effect will be performed using returned dispatcher.
Example::
@effect_performer
def perform_foo(dispatcher, foo):
new_disp = ComposedDispatcher(
[special_dispatcher(foo.fields), dispatcher])
return new_disp, foo.effect
"""
@wraps(f)
def eff_wrapper(*args):
box = args[-1]
pass_args = args[:-1]
new_disp, eff = f(*pass_args)
perform(new_disp, eff, eff.on(box.succeed, box.fail))
return eff_wrapper