]> git.decadent.org.uk Git - dak.git/blob - daklib/extensions.py
9befa7bf6c427984968b9ad42c336c1d9ef9ffee
[dak.git] / daklib / extensions.py
1 #!/usr/bin/env python
2
3 # Utility functions for extensions
4 # Copyright (C) 2008 Anthony Towns <ajt@dbeian.org>
5
6 ################################################################################
7
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.
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 General Public License for more details.
17
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
21
22 ################################################################################
23
24 dak_functions_to_replace = {}
25 dak_replaced_functions = {}
26
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."""
31
32     def x(f):
33         def myfunc(*a,**kw):
34             global replaced_funcs
35             f(dak_replaced_functions[name], *a, **kw)
36         myfunc.__name__ = f.__name__
37         myfunc.__doc__ = f.__doc__
38         myfunc.__dict__.update(f.__dict__)
39
40         fnname = "%s:%s" % (module, name)
41         if fnname in dak_functions_to_replace:
42             raise Exception, \
43                 "%s in %s already marked to be replaced" % (name, module)
44         dak_functions_to_replace["%s:%s" % (module,name)] = myfunc
45         return f
46     return x
47
48 ################################################################################
49
50 def init(name, module, userext):
51     global dak_replaced_functions
52
53     # This bit should be done automatically too
54     dak_replaced_functions = {}
55     for f,newfunc in dak_functions_to_replace.iteritems():
56         m,f = f.split(":",1)
57         if len(f) > 0 and m == name:
58             dak_replaced_functions[f] = module.__dict__[f]
59             module.__dict__[f] = newfunc