~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Cached properties for situations where a property is computed once and
then returned each time it is asked for.

The clear_cachedproperties function can be used to wipe the cache of properties
from an instance.
"""

__metaclass__ = type

__all__ = [
    'cache_property',
    'cachedproperty',
    'clear_cachedproperties',
    'clear_property',
    ]

from zope.security.proxy import removeSecurityProxy

from canonical.lazr.utils import safe_hasattr

# XXX: JonathanLange 2010-01-11 bug=505731: Move this to lp.services.

def cachedproperty(attrname_or_fn):
    """A decorator for methods that makes them properties with their return
    value cached.

    The value is cached on the instance, using the attribute name provided.

    If you don't provide a name, the mangled name of the property is used.

    cachedproperty is not threadsafe - it should not be used on objects which
    are shared across threads / external locking should be used on those
    objects.

    >>> class CachedPropertyTest(object):
    ...
    ...     @cachedproperty('_foo_cache')
    ...     def foo(self):
    ...         print 'foo computed'
    ...         return 23
    ...
    ...     @cachedproperty
    ...     def bar(self):
    ...         print 'bar computed'
    ...         return 69

    >>> cpt = CachedPropertyTest()
    >>> getattr(cpt, '_foo_cache', None) is None
    True
    >>> cpt.foo
    foo computed
    23
    >>> cpt.foo
    23
    >>> cpt._foo_cache
    23
    >>> cpt.bar
    bar computed
    69
    >>> cpt._bar_cached_value
    69
    
    Cached properties are listed on instances.
    >>> sorted(cpt._cached_properties)
    ['_bar_cached_value', '_foo_cache']

    """
    if isinstance(attrname_or_fn, basestring):
        attrname = attrname_or_fn
        return CachedPropertyForAttr(attrname)
    else:
        fn = attrname_or_fn
        attrname = '_%s_cached_value' % fn.__name__
        return CachedProperty(attrname, fn)

def cache_property(instance, attrname, value):
    """Cache value on instance as attrname.
    
    instance._cached_properties is updated with attrname.

        >>> class CachedPropertyTest(object):
        ...
        ...     @cachedproperty('_foo_cache')
        ...     def foo(self):
        ...         return 23
        ...
        >>> instance = CachedPropertyTest()
        >>> cache_property(instance, '_foo_cache', 42)
        >>> instance.foo
        42
        >>> instance._cached_properties
        ['_foo_cache']

    Caching a new value does not duplicate the cache keys.

        >>> cache_property(instance, '_foo_cache', 84)
        >>> instance._cached_properties
        ['_foo_cache']

    And does update the cached value.

        >>> instance.foo
        84
    """
    naked_instance = removeSecurityProxy(instance)
    clear_property(naked_instance, attrname)
    setattr(naked_instance, attrname, value)
    cached_properties = getattr(naked_instance, '_cached_properties', [])
    cached_properties.append(attrname)
    naked_instance._cached_properties = cached_properties


def clear_property(instance, attrname):
    """Remove a cached attribute from instance.

    The attribute name is removed from instance._cached_properties.

    If the property is not cached, nothing happens.

    :seealso clear_cachedproperties: For clearing all cached items at once.

    >>> class CachedPropertyTest(object):
    ...
    ...     @cachedproperty('_foo_cache')
    ...     def foo(self):
    ...         return 23
    ...
    >>> instance = CachedPropertyTest()
    >>> instance.foo
    23
    >>> clear_property(instance, '_foo_cache')
    >>> instance._cached_properties
    []
    >>> is_cached(instance, '_foo_cache')
    False
    >>> clear_property(instance, '_foo_cache')
    """
    naked_instance = removeSecurityProxy(instance)
    if not is_cached(naked_instance, attrname):
        return
    delattr(naked_instance, attrname)
    naked_instance._cached_properties.remove(attrname)


def clear_cachedproperties(instance):
    """Clear cached properties from an object.
    
    >>> class CachedPropertyTest(object):
    ...
    ...     @cachedproperty('_foo_cache')
    ...     def foo(self):
    ...         return 23
    ...
    >>> instance = CachedPropertyTest()
    >>> instance.foo
    23
    >>> instance._cached_properties
    ['_foo_cache']
    >>> clear_cachedproperties(instance)
    >>> instance._cached_properties
    []
    >>> hasattr(instance, '_foo_cache')
    False
    """
    naked_instance = removeSecurityProxy(instance)
    cached_properties = getattr(naked_instance, '_cached_properties', [])
    for property_name in cached_properties:
        delattr(naked_instance, property_name)
    naked_instance._cached_properties = []


def is_cached(instance, attrname):
    """Return True if attrname is cached on instance.

    >>> class CachedPropertyTest(object):
    ...
    ...     @cachedproperty('_foo_cache')
    ...     def foo(self):
    ...         return 23
    ...
    >>> instance = CachedPropertyTest()
    >>> instance.foo
    23
    >>> is_cached(instance, '_foo_cache')
    True
    >>> is_cached(instance, '_var_cache')
    False
    """
    naked_instance = removeSecurityProxy(instance)
    return safe_hasattr(naked_instance, attrname)


class CachedPropertyForAttr:
    """Curry a decorator to provide arguments to the CachedProperty."""

    def __init__(self, attrname):
        self.attrname = attrname

    def __call__(self, fn):
        return CachedProperty(self.attrname, fn)


class CachedProperty:

    # Used to detect not-yet-cached properties.
    sentinel = object()

    def __init__(self, attrname, fn):
        self.fn = fn
        self.attrname = attrname

    def __get__(self, inst, cls=None):
        if inst is None:
            return self
        cachedresult = getattr(inst, self.attrname, CachedProperty.sentinel)
        if cachedresult is CachedProperty.sentinel:
            result = self.fn(inst)
            cache_property(inst, self.attrname, result)
            return result
        else:
            return cachedresult


if __name__ == '__main__':
    import doctest
    doctest.testmod()