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