3 # Utility functions for extensions
4 # Copyright (C) 2008 Anthony Towns <ajt@dbeian.org>
6 ################################################################################
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (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 General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ################################################################################
24 dak_functions_to_replace = {}
25 dak_replaced_functions = {}
27 def replace_dak_function(module,name):
28 """Decorator to make a function replace a standard dak function
29 in a given module. The replaced function will be provided as
30 the first argument."""
35 f(dak_replaced_functions[name], *a, **kw)
36 myfunc.__name__ = f.__name__
37 myfunc.__doc__ = f.__doc__
38 myfunc.__dict__.update(f.__dict__)
40 fnname = "%s:%s" % (module, name)
41 if fnname in dak_functions_to_replace:
43 "%s in %s already marked to be replaced" % (name, module)
44 dak_functions_to_replace["%s:%s" % (module,name)] = myfunc
48 ################################################################################
50 def init(name, module, userext):
51 global dak_replaced_functions
53 # This bit should be done automatically too
54 dak_replaced_functions = {}
55 for f,newfunc in dak_functions_to_replace.iteritems():
57 if len(f) > 0 and m == name:
58 dak_replaced_functions[f] = module.__dict__[f]
59 module.__dict__[f] = newfunc