[MERGE] forward port of branch 8.0 up to 92c7874
[odoo/odoo.git] / openerp / tools / func.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
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>).
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 __all__ = ['synchronized', 'lazy_property', 'classproperty', 'conditional']
24
25 from functools import wraps
26 from inspect import getsourcefile
27
28
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
33         get it again.
34     """
35     def __init__(self, fget):
36         self.fget = fget
37
38     def __get__(self, obj, cls):
39         if obj is None:
40             return self
41         value = self.fget(obj)
42         setattr(obj, self.fget.__name__, value)
43         return value
44
45     @property
46     def __doc__(self):
47         return self.fget.__doc__
48
49     @staticmethod
50     def reset_all(obj):
51         """ Reset all lazy properties on the instance `obj`. """
52         cls = type(obj)
53         obj_dict = vars(obj)
54         for name in obj_dict.keys():
55             if isinstance(getattr(cls, name, None), lazy_property):
56                 obj_dict.pop(name)
57
58 def conditional(condition, decorator):
59     """ Decorator for a conditionally applied decorator.
60
61         Example:
62
63            @conditional(get_config('use_cache'), ormcache)
64            def fn():
65                pass
66     """
67     if condition:
68         return decorator
69     else:
70         return lambda fn: fn
71
72 def synchronized(lock_attr='_lock'):
73     def decorator(func):
74         @wraps(func)
75         def wrapper(self, *args, **kwargs):
76             lock = getattr(self, lock_attr)
77             try:
78                 lock.acquire()
79                 return func(self, *args, **kwargs)
80             finally:
81                 lock.release()
82         return wrapper
83     return decorator
84
85 def frame_codeinfo(fframe, back=0):
86     """ Return a (filename, line) pair for a previous frame .
87         @return (filename, lineno) where lineno is either int or string==''
88     """
89     
90     try:
91         if not fframe:
92             return "<unknown>", ''
93         for i in range(back):
94             fframe = fframe.f_back
95         try:
96             fname = getsourcefile(fframe)
97         except TypeError:
98             fname = '<builtin>'
99         lineno = fframe.f_lineno or ''
100         return fname, lineno
101     except Exception:
102         return "<unknown>", ''
103
104 def compose(a, b):
105     """ Composes the callables ``a`` and ``b``. ``compose(a, b)(*args)`` is
106     equivalent to ``a(b(*args))``.
107
108     Can be used as a decorator by partially applying ``a``::
109
110          @partial(compose, a)
111          def b():
112             ...
113     """
114     @wraps(b)
115     def wrapper(*args, **kwargs):
116         return a(b(*args, **kwargs))
117     return wrapper
118
119
120 class _ClassProperty(property):
121     def __get__(self, cls, owner):
122         return self.fget.__get__(None, owner)()
123
124 def classproperty(func):
125     return _ClassProperty(classmethod(func))
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: