Completed
Push — develop ( b00054...37b274 )
by Kale
8s
created

auxlib.Wrapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 11
Duplicated Lines 0 %
Metric Value
dl 0
loc 11
rs 10
wmc 6
1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, division, print_function
3
4
from inspect import isbuiltin
5
from logging import getLogger
6
import sys
7
import warnings
8
9
log = getLogger(__name__)
10
11
12
13
def deprecated(func):
14
    """This is a decorator which can be used to mark functions
15
    as deprecated. It will result in a warning being emmitted
16
    when the function is used."""
17
    if callable(func):
18
        def new_func(*args, **kwargs):
19
            warnings.simplefilter('always', DeprecationWarning)  # turn off filter
20
            warnings.warn("Call to deprecated {0}.".format(func.__name__),
21
                          category=DeprecationWarning,
22
                          stacklevel=2)
23
            warnings.simplefilter('default', DeprecationWarning)  # reset filter
24
            return func(*args, **kwargs)
25
        new_func.__name__ = func.__name__
26
        new_func.__doc__ = func.__doc__
27
        new_func.__dict__.update(func.__dict__)
28
    else:
29
        raise NotImplementedError()
30
    return new_func
31
32
33
def deprecated_import(module_name):
34
    warnings.simplefilter('always', ImportWarning)  # turn off filter
35
    warnings.warn("Import of deprecated module {0}.".format(module_name),
36
                  category=ImportWarning)
37
    warnings.simplefilter('default', ImportWarning)  # reset filter
38
39
40
def import_and_wrap_deprecated(module_name, module_dict, warn_import=True):
41
    """
42
    Usage:
43
        import_and_wrap_deprecated('conda.common.connection', locals())
44
        # looks for conda.common.connection.__all__
45
    """
46
    if warn_import:
47
        deprecated_import(module_name)
48
49
    from importlib import import_module
50
    module = import_module(module_name)
51
    for attr in module.__all__:
52
        module_dict[attr] = deprecated(getattr(module, attr))
53
54
55
def deprecate_module_with_proxy(module_name, module_dict, deprecated_attributes=None):
56
    """
57
    Usage:
58
        deprecate_module_with_proxy(__name__, locals())  # at bottom of module
59
    """
60
    def _ModuleProxy(module, depr):
61
        """Return a wrapped object that warns about deprecated accesses"""
62
        # http://stackoverflow.com/a/922693/2127762
63
        class Wrapper(object):
64
            def __getattr__(self, attr):
65
                if depr is None or attr in depr:
66
                    warnings.warn("Property %s is deprecated" % attr)
67
68
                return getattr(module, attr)
69
70
            def __setattr__(self, attr, value):
71
                if depr is None or attr in depr:
72
                    warnings.warn("Property %s is deprecated" % attr)
73
                return setattr(module, attr, value)
74
        return Wrapper()
75
76
    deprecated_import(module_name)
77
78
    deprs = set()
79
    for key in deprecated_attributes or module_dict:
80
        if key.startswith('_'):
81
            continue
82
        if callable(module_dict[key]) and not isbuiltin(module_dict[key]):
83
            module_dict[key] = deprecated(module_dict[key])
84
        else:
85
            deprs.add(key)
86
    sys.modules[module_name] = _ModuleProxy(sys.modules[module_name], deprs or None)
87