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 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__ = ['partial', 'wraps', 'update_wrapper', 'synchronized']
26 from functools import partial, wraps, update_wrapper
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
32 def partial(fun, *args, **kwargs):
33 """ Partial implementation
35 See: http://svn.python.org/view/python/tags/r254/Lib/functools.py
37 def _partial(*args2, **kwargs2):
38 return fun(*(args+args2), **dict(kwargs, **kwargs2))
41 ### --- code from python 2.5
43 # update_wrapper() and wraps() are tools to help write
44 # wrapper functions that can handle naive introspection
46 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
47 WRAPPER_UPDATES = ('__dict__',)
48 def update_wrapper(wrapper,
50 assigned = WRAPPER_ASSIGNMENTS,
51 updated = WRAPPER_UPDATES):
52 """Update a wrapper function to look like the wrapped function
54 wrapper is the function to be updated
55 wrapped is the original function
56 assigned is a tuple naming the attributes assigned directly
57 from the wrapped function to the wrapper function (defaults to
58 functools.WRAPPER_ASSIGNMENTS)
59 updated is a tuple naming the attributes off the wrapper that
60 are updated with the corresponding attribute from the wrapped
61 function (defaults to functools.WRAPPER_UPDATES)
64 setattr(wrapper, attr, getattr(wrapped, attr))
66 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
67 # Return the wrapper so this can be used as a decorator via partial()
71 assigned = WRAPPER_ASSIGNMENTS,
72 updated = WRAPPER_UPDATES):
73 """Decorator factory to apply update_wrapper() to a wrapper function
75 Returns a decorator that invokes update_wrapper() with the decorated
76 function as the wrapper argument and the arguments to wraps() as the
77 remaining arguments. Default arguments are as for update_wrapper().
78 This is a convenience function to simplify applying partial() to
81 return partial(update_wrapper, wrapped=wrapped,
82 assigned=assigned, updated=updated)
86 def synchronized(lock_attr='_lock'):
89 def wrapper(self, *args, **kwargs):
90 lock = getattr(self, lock_attr)
93 return func(self, *args, **kwargs)
101 from inspect import getsourcefile
103 def frame_codeinfo(fframe, back=0):
104 """ Return a (filename, line) pair for a previous frame .
105 @return (filename, lineno) where lineno is either int or string==''
110 return ("<unknown>", '')
111 for i in range(back):
112 fframe = fframe.f_back
114 fname = getsourcefile(fframe)
117 lineno = fframe.f_lineno or ''
118 return (fname, lineno)
120 return ("<unknown>", '')