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 def _partial(*args2, **kwargs2):
34 return fun(*(args+args2), **dict(kwargs, **kwargs2))
37 ### --- code from python 2.5
39 # update_wrapper() and wraps() are tools to help write
40 # wrapper functions that can handle naive introspection
42 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
43 WRAPPER_UPDATES = ('__dict__',)
44 def update_wrapper(wrapper,
46 assigned = WRAPPER_ASSIGNMENTS,
47 updated = WRAPPER_UPDATES):
48 """Update a wrapper function to look like the wrapped function
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)
60 setattr(wrapper, attr, getattr(wrapped, attr))
62 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
63 # Return the wrapper so this can be used as a decorator via partial()
67 assigned = WRAPPER_ASSIGNMENTS,
68 updated = WRAPPER_UPDATES):
69 """Decorator factory to apply update_wrapper() to a wrapper function
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
77 return partial(update_wrapper, wrapped=wrapped,
78 assigned=assigned, updated=updated)
82 def synchronized(lock_attr='_lock'):
85 def wrapper(self, *args, **kwargs):
86 lock = getattr(self, lock_attr)
89 return func(self, *args, **kwargs)
97 from inspect import getsourcefile
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==''
106 return ("<unknown>", '')
107 for i in range(back):
108 fframe = fframe.f_back
110 fname = getsourcefile(fframe)
113 lineno = fframe.f_lineno or ''
114 return (fname, lineno)
116 return ("<unknown>", '')