1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # Copyright (C) 2010, 2014 OpenERP s.a. (<http://openerp.com>).
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 __all__ = ['synchronized', 'lazy_property', 'classproperty']
25 from functools import wraps
26 from inspect import getsourcefile
29 class lazy_property(object):
30 """ Decorator for a lazy property of an object, i.e., an object attribute
31 that is determined by the result of a method call evaluated once. To
32 reevaluate the property, simply delete the attribute on the object, and
35 def __init__(self, fget):
38 def __get__(self, obj, cls):
41 value = self.fget(obj)
42 setattr(obj, self.fget.__name__, value)
47 return self.fget.__doc__
51 """ Reset all lazy properties on the instance `obj`. """
54 for name in obj_dict.keys():
55 if isinstance(getattr(cls, name, None), lazy_property):
59 def synchronized(lock_attr='_lock'):
62 def wrapper(self, *args, **kwargs):
63 lock = getattr(self, lock_attr)
66 return func(self, *args, **kwargs)
72 def frame_codeinfo(fframe, back=0):
73 """ Return a (filename, line) pair for a previous frame .
74 @return (filename, lineno) where lineno is either int or string==''
79 return "<unknown>", ''
81 fframe = fframe.f_back
83 fname = getsourcefile(fframe)
86 lineno = fframe.f_lineno or ''
89 return "<unknown>", ''
92 """ Composes the callables ``a`` and ``b``. ``compose(a, b)(*args)`` is
93 equivalent to ``a(b(*args))``.
95 Can be used as a decorator by partially applying ``a``::
102 def wrapper(*args, **kwargs):
103 return a(b(*args, **kwargs))
107 class _ClassProperty(property):
108 def __get__(self, cls, owner):
109 return self.fget.__get__(None, owner)()
111 def classproperty(func):
112 return _ClassProperty(classmethod(func))
114 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: