File: utils.py

package info (click to toggle)
python-promise 2.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: python: 2,681; sh: 13; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (2)
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
import functools
import inspect
import types
import warnings
import sys


def warn(msg):
    # type: (str) -> None
    warnings.simplefilter("always", DeprecationWarning)  # turn off filter
    warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
    warnings.simplefilter("default", DeprecationWarning)  # reset filter


class deprecated(object):
    def __init__(self, reason, name=None):
        if inspect.isclass(reason) or inspect.isfunction(reason):
            raise TypeError("Reason for deprecation must be supplied")
        self.reason = reason
        self.name = name

    def __call__(self, cls_or_func):
        if inspect.isfunction(cls_or_func):
            fmt = "Call to deprecated function or method {name} ({reason})."

        elif inspect.isclass(cls_or_func):
            fmt = "Call to deprecated class {name} ({reason})."

        else:
            raise TypeError(type(cls_or_func))

        msg = fmt.format(name=self.name or cls_or_func.__name__, reason=self.reason)

        @functools.wraps(cls_or_func)
        def new_func(*args, **kwargs):
            warn(msg)
            return cls_or_func(*args, **kwargs)

        return new_func


PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

if PY3:
    string_types = (str,)  # type: tuple
    integer_types = (int,)  # type: tuple
    class_types = (type,)  # type: tuple
    text_type = str
    binary_type = bytes
else:
    string_types = (basestring,)  # type: tuple
    integer_types = (int, long)  # type: tuple
    class_types = (type, types.ClassType)  # type: tuple
    text_type = unicode
    binary_type = str