Launchpad automatic translations update.
[odoo/odoo.git] / bin / 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 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__ = ['partial', 'wraps', 'update_wrapper', 'synchronized']
24
25 try:
26     from functools import partial, wraps, update_wrapper
27 except ImportError:
28     # The functools module doesn't exist in python < 2.5
29     # Code taken from python 2.5
30     # http://svn.python.org/view/python/tags/r254/Lib/functools.py?view=markup
31
32     def partial(fun, *args, **kwargs):
33         def _partial(*args2, **kwargs2):
34             return fun(*(args+args2), **dict(kwargs, **kwargs2))
35         return _partial
36
37     ### --- code from python 2.5
38
39     # update_wrapper() and wraps() are tools to help write
40     # wrapper functions that can handle naive introspection
41
42     WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
43     WRAPPER_UPDATES = ('__dict__',)
44     def update_wrapper(wrapper,
45                        wrapped,
46                        assigned = WRAPPER_ASSIGNMENTS,
47                        updated = WRAPPER_UPDATES):
48         """Update a wrapper function to look like the wrapped function
49
50            wrapper is the function to be updated
51            wrapped is the original function
52            assigned is a tuple naming the attributes assigned directly
53            from the wrapped function to the wrapper function (defaults to
54            functools.WRAPPER_ASSIGNMENTS)
55            updated is a tuple naming the attributes off the wrapper that
56            are updated with the corresponding attribute from the wrapped
57            function (defaults to functools.WRAPPER_UPDATES)
58         """
59         for attr in assigned:
60             setattr(wrapper, attr, getattr(wrapped, attr))
61         for attr in updated:
62             getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
63         # Return the wrapper so this can be used as a decorator via partial()
64         return wrapper
65
66     def wraps(wrapped,
67               assigned = WRAPPER_ASSIGNMENTS,
68               updated = WRAPPER_UPDATES):
69         """Decorator factory to apply update_wrapper() to a wrapper function
70
71            Returns a decorator that invokes update_wrapper() with the decorated
72            function as the wrapper argument and the arguments to wraps() as the
73            remaining arguments. Default arguments are as for update_wrapper().
74            This is a convenience function to simplify applying partial() to
75            update_wrapper().
76         """
77         return partial(update_wrapper, wrapped=wrapped,
78                        assigned=assigned, updated=updated)
79
80
81
82 def synchronized(lock_attr='_lock'):
83     def decorator(func):
84         @wraps(func)
85         def wrapper(self, *args, **kwargs):
86             lock = getattr(self, lock_attr)
87             try:
88                 lock.acquire()
89                 return func(self, *args, **kwargs)
90             finally:
91                 lock.release()
92         return wrapper
93     return decorator
94
95
96
97 from inspect import getsourcefile
98
99 def frame_codeinfo(fframe, back=0):
100     """ Return a (filename, line) pair for a previous frame .
101         @return (filename, lineno) where lineno is either int or string==''
102     """
103     
104     try:
105         if not fframe:
106             return ("<unknown>", '')
107         for i in range(back):
108             fframe = fframe.f_back
109         try:
110             fname = getsourcefile(fframe)
111         except TypeError:
112             fname = '<builtin>'
113         lineno = fframe.f_lineno or ''
114         return (fname, lineno)
115     except Exception:
116         return ("<unknown>", '')