/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Martin Albisetti
  • Date: 2008-05-12 00:59:13 UTC
  • mto: (3431.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 3432.
  • Revision ID: argentina@gmail.com-20080512005913-26gslxhc7e2ry4yr
 * Added Makefile for spanish docs
 * Created the user guide index

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com> and others
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
"""Symbol versioning
 
19
 
 
20
The methods here allow for api symbol versioning.
 
21
"""
 
22
 
 
23
__all__ = ['deprecated_function',
 
24
           'deprecated_in',
 
25
           'deprecated_list',
 
26
           'deprecated_method',
 
27
           'DEPRECATED_PARAMETER',
 
28
           'deprecated_passed',
 
29
           'set_warning_method',
 
30
           'warn',
 
31
           'zero_seven',
 
32
           'zero_eight',
 
33
           'zero_nine',
 
34
           'zero_ten',
 
35
           'zero_eleven',
 
36
           'zero_twelve',
 
37
           'zero_thirteen',
 
38
           'zero_fourteen',
 
39
           'zero_fifteen',
 
40
           'zero_sixteen',
 
41
           'zero_seventeen',
 
42
           'zero_eighteen',
 
43
           'zero_ninety',
 
44
           'zero_ninetyone',
 
45
           'zero_ninetytwo',
 
46
           'zero_ninetythree',
 
47
           'one_zero',
 
48
           'one_one',
 
49
           'one_two',
 
50
           'one_three',
 
51
           'one_four',
 
52
           'one_five',
 
53
           'one_six',
 
54
           ]
 
55
 
 
56
from warnings import warn
 
57
 
 
58
import bzrlib
 
59
 
 
60
 
 
61
DEPRECATED_PARAMETER = "A deprecated parameter marker."
 
62
zero_seven = "%s was deprecated in version 0.7."
 
63
zero_eight = "%s was deprecated in version 0.8."
 
64
zero_nine = "%s was deprecated in version 0.9."
 
65
zero_ten = "%s was deprecated in version 0.10."
 
66
zero_eleven = "%s was deprecated in version 0.11."
 
67
zero_twelve = "%s was deprecated in version 0.12."
 
68
zero_thirteen = "%s was deprecated in version 0.13."
 
69
zero_fourteen = "%s was deprecated in version 0.14."
 
70
zero_fifteen = "%s was deprecated in version 0.15."
 
71
zero_sixteen = "%s was deprecated in version 0.16."
 
72
zero_seventeen = "%s was deprecated in version 0.17."
 
73
zero_eighteen = "%s was deprecated in version 0.18."
 
74
zero_ninety = "%s was deprecated in version 0.90."
 
75
zero_ninetyone = "%s was deprecated in version 0.91."
 
76
zero_ninetytwo = "%s was deprecated in version 0.92."
 
77
one_zero = "%s was deprecated in version 1.0."
 
78
zero_ninetythree = one_zero # Maintained for backwards compatibility
 
79
one_one = "%s was deprecated in version 1.1."
 
80
one_two = "%s was deprecated in version 1.2."
 
81
one_three = "%s was deprecated in version 1.3."
 
82
one_four = "%s was deprecated in version 1.4."
 
83
one_five = "%s was deprecated in version 1.5."
 
84
one_six = "%s was deprecated in version 1.6."
 
85
 
 
86
 
 
87
def deprecated_in(version_tuple):
 
88
    """Generate a message that something was deprecated in a release.
 
89
 
 
90
    >>> deprecated_in((1, 4, 0))
 
91
    '%s was deprecated in version 1.4'
 
92
    """
 
93
    return ("%s was deprecated in version "
 
94
            + bzrlib._format_version_tuple(version_tuple))
 
95
 
 
96
def set_warning_method(method):
 
97
    """Set the warning method to be used by this module.
 
98
 
 
99
    It should take a message and a warning category as warnings.warn does.
 
100
    """
 
101
    global warn
 
102
    warn = method
 
103
 
 
104
 
 
105
# TODO - maybe this would be easier to use as one 'smart' method that
 
106
# guess if it is a method or a class or an attribute ? If so, we can
 
107
# add that on top of the primitives, once we have all three written
 
108
# - RBC 20050105
 
109
 
 
110
 
 
111
def deprecation_string(a_callable, deprecation_version):
 
112
    """Generate an automatic deprecation string for a_callable.
 
113
 
 
114
    :param a_callable: The callable to substitute into deprecation_version.
 
115
    :param deprecation_version: A deprecation format warning string. This should
 
116
        have a single %s operator in it. a_callable will be turned into a nice
 
117
        python symbol and then substituted into deprecation_version.
 
118
    """
 
119
    # We also want to handle old-style classes, in particular exception, and
 
120
    # they don't have an im_class attribute.
 
121
    if getattr(a_callable, 'im_class', None) is None:
 
122
        symbol = "%s.%s" % (a_callable.__module__,
 
123
                            a_callable.__name__)
 
124
    else:
 
125
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
 
126
                               a_callable.im_class.__name__,
 
127
                               a_callable.__name__
 
128
                               )
 
