HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/scrapy/utils/decorators.py
import warnings
from functools import wraps
from typing import Any, Callable

from twisted.internet import defer, threads
from twisted.internet.defer import Deferred

from scrapy.exceptions import ScrapyDeprecationWarning


def deprecated(use_instead: Any = None) -> Callable:
    """This is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emitted
    when the function is used."""

    def deco(func: Callable) -> Callable:
        @wraps(func)
        def wrapped(*args: Any, **kwargs: Any) -> Any:
            message = f"Call to deprecated function {func.__name__}."
            if use_instead:
                message += f" Use {use_instead} instead."
            warnings.warn(message, category=ScrapyDeprecationWarning, stacklevel=2)
            return func(*args, **kwargs)

        return wrapped

    if callable(use_instead):
        deco = deco(use_instead)
        use_instead = None
    return deco


def defers(func: Callable) -> Callable[..., Deferred]:
    """Decorator to make sure a function always returns a deferred"""

    @wraps(func)
    def wrapped(*a: Any, **kw: Any) -> Deferred:
        return defer.maybeDeferred(func, *a, **kw)

    return wrapped


def inthread(func: Callable) -> Callable[..., Deferred]:
    """Decorator to call a function in a thread and return a deferred with the
    result
    """

    @wraps(func)
    def wrapped(*a: Any, **kw: Any) -> Deferred:
        return threads.deferToThread(func, *a, **kw)

    return wrapped