1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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',
38
from warnings import warn
41
DEPRECATED_PARAMETER = "A deprecated parameter marker."
42
zero_seven = "%s was deprecated in version 0.7."
43
zero_eight = "%s was deprecated in version 0.8."
44
zero_nine = "%s was deprecated in version 0.9."
45
zero_ten = "%s was deprecated in version 0.10."
46
zero_eleven = "%s was deprecated in version 0.11."
47
zero_twelve = "%s was deprecated in version 0.12."
48
zero_thirteen = "%s was deprecated in version 0.13."
49
zero_fourteen = "%s was deprecated in version 0.14."
52
def set_warning_method(method):
53
"""Set the warning method to be used by this module.
55
It should take a message and a warning category as warnings.warn does.
61
# TODO - maybe this would be easier to use as one 'smart' method that
62
# guess if it is a method or a class or an attribute ? If so, we can
63
# add that on top of the primitives, once we have all three written
67
def deprecation_string(a_callable, deprecation_version):
68
"""Generate an automatic deprecation string for a_callable.
70
:param a_callable: The callable to substitute into deprecation_version.
71
:param deprecation_version: A deprecation format warning string. This should
72
have a single %s operator in it. a_callable will be turned into a nice
73
python symbol and then substituted into deprecation_version.
75
if getattr(a_callable, 'im_class', None) is None:
76
symbol = "%s.%s" % (a_callable.__module__,
79
symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
80
a_callable.im_class.__name__,
83
return deprecation_version % symbol
86
def deprecated_function(deprecation_version):
87
"""Decorate a function so that use of it will trigger a warning."""
89
def function_decorator(callable):
90
"""This is the function python calls to perform the decoration."""
92
def decorated_function(*args, **kwargs):
93
"""This is the decorated function."""
94
warn(deprecation_string(callable, deprecation_version),
95
DeprecationWarning, stacklevel=2)
96
return callable(*args, **kwargs)
97
_populate_decorated(callable, deprecation_version, "function",
99
return decorated_function
100
return function_decorator
103
def deprecated_method(deprecation_version):
104
"""Decorate a method so that use of it will trigger a warning.
106
To deprecate an entire class, decorate __init__.
109
def method_decorator(callable):
110
"""This is the function python calls to perform the decoration."""
112
def decorated_method(self, *args, **kwargs):
113
"""This is the decorated method."""
114
symbol = "%s.%s.%s" % (self.__class__.__module__,
115
self.__class__.__name__,
118
warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
119
return callable(self, *args, **kwargs)
120
_populate_decorated(callable, deprecation_version, "method",
122
return decorated_method
123
return method_decorator
126
def deprecated_passed(parameter_value):
127
"""Return True if parameter_value was used."""
128
# FIXME: it might be nice to have a parameter deprecation decorator.
129
# it would need to handle positional and *args and **kwargs parameters,
130
# which means some mechanism to describe how the parameter was being
131
# passed before deprecation, and some way to deprecate parameters that
132
# were not at the end of the arg list. Thats needed for __init__ where
133
# we cannot just forward to a new method name.I.e. in the following
134
# examples we would want to have callers that pass any value to 'bad' be
135
# given a warning - because we have applied:
136
# @deprecated_parameter('bad', zero_seven)
138
# def __init__(self, bad=None)
139
# def __init__(self, bad, other)
140
# def __init__(self, **kwargs)
142
return not parameter_value is DEPRECATED_PARAMETER
145
def _decorate_docstring(callable, deprecation_version, label,
148
docstring_lines = callable.__doc__.split('\n')
151
if len(docstring_lines) == 0:
152
decorated_callable.__doc__ = deprecation_version % ("This " + label)
153
elif len(docstring_lines) == 1:
154
decorated_callable.__doc__ = (callable.__doc__
157
+ deprecation_version % ("This " + label)
160
spaces = len(docstring_lines[-1])
161
new_doc = callable.__doc__
162
new_doc += "\n" + " " * spaces
163
new_doc += deprecation_version % ("This " + label)
164
new_doc += "\n" + " " * spaces
165
decorated_callable.__doc__ = new_doc
168
def _populate_decorated(callable, deprecation_version, label,
170
"""Populate attributes like __name__ and __doc__ on the decorated callable.
172
_decorate_docstring(callable, deprecation_version, label,
174
decorated_callable.__module__ = callable.__module__
175
decorated_callable.__name__ = callable.__name__
176
decorated_callable.is_deprecated = True
179
def _dict_deprecation_wrapper(func):
180
"""Returns a closure that emits a warning and calls the superclass"""
181
def cb(dep_dict, *args, **kwargs):
182
msg = 'access to %s' % (dep_dict._variable_name, )
183
msg = dep_dict._deprecation_version % (msg,)
185
msg += ' ' + dep_dict._advice
186
warn(msg, DeprecationWarning, stacklevel=2)
187
return func(dep_dict, *args, **kwargs)
191
class DeprecatedDict(dict):
192
"""A dictionary that complains when read or written."""
202
"""Create a dict that warns when read or modified.
204
:param deprecation_version: something like zero_nine
205
:param initial_value: The contents of the dict
206
:param variable_name: This allows better warnings to be printed
207
:param advice: String of advice on what callers should do instead
208
of using this variable.
210
self._deprecation_version = deprecation_version
211
self._variable_name = variable_name
212
self._advice = advice
213
dict.__init__(self, initial_value)
215
# This isn't every possible method but it should trap anyone using the
216
# dict -- add more if desired
217
__len__ = _dict_deprecation_wrapper(dict.__len__)
218
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
219
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
220
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
221
keys = _dict_deprecation_wrapper(dict.keys)
222
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
225
def deprecated_list(deprecation_version, variable_name,
226
initial_value, extra=None):
227
"""Create a list that warns when modified
229
:param deprecation_version: something like zero_nine
230
:param initial_value: The contents of the list
231
:param variable_name: This allows better warnings to be printed
232
:param extra: Extra info to print when printing a warning
235
subst_text = 'Modifying %s' % (variable_name,)
236
msg = deprecation_version % (subst_text,)
240
class _DeprecatedList(list):
241
__doc__ = list.__doc__ + msg
245
def _warn_deprecated(self, func, *args, **kwargs):
246
warn(msg, DeprecationWarning, stacklevel=3)
247
return func(self, *args, **kwargs)
249
def append(self, obj):
250
"""appending to %s is deprecated""" % (variable_name,)
251
return self._warn_deprecated(list.append, obj)
253
def insert(self, index, obj):
254
"""inserting to %s is deprecated""" % (variable_name,)
255
return self._warn_deprecated(list.insert, index, obj)
257
def extend(self, iterable):
258
"""extending %s is deprecated""" % (variable_name,)
259
return self._warn_deprecated(list.extend, iterable)
261
def remove(self, value):
262
"""removing from %s is deprecated""" % (variable_name,)
263
return self._warn_deprecated(list.remove, value)
265
def pop(self, index=None):
266
"""pop'ing from from %s is deprecated""" % (variable_name,)
268
return self._warn_deprecated(list.pop, index)
271
return self._warn_deprecated(list.pop)
273
return _DeprecatedList(initial_value)