129
    return deprecation_version % symbol
 
130
 
 
131
 
 
132
def deprecated_function(deprecation_version):
 
133
    """Decorate a function so that use of it will trigger a warning."""
 
134
 
 
135
    def function_decorator(callable):
 
136
        """This is the function python calls to perform the decoration."""
 
137
        
 
138
        def decorated_function(*args, **kwargs):
 
139
            """This is the decorated function."""
 
140
            warn(deprecation_string(callable, deprecation_version),
 
141
                DeprecationWarning, stacklevel=2)
 
142
            return callable(*args, **kwargs)
 
143
        _populate_decorated(callable, deprecation_version, "function",
 
144
                            decorated_function)
 
145
        return decorated_function
 
146
    return function_decorator
 
147
 
 
148
 
 
149
def deprecated_method(deprecation_version):
 
150
    """Decorate a method so that use of it will trigger a warning.
 
151
 
 
152
    To deprecate a static or class method, use 
 
153
 
 
154
        @staticmethod
 
155
        @deprecated_function
 
156
        def ...
 
157
    
 
158
    To deprecate an entire class, decorate __init__.
 
159
    """
 
160
 
 
161
    def method_decorator(callable):
 
162
        """This is the function python calls to perform the decoration."""
 
163
        
 
164
        def decorated_method(self, *args, **kwargs):
 
165
            """This is the decorated method."""
 
166
            if callable.__name__ == '__init__':
 
167
                symbol = "%s.%s" % (self.__class__.__module__,
 
168
                                    self.__class__.__name__,
 
169
                                    )
 
170
            else:
 
171
                symbol = "%s.%s.%s" % (self.__class__.__module__,
 
172
                                       self.__class__.__name__,
 
173
                                       callable.__name__
 
174
                                       )
 
175
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
 
176
            return callable(self, *args, **kwargs)
 
177
        _populate_decorated(callable, deprecation_version, "method",
 
178
                            decorated_method)
 
179
        return decorated_method
 
180
    return method_decorator
 
181
 
 
182
 
 
183
def deprecated_passed(parameter_value):
 
184
    """Return True if parameter_value was used."""
 
185
    # FIXME: it might be nice to have a parameter deprecation decorator. 
 
186
    # it would need to handle positional and *args and **kwargs parameters,
 
187
    # which means some mechanism to describe how the parameter was being
 
188
    # passed before deprecation, and some way to deprecate parameters that
 
189
    # were not at the end of the arg list. Thats needed for __init__ where
 
190
    # we cannot just forward to a new method name.I.e. in the following
 
191
    # examples we would want to have callers that pass any value to 'bad' be
 
192
    # given a warning - because we have applied:
 
193
    # @deprecated_parameter('bad', deprecated_in((1, 5, 0))
 
194
    #
 
195
    # def __init__(self, bad=None)
 
196
    # def __init__(self, bad, other)
 
197
    # def __init__(self, **kwargs)
 
198
    # RBC 20060116
 
199
    return not parameter_value is DEPRECATED_PARAMETER
 
200
 
 
201
 
 
202
def _decorate_docstring(callable, deprecation_version, label,
 
203
                        decorated_callable):
 
204
    if callable.__doc__:
 
205
        docstring_lines = callable.__doc__.split('\n')
 
206
    else:
 
207
        docstring_lines = []
 
208
    if len(docstring_lines) == 0:
 
209
        decorated_callable.__doc__ = deprecation_version % ("This " + label)
 
210
    elif len(docstring_lines) == 1:
 
211
        decorated_callable.__doc__ = (callable.__doc__ 
 
212
                                    + "\n"
 
213
                                    + "\n"
 
214
                                    + deprecation_version % ("This " + label)
 
215
                                    + "\n")
 
216
    else:
 
217
        spaces = len(docstring_lines[-1])
 
218
        new_doc = callable.__doc__
 
219
        new_doc += "\n" + " " * spaces
 
220
        new_doc += deprecation_version % ("This " + label)
 
221
        new_doc += "\n" + " " * spaces
 
222
        decorated_callable.__doc__ = new_doc
 
223
 
 
224
 
 
225
def _populate_decorated(callable, deprecation_version, label,
 
226
                        decorated_callable):
 
