1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
204
206
return write_locked
209
def only_raises(*errors):
210
"""Make a decorator that will only allow the given error classes to be
211
raised. All other errors will be logged and then discarded.
213
Typical use is something like::
215
@only_raises(LockNotHeld, LockBroken)
219
def decorator(unbound):
220
def wrapped(*args, **kwargs):
222
return unbound(*args, **kwargs)
226
trace.mutter('Error suppressed by only_raises:')
227
trace.log_exception_quietly()
228
wrapped.__doc__ = unbound.__doc__
229
wrapped.__name__ = unbound.__name__
207
234
# Default is more functionality, 'bzr' the commandline will request fast
209
236
needs_read_lock = _pretty_needs_read_lock
226
253
global needs_read_lock, needs_write_lock
227
254
needs_read_lock = _pretty_needs_read_lock
228
255
needs_write_lock = _pretty_needs_write_lock
258
# This implementation of cachedproperty is copied from Launchpad's
259
# canonical.launchpad.cachedproperty module (with permission from flacoste)
260
# -- spiv & vila 100120
261
def cachedproperty(attrname_or_fn):
262
"""A decorator for methods that makes them properties with their return
265
The value is cached on the instance, using the attribute name provided.
267
If you don't provide a name, the mangled name of the property is used.
269
>>> class CachedPropertyTest(object):
271
... @cachedproperty('_foo_cache')
273
... print 'foo computed'
278
... print 'bar computed'
281
>>> cpt = CachedPropertyTest()
282
>>> getattr(cpt, '_foo_cache', None) is None
294
>>> cpt._bar_cached_value
298
if isinstance(attrname_or_fn, basestring):
299
attrname = attrname_or_fn
300
return _CachedPropertyForAttr(attrname)
303
attrname = '_%s_cached_value' % fn.__name__
304
return _CachedProperty(attrname, fn)
307
class _CachedPropertyForAttr(object):
309
def __init__(self, attrname):
310
self.attrname = attrname
312
def __call__(self, fn):
313
return _CachedProperty(self.attrname, fn)
316
class _CachedProperty(object):
318
def __init__(self, attrname, fn):
320
self.attrname = attrname
321
self.marker = object()
323
def __get__(self, inst, cls=None):
326
cachedresult = getattr(inst, self.attrname, self.marker)
327
if cachedresult is self.marker:
328
result = self.fn(inst)
329
setattr(inst, self.attrname, result)