1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com> and others
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
The methods here allow for api symbol versioning.
23
__all__ = ['deprecated_function',
26
'DEPRECATED_PARAMETER',
28
'warn', 'set_warning_method', 'zero_seven',
50
from warnings import warn
53
DEPRECATED_PARAMETER = "A deprecated parameter marker."
54
zero_seven = "%s was deprecated in version 0.7."
55
zero_eight = "%s was deprecated in version 0.8."
56
zero_nine = "%s was deprecated in version 0.9."
57
zero_ten = "%s was deprecated in version 0.10."
58
zero_eleven = "%s was deprecated in version 0.11."
59
zero_twelve = "%s was deprecated in version 0.12."
60
zero_thirteen = "%s was deprecated in version 0.13."
61
zero_fourteen = "%s was deprecated in version 0.14."
62
zero_fifteen = "%s was deprecated in version 0.15."
63
zero_sixteen = "%s was deprecated in version 0.16."
64
zero_seventeen = "%s was deprecated in version 0.17."
65
zero_eighteen = "%s was deprecated in version 0.18."
66
zero_ninety = "%s was deprecated in version 0.90."
67
zero_ninetyone = "%s was deprecated in version 0.91."
68
zero_ninetytwo = "%s was deprecated in version 0.92."
69
one_zero = "%s was deprecated in version 1.0."
70
zero_ninetythree = one_zero # Maintained for backwards compatibility
71
one_one = "%s was deprecated in version 1.1."
72
one_two = "%s was deprecated in version 1.2."
73
one_three = "%s was deprecated in version 1.3."
75
def set_warning_method(method):
76
"""Set the warning method to be used by this module.
78
It should take a message and a warning category as warnings.warn does.
84
# TODO - maybe this would be easier to use as one 'smart' method that
85
# guess if it is a method or a class or an attribute ? If so, we can
86
# add that on top of the primitives, once we have all three written
90
def deprecation_string(a_callable, deprecation_version):
91
"""Generate an automatic deprecation string for a_callable.
93
:param a_callable: The callable to substitute into deprecation_version.
94
:param deprecation_version: A deprecation format warning string. This should
95
have a single %s operator in it. a_callable will be turned into a nice
96
python symbol and then substituted into deprecation_version.
98
# We also want to handle old-style classes, in particular exception, and
99
# they don't have an im_class attribute.
100
if getattr(a_callable, 'im_class', None) is None:
101
symbol = "%s.%s" % (a_callable.__module__,
104
symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
105
a_callable.im_class.__name__,
108
return deprecation_version % symbol
111
def deprecated_function(deprecation_version):
112
"""Decorate a function so that use of it will trigger a warning."""
114
def function_decorator(callable):
115
"""This is the function python calls to perform the decoration."""
117
def decorated_function(*args, **kwargs):
118
"""This is the decorated function."""
119
warn(deprecation_string(callable, deprecation_version),
120
DeprecationWarning, stacklevel=2)
121
return callable(*args, **kwargs)
122
_populate_decorated(callable, deprecation_version, "function",
124
return decorated_function
125
return function_decorator
128
def deprecated_method(deprecation_version):
129
"""Decorate a method so that use of it will trigger a warning.
131
To deprecate a static or class method, use
137
To deprecate an entire class, decorate __init__.
140
def method_decorator(callable):
141
"""This is the function python calls to perform the decoration."""
143
def decorated_method(self, *args, **kwargs):
144
"""This is the decorated method."""
145
if callable.__name__ == '__init__':
146
symbol = "%s.%s" % (self.__class__.__module__,
147
self.__class__.__name__,
150
symbol = "%s.%s.%s" % (self.__class__.__module__,
151
self.__class__.__name__,
154
warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
155
return callable(self, *args, **kwargs)
156
_populate_decorated(callable, deprecation_version, "method",
158
return decorated_method
159
return method_decorator
162
def deprecated_passed(parameter_value):
163
"""Return True if parameter_value was used."""
164
# FIXME: it might be nice to have a parameter deprecation decorator.
165
# it would need to handle positional and *args and **kwargs parameters,
166
# which means some mechanism to describe how the parameter was being
167
# passed before deprecation, and some way to deprecate parameters that
168
# were not at the end of the arg list. Thats needed for __init__ where
169
# we cannot just forward to a new method name.I.e. in the following
170
# examples we would want to have callers that pass any value to 'bad' be
171
# given a warning - because we have applied:
172
# @deprecated_parameter('bad', zero_seven)
174
# def __init__(self, bad=None)
175
# def __init__(self, bad, other)
176
# def __init__(self, **kwargs)
178
return not parameter_value is DEPRECATED_PARAMETER
181
def _decorate_docstring(callable, deprecation_version, label,
184
docstring_lines = callable.__doc__.split('\n')
187
if len(docstring_lines) == 0:
188
decorated_callable.__doc__ = deprecation_version % ("This " + label)
189
elif len(docstring_lines) == 1:
190
decorated_callable.__doc__ = (callable.__doc__
193
+ deprecation_version % ("This " + label)
196
spaces = len(docstring_lines[-1])
197
new_doc = callable.__doc__
198
new_doc += "\n" + " " * spaces
199
new_doc += deprecation_version % ("This " + label)
200
new_doc += "\n" + " " * spaces
201
decorated_callable.__doc__ = new_doc
204
def _populate_decorated(callable, deprecation_version, label,
206
"""Populate attributes like __name__ and __doc__ on the decorated callable.
208
_decorate_docstring(callable, deprecation_version, label,
210
decorated_callable.__module__ = callable.__module__
211
decorated_callable.__name__ = callable.__name__
212
decorated_callable.is_deprecated = True
215
def _dict_deprecation_wrapper(wrapped_method):
216
"""Returns a closure that emits a warning and calls the superclass"""
217
def cb(dep_dict, *args, **kwargs):
218
msg = 'access to %s' % (dep_dict._variable_name, )
219
msg = dep_dict._deprecation_version % (msg,)
221
msg += ' ' + dep_dict._advice
222
warn(msg, DeprecationWarning, stacklevel=2)
223
return wrapped_method(dep_dict, *args, **kwargs)
227
class DeprecatedDict(dict):
228
"""A dictionary that complains when read or written."""
238
"""Create a dict that warns when read or modified.
240
:param deprecation_version: something like zero_nine
241
:param initial_value: The contents of the dict
242
:param variable_name: This allows better warnings to be printed
243
:param advice: String of advice on what callers should do instead
244
of using this variable.
246
self._deprecation_version = deprecation_version
247
self._variable_name = variable_name
248
self._advice = advice
249
dict.__init__(self, initial_value)
251
# This isn't every possible method but it should trap anyone using the
252
# dict -- add more if desired
253
__len__ = _dict_deprecation_wrapper(dict.__len__)
254
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
255
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
256
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
257
keys = _dict_deprecation_wrapper(dict.keys)
258
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
261
def deprecated_list(deprecation_version, variable_name,
262
initial_value, extra=None):
263
"""Create a list that warns when modified
265
:param deprecation_version: something like zero_nine
266
:param initial_value: The contents of the list
267
:param variable_name: This allows better warnings to be printed
268
:param extra: Extra info to print when printing a warning
271
subst_text = 'Modifying %s' % (variable_name,)
272
msg = deprecation_version % (subst_text,)
276
class _DeprecatedList(list):
277
__doc__ = list.__doc__ + msg
281
def _warn_deprecated(self, func, *args, **kwargs):
282
warn(msg, DeprecationWarning, stacklevel=3)
283
return func(self, *args, **kwargs)
285
def append(self, obj):
286
"""appending to %s is deprecated""" % (variable_name,)
287
return self._warn_deprecated(list.append, obj)
289
def insert(self, index, obj):
290
"""inserting to %s is deprecated""" % (variable_name,)
291
return self._warn_deprecated(list.insert, index, obj)
293
def extend(self, iterable):
294
"""extending %s is deprecated""" % (variable_name,)
295
return self._warn_deprecated(list.extend, iterable)
297
def remove(self, value):
298
"""removing from %s is deprecated""" % (variable_name,)
299
return self._warn_deprecated(list.remove, value)
301
def pop(self, index=None):
302
"""pop'ing from from %s is deprecated""" % (variable_name,)
304
return self._warn_deprecated(list.pop, index)
307
return self._warn_deprecated(list.pop)
309
return _DeprecatedList(initial_value)