aboutsummaryrefslogtreecommitdiffstats
path: root/share/shadow.py
blob: 5cc7c9eba449f38426c1b109999a388c9a167b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : share
# File            : shadow.py
# Version         : $Id$
# Author          : Frederic Lepied
# Created On      : Sat Jan 26 17:38:39 2002
# Purpose         : loads a python module and creates another one
# on stdout. All the functions of the module are shadowed according
# to their doc string: "D" direct mapping, "1" indirect call but
# name + first arg used as the key and all other cases indirect
# call with the name as the key.
#---------------------------------------------------------------

import sys
import imp
import inspect

### strings used in the rewritting
direct_str = """
%s=%s.%s

"""

indirect_str = """
def %s(*args):
    indirect(\"%s\", %s.%s, %d, args)
    
"""

header = """

NONE=0
ALL=1
LOCAL=2

FAKE = {}

def indirect(name, func, type, args):
    if type == 1:
        key = (name, args[0])
    else:
        key = name
    FAKE[key] = (func, args)

def commit_changes():
    for f in FAKE.values():
        apply(f[0], f[1])

"""

### code
modulename = sys.argv[1]

module = __import__(modulename)

sys.stdout.write(header)

sys.stdout.write("import %s\n\n" % modulename)

for f in inspect.getmembers(module, inspect.isfunction):
    (args, varargs, varkw, locals) = inspect.getargspec(f[1])
    if f[1].__doc__ and f[1].__doc__[0] == 'D':
        #argspec = inspect.formatargspec(args, varargs, varkw, locals)
        s = direct_str % (f[0], modulename, f[0])
    else:
        if f[1].__doc__ and f[1].__doc__[0] == '1':
            type = 1
        else:
            type = 0
        s = indirect_str % (f[0], f[0], modulename, f[0], type)
    sys.stdout.write(s)

# shadow.py ends here