]> git.decadent.org.uk Git - dak.git/blob - daklib/extensions.py
Remove shadowing builtins in daklib.dbconn.
[dak.git] / daklib / extensions.py
1 #!/usr/bin/env python
2
3 """
4 Utility functions for extensions
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2008 Anthony Towns <ajt@dbeian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 dak_functions_to_replace = {}
26 dak_replaced_functions = {}
27
28 def replace_dak_function(module, name):
29     """
30     Decorator to make a function replace a standard dak function
31     in a given module.
32
33     @type module: string
34     @param module: name of module where replaced function is in
35
36     @type name: string
37     @param name: name of the function to replace
38     """
39
40     def x(f):
41         def myfunc(*a,**kw):
42             global replaced_funcs
43             f(dak_replaced_functions[name], *a, **kw)
44         myfunc.__name__ = f.__name__
45         myfunc.__doc__ = f.__doc__
46         myfunc.__dict__.update(f.__dict__)
47
48         fnname = "%s:%s" % (module, name)
49         if fnname in dak_functions_to_replace:
50             raise Exception, \
51                 "%s in %s already marked to be replaced" % (name, module)
52         dak_functions_to_replace["%s:%s" % (module,name)] = myfunc
53         return f
54     return x
55
56 ################################################################################
57
58 def init(name, module, userext):
59     global dak_replaced_functions
60
61     # This bit should be done automatically too
62     dak_replaced_functions = {}
63     for f,newfunc in dak_functions_to_replace.iteritems():
64         m,f = f.split(":",1)
65         if len(f) > 0 and m == name:
66             dak_replaced_functions[f] = module.__dict__[f]
67             module.__dict__[f] = newfunc