scrapy / scrapy
1 7
import warnings
2 7
from functools import wraps
3

4 7
from twisted.internet import defer, threads
5

6 7
from scrapy.exceptions import ScrapyDeprecationWarning
7

8

9 7
def deprecated(use_instead=None):
10
    """This is a decorator which can be used to mark functions
11
    as deprecated. It will result in a warning being emitted
12
    when the function is used."""
13

14 7
    def deco(func):
15 7
        @wraps(func)
16 4
        def wrapped(*args, **kwargs):
17 7
            message = f"Call to deprecated function {func.__name__}."
18 7
            if use_instead:
19 7
                message += f" Use {use_instead} instead."
20 7
            warnings.warn(message, category=ScrapyDeprecationWarning, stacklevel=2)
21 7
            return func(*args, **kwargs)
22 7
        return wrapped
23

24 7
    if callable(use_instead):
25 7
        deco = deco(use_instead)
26 7
        use_instead = None
27 7
    return deco
28

29

30 7
def defers(func):
31
    """Decorator to make sure a function always returns a deferred"""
32 7
    @wraps(func)
33 4
    def wrapped(*a, **kw):
34 7
        return defer.maybeDeferred(func, *a, **kw)
35 7
    return wrapped
36

37

38 7
def inthread(func):
39
    """Decorator to call a function in a thread and return a deferred with the
40
    result
41
    """
42 0
    @wraps(func)
43 0
    def wrapped(*a, **kw):
44 0
        return threads.deferToThread(func, *a, **kw)
45 0
    return wrapped

Read our documentation on viewing source code .

Loading