aboutsummaryrefslogtreecommitdiff
blob: c0e0429dc056b2881867c4f9523e3909ee6e6379 (plain)
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
"""Various decorator utilities."""

from functools import wraps

from .contexts import Namespace, SplitExec


def splitexec(func):
    """Run the decorated function in another process."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        with SplitExec():
            return func(*args, **kwargs)

    return wrapper


def namespace(**namespaces):
    """Run the decorated function in a specified namespace."""

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            with Namespace(**namespaces):
                return func(*args, **kwargs)

        return wrapper

    return decorator


def coroutine(func):
    """Prime a coroutine for input."""

    @wraps(func)
    def prime(*args, **kwargs):
        cr = func(*args, **kwargs)
        next(cr)
        return cr

    return prime