summaryrefslogtreecommitdiff
path: root/pypers/oxford/import_with_meta.py
blob: 92ef395fd69b55851d3c24ed5f619f840c57cd5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# import_with_metaclass.py

import inspect, types

def import_with_metaclass(metaclass, modname):
    "Module importer substituting custom metaclass"
    dct = {'__module__' : modname}
    mod = __import__(modname)
    for key, val in mod.__dict__.items():
        if inspect.isclass(val):
            if isinstance(val, types.ClassType):
                bases = (val, object) # convert old-style to new-style
            else:
                bases = (val,)
            setattr(mod, key, metaclass(key, bases, dct))
    return mod