1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Deprecation utilities."""
19
20 __docformat__ = "restructuredtext en"
21
22 import sys
23 from warnings import warn
24
26 """metaclass to print a warning on instantiation of a deprecated class"""
27
29 msg = getattr(cls, "__deprecation_warning__",
30 "%(cls)s is deprecated") % {'cls': cls.__name__}
31 warn(msg, DeprecationWarning, stacklevel=2)
32 return type.__call__(cls, *args, **kwargs)
33
34
36 """automatically creates a class which fires a DeprecationWarning
37 when instantiated.
38
39 >>> Set = class_renamed('Set', set, 'Set is now replaced by set')
40 >>> s = Set()
41 sample.py:57: DeprecationWarning: Set is now replaced by set
42 s = Set()
43 >>>
44 """
45 clsdict = {}
46 if message is None:
47 message = '%s is deprecated, use %s' % (old_name, new_class.__name__)
48 clsdict['__deprecation_warning__'] = message
49 try:
50
51 return class_deprecated(old_name, (new_class,), clsdict)
52 except (NameError, TypeError):
53
54 class DeprecatedClass(new_class):
55 """FIXME: There might be a better way to handle old/new-style class
56 """
57 def __init__(self, *args, **kwargs):
58 warn(message, DeprecationWarning, stacklevel=2)
59 new_class.__init__(self, *args, **kwargs)
60 return DeprecatedClass
61
62
63 -def class_moved(new_class, old_name=None, message=None):
64 """nice wrapper around class_renamed when a class has been moved into
65 another module
66 """
67 if old_name is None:
68 old_name = new_class.__name__
69 if message is None:
70 message = 'class %s is now available as %s.%s' % (
71 old_name, new_class.__module__, new_class.__name__)
72 return class_renamed(old_name, new_class, message)
73
75 """Decorator that raises a DeprecationWarning to print a message
76 when the decorated function is called.
77 """
78 def deprecated_decorator(func):
79 message = reason or 'The function "%s" is deprecated'
80 if '%s' in message:
81 message = message % func.func_name
82 def wrapped(*args, **kwargs):
83 warn(message, DeprecationWarning, stacklevel=stacklevel)
84 return func(*args, **kwargs)
85 try:
86 wrapped.__name__ = func.__name__
87 except TypeError:
88 pass
89 wrapped.__doc__ = func.__doc__
90 return wrapped
91 return deprecated_decorator
92
93 -def moved(modpath, objname):
94 """use to tell that a callable has been moved to a new module.
95
96 It returns a callable wrapper, so that when its called a warning is printed
97 telling where the object can be found, import is done (and not before) and
98 the actual object is called.
99
100 NOTE: the usage is somewhat limited on classes since it will fail if the
101 wrapper is use in a class ancestors list, use the `class_moved` function
102 instead (which has no lazy import feature though).
103 """
104 def callnew(*args, **kwargs):
105 from logilab.common.modutils import load_module_from_name
106 message = "object %s has been moved to module %s" % (objname, modpath)
107 warn(message, DeprecationWarning, stacklevel=2)
108 m = load_module_from_name(modpath)
109 return getattr(m, objname)(*args, **kwargs)
110 return callnew
111