1
# Copyright (C) 2006 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
47
48
zero_twelve = "%s was deprecated in version 0.12."
48
49
zero_thirteen = "%s was deprecated in version 0.13."
49
50
zero_fourteen = "%s was deprecated in version 0.14."
51
zero_fifteen = "%s was deprecated in version 0.15."
52
54
def set_warning_method(method):
178
180
decorated_callable.is_deprecated = True
183
def _dict_deprecation_wrapper(wrapped_method):
184
"""Returns a closure that emits a warning and calls the superclass"""
185
def cb(dep_dict, *args, **kwargs):
186
msg = 'access to %s' % (dep_dict._variable_name, )
187
msg = dep_dict._deprecation_version % (msg,)
189
msg += ' ' + dep_dict._advice
190
warn(msg, DeprecationWarning, stacklevel=2)
191
return wrapped_method(dep_dict, *args, **kwargs)
195
class DeprecatedDict(dict):
196
"""A dictionary that complains when read or written."""
206
"""Create a dict that warns when read or modified.
208
:param deprecation_version: something like zero_nine
209
:param initial_value: The contents of the dict
210
:param variable_name: This allows better warnings to be printed
211
:param advice: String of advice on what callers should do instead
212
of using this variable.
214
self._deprecation_version = deprecation_version
215
self._variable_name = variable_name
216
self._advice = advice
217
dict.__init__(self, initial_value)
219
# This isn't every possible method but it should trap anyone using the
220
# dict -- add more if desired
221
__len__ = _dict_deprecation_wrapper(dict.__len__)
222
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
223
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
224
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
225
keys = _dict_deprecation_wrapper(dict.keys)
226
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
181
229
def deprecated_list(deprecation_version, variable_name,
182
230
initial_value, extra=None):
183
231
"""Create a list that warns when modified