~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
3
3
#
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
33
33
           'zero_twelve',
34
34
           'zero_thirteen',
35
35
           'zero_fourteen',
 
36
           'zero_fifteen',
36
37
           ]
37
38
 
38
39
from warnings import warn
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."
50
52
 
51
53
 
52
54
def set_warning_method(method):
178
180
    decorated_callable.is_deprecated = True
179
181
 
180
182
 
 
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,)
 
188
        if dep_dict._advice:
 
189
            msg += ' ' + dep_dict._advice
 
190
        warn(msg, DeprecationWarning, stacklevel=2)
 
191
        return wrapped_method(dep_dict, *args, **kwargs)
 
192
    return cb
 
193
 
 
194
 
 
195
class DeprecatedDict(dict):
 
196
    """A dictionary that complains when read or written."""
 
197
 
 
198
    is_deprecated = True
 
199
 
 
200
    def __init__(self,
 
201
        deprecation_version,
 
202
        variable_name,
 
203
        initial_value,
 
204
        advice,
 
205
        ):
 
206
        """Create a dict that warns when read or modified.
 
207
 
 
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.
 
213
        """
 
214
        self._deprecation_version = deprecation_version
 
215
        self._variable_name = variable_name
 
216
        self._advice = advice
 
217
        dict.__init__(self, initial_value)
 
218
 
 
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__)
 
227
 
 
228
 
181
229
def deprecated_list(deprecation_version, variable_name,
182
230
                    initial_value, extra=None):
183
231
    """Create a list that warns when modified