1
# Copyright (C) 2006 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com> and others
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
43
46
zero_ten = "%s was deprecated in version 0.10."
44
47
zero_eleven = "%s was deprecated in version 0.11."
45
48
zero_twelve = "%s was deprecated in version 0.12."
49
zero_thirteen = "%s was deprecated in version 0.13."
50
zero_fourteen = "%s was deprecated in version 0.14."
51
zero_fifteen = "%s was deprecated in version 0.15."
48
54
def set_warning_method(method):
172
178
decorated_callable.is_deprecated = True
181
def _dict_deprecation_wrapper(wrapped_method):
182
"""Returns a closure that emits a warning and calls the superclass"""
183
def cb(dep_dict, *args, **kwargs):
184
msg = 'access to %s' % (dep_dict._variable_name, )
185
msg = dep_dict._deprecation_version % (msg,)
187
msg += ' ' + dep_dict._advice
188
warn(msg, DeprecationWarning, stacklevel=2)
189
return wrapped_method(dep_dict, *args, **kwargs)
193
class DeprecatedDict(dict):
194
"""A dictionary that complains when read or written."""
204
"""Create a dict that warns when read or modified.
206
:param deprecation_version: something like zero_nine
207
:param initial_value: The contents of the dict
208
:param variable_name: This allows better warnings to be printed
209
:param advice: String of advice on what callers should do instead
210
of using this variable.
212
self._deprecation_version = deprecation_version
213
self._variable_name = variable_name
214
self._advice = advice
215
dict.__init__(self, initial_value)
217
# This isn't every possible method but it should trap anyone using the
218
# dict -- add more if desired
219
__len__ = _dict_deprecation_wrapper(dict.__len__)
220
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
221
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
222
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
223
keys = _dict_deprecation_wrapper(dict.keys)
224
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
175
227
def deprecated_list(deprecation_version, variable_name,
176
228
initial_value, extra=None):
177
229
"""Create a list that warns when modified