1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 __all__ = ['partial', 'wraps', 'update_wrapper']
25 from functools import partial, wraps, update_wrapper
27 # The functools module doesn't exist in python < 2.5
28 # Code taken from python 2.5
29 # http://svn.python.org/view/python/tags/r254/Lib/functools.py?view=markup
31 def partial(fun, *args, **kwargs):
32 def _partial(*args2, **kwargs2):
33 return fun(*(args+args2), **dict(kwargs, **kwargs2))
36 ### --- code from python 2.5
38 # update_wrapper() and wraps() are tools to help write
39 # wrapper functions that can handle naive introspection
41 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
42 WRAPPER_UPDATES = ('__dict__',)
43 def update_wrapper(wrapper,
45 assigned = WRAPPER_ASSIGNMENTS,
46 updated = WRAPPER_UPDATES):
47 """Update a wrapper function to look like the wrapped function
49 wrapper is the function to be updated
50 wrapped is the original function
51 assigned is a tuple naming the attributes assigned directly
52 from the wrapped function to the wrapper function (defaults to
53 functools.WRAPPER_ASSIGNMENTS)
54 updated is a tuple naming the attributes off the wrapper that
55 are updated with the corresponding attribute from the wrapped
56 function (defaults to functools.WRAPPER_UPDATES)
59 setattr(wrapper, attr, getattr(wrapped, attr))
61 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
62 # Return the wrapper so this can be used as a decorator via partial()
66 assigned = WRAPPER_ASSIGNMENTS,
67 updated = WRAPPER_UPDATES):
68 """Decorator factory to apply update_wrapper() to a wrapper function
70 Returns a decorator that invokes update_wrapper() with the decorated
71 function as the wrapper argument and the arguments to wraps() as the
72 remaining arguments. Default arguments are as for update_wrapper().
73 This is a convenience function to simplify applying partial() to
76 return partial(update_wrapper, wrapped=wrapped,
77 assigned=assigned, updated=updated)