56def _DelegateMetaFunction(clsName, bases, atts):
57 """ Implements a delegation pattern using a metaclass approach
58
59 A class using this meta mechanism should have 'memberclass' class attribute
60 initialized at the class of the instance to proxied. The metaclass will
61 make sure the delegate class will expose all the public methods of the
62 proxied one.
63 Moreover, the metaclass will provide the delegate class with a '__init__'
64 function instantiating a 'memberclass' object, storing it in 'self._obj'.
65 The delegate class constructor method will therefore accept all the
66 arguments accepted by the proxied class constructor.
67 The delegate class uses slots
68
69 """
70 memberclass = atts['memberclass']
71
72 def make_method(name):
73 def method(self, *args, **kwds):
74 return getattr(self._obj, name)(*args, **kwds)
75 method.__name__ = name
76 return method
77
78 for methodname in dir(memberclass):
79 if not methodname.startswith('__'):
80 atts[methodname] = make_method(methodname)
81
82 atts['__slots__'] = ['_obj', ]
83
84 def initfun(self, *args, **kwds):
85
86
87
88
89 obj = memberclass(*args, **kwds)
90 self._obj = obj
91
92 initfun.__doc__ = \
93 """Instantiate %s and store the instance in 'self._obj'
94
95 """ % str(memberclass)
96 initfun.__name__ = '__init__'
97
98 atts['__init__'] = initfun
99
100 return type(clsName, bases, atts)