227
    """Populate attributes like __name__ and __doc__ on the decorated callable.
 
228
    """
 
229
    _decorate_docstring(callable, deprecation_version, label,
 
230
                        decorated_callable)
 
231
    decorated_callable.__module__ = callable.__module__
 
232
    decorated_callable.__name__ = callable.__name__
 
233
    decorated_callable.is_deprecated = True
 
234
 
 
235
 
 
236
def _dict_deprecation_wrapper(wrapped_method):
 
237
    """Returns a closure that emits a warning and calls the superclass"""
 
238
    def cb(dep_dict, *args, **kwargs):
 
239
        msg = 'access to %s' % (dep_dict._variable_name, )
 
240
        msg = dep_dict._deprecation_version % (msg,)
 
241
        if dep_dict._advice:
 
242
            msg += ' ' + dep_dict._advice
 
243
        warn(msg, DeprecationWarning, stacklevel=2)
 
244
        return wrapped_method(dep_dict, *args, **kwargs)
 
245
    return cb
 
246
 
 
247
 
 
248
class DeprecatedDict(dict):
 
249
    """A dictionary that complains when read or written."""
 
250
 
 
251
    is_deprecated = True
 
252
 
 
253
    def __init__(self,
 
254
        deprecation_version,
 
255
        variable_name,
 
256
        initial_value,
 
257
        advice,
 
258
        ):
 
259
        """Create a dict that warns when read or modified.
 
260
 
 
261
        :param deprecation_version: string for the warning format to raise,
 
262
            typically from deprecated_in()
 
263
        :param initial_value: The contents of the dict
 
264
        :param variable_name: This allows better warnings to be printed
 
265
        :param advice: String of advice on what callers should do instead 
 
266
            of using this variable.
 
267
        """
 
268
        self._deprecation_version = deprecation_version
 
269
        self._variable_name = variable_name
 
270
        self._advice = advice
 
271
        dict.__init__(self, initial_value)
 
272
 
 
273
    # This isn't every possible method but it should trap anyone using the
 
274
    # dict -- add more if desired
 
275
    __len__ = _dict_deprecation_wrapper(dict.__len__)
 
276
    __getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
 
277
    __setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
 
278
    __delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
 
279
    keys = _dict_deprecation_wrapper(dict.keys)
 
280
    __contains__ = _dict_deprecation_wrapper(dict.__contains__)
 
281
 
 
282
 
 
283
def deprecated_list(deprecation_version, variable_name,
 
284
                    initial_value, extra=None):
 
285
    """Create a list that warns when modified
 
286
 
 
287
    :param deprecation_version: string for the warning format to raise,
 
288
        typically from deprecated_in()
 
289
    :param initial_value: The contents of the list
 
290
    :param variable_name: This allows better warnings to be printed
 
291
    :param extra: Extra info to print when printing a warning
 
292
    """
 
293
 
 
294
    subst_text = 'Modifying %s' % (variable_name,)
 
295
    msg = deprecation_version % (subst_text,)
 
296
    if extra:
 
297
        msg += ' ' + extra
 
298
 
 
299
    class _DeprecatedList(list):
 
300
        __doc__ = list.__doc__ + msg
 
301
 
 
302
        is_deprecated = True
 
303
 
 
304
        def _warn_deprecated(self, func, *args, **kwargs):
 
305
            warn(msg, DeprecationWarning, stacklevel=3)
 
306
            return func(self, *args, **kwargs)
 
307
            
 
308
        def append(self, obj):
 
309
            """appending to %s is deprecated""" % (variable_name,)
 
310
            return self._warn_deprecated(list.append, obj)
 
311
 
 
312
        def insert(self, index, obj):
 
313
            """inserting to %s is deprecated""" % (variable_name,)
 
314
            return self._warn_deprecated(list.insert, index, obj)
 
315
 
 
316
        def extend(self, iterable):
 
317
            """extending %s is deprecated""" % (variable_name,)
 
318
            return self._warn_deprecated(list.extend, iterable)
 
319
 
 
320
        def remove(self, value):
 
321
            """removing from %s is deprecated""" % (variable_name,)
 
322
            return self._warn_deprecated(list.remove, value)
 
323
 
 
324
        def pop(self, index=None):
 
325
            """pop'ing from from %s is deprecated""" % (variable_name,)
 
326
            if index:
 
327
                return self._warn_deprecated(list.pop, index)
 
328
            else:
 
329
                # Can't pass None
 
330
                return self._warn_deprecated(list.pop)
 
331
 
 
332
    return _DeprecatedList(initial_value)