/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
16
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
17
from __future__ import absolute_import
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from . import trace
4634.85.9 by Andrew Bennetts
Add some experimental decorators: @only_raises(..) and @cleanup_method.
20
3316.3.1 by Andrew Bennetts
Try not to lose the original exception in needs_write_lock decorator if the unlock raises a secondary exception.
21
4634.85.9 by Andrew Bennetts
Add some experimental decorators: @only_raises(..) and @cleanup_method.
22
def only_raises(*errors):
4634.62.2 by Andrew Bennetts
Update test_decorators, add docstring.
23
    """Make a decorator that will only allow the given error classes to be
24
    raised.  All other errors will be logged and then discarded.
25
26
    Typical use is something like::
27
28
        @only_raises(LockNotHeld, LockBroken)
29
        def unlock(self):
30
            # etc
31
    """
4634.85.9 by Andrew Bennetts
Add some experimental decorators: @only_raises(..) and @cleanup_method.
32
    def decorator(unbound):
33
        def wrapped(*args, **kwargs):
34
            try:
35
                return unbound(*args, **kwargs)
36
            except errors:
37
                raise
38
            except:
39
                trace.mutter('Error suppressed by only_raises:')
40
                trace.log_exception_quietly()
41
        wrapped.__doc__ = unbound.__doc__
42
        wrapped.__name__ = unbound.__name__
43
        return wrapped
44
    return decorator
45
46
4869.3.32 by Andrew Bennetts
Use Launchpad's cachedproperty decorator instead of my stupidly broken one.
47
# This implementation of cachedproperty is copied from Launchpad's
4869.3.34 by Vincent Ladeuil
Finish the patch based on reviews.
48
# canonical.launchpad.cachedproperty module (with permission from flacoste)
49
# -- spiv & vila 100120
4869.3.32 by Andrew Bennetts
Use Launchpad's cachedproperty decorator instead of my stupidly broken one.
50
def cachedproperty(attrname_or_fn):
51
    """A decorator for methods that makes them properties with their return
52
    value cached.
53
54
    The value is cached on the instance, using the attribute name provided.
55
56
    If you don't provide a name, the mangled name of the property is used.
57
58
    >>> class CachedPropertyTest(object):
59
    ...
60
    ...     @cachedproperty('_foo_cache')
61
    ...     def foo(self):
62
    ...         print 'foo computed'
63
    ...         return 23
64
    ...
65
    ...     @cachedproperty
66
    ...     def bar(self):
67
    ...         print 'bar computed'
68
    ...         return 69
69
70
    >>> cpt = CachedPropertyTest()
71
    >>> getattr(cpt, '_foo_cache', None) is None
72
    True
73
    >>> cpt.foo
74
    foo computed
75
    23
76
    >>> cpt.foo
77
    23
78
    >>> cpt._foo_cache
79
    23
80
    >>> cpt.bar
81
    bar computed
82
    69
83
    >>> cpt._bar_cached_value
84
    69
85
4869.3.13 by Andrew Bennetts
Add simple cachedproperty decorator, and add {this,other,base}_lines cachedproperties to MergeHookParams.
86
    """
6621.2.26 by Martin
Misc set of changes to get started with selftest on Python 3
87
    if isinstance(attrname_or_fn, str):
4869.3.32 by Andrew Bennetts
Use Launchpad's cachedproperty decorator instead of my stupidly broken one.
88
        attrname = attrname_or_fn
89
        return _CachedPropertyForAttr(attrname)
90
    else:
91
        fn = attrname_or_fn
92
        attrname = '_%s_cached_value' % fn.__name__
93
        return _CachedProperty(attrname, fn)
94
95
96
class _CachedPropertyForAttr(object):
97
98
    def __init__(self, attrname):
99
        self.attrname = attrname
100
101
    def __call__(self, fn):
102
        return _CachedProperty(self.attrname, fn)
103
104
105
class _CachedProperty(object):
106
107
    def __init__(self, attrname, fn):
108
        self.fn = fn
109
        self.attrname = attrname
110
        self.marker = object()
111
112
    def __get__(self, inst, cls=None):
113
        if inst is None:
114
            return self
115
        cachedresult = getattr(inst, self.attrname, self.marker)
116
        if cachedresult is self.marker:
117
            result = self.fn(inst)
118
            setattr(inst, self.attrname, result)
119
            return result
120
        else:
121
            return cachedresult