27 """Builds a delegation class out of a given type.
29 Uses the Proxy class to generate a new proxy class exposing the same
30 interface of the provided class and delegating the method calls to
31 the hosted object instance.
34 def make_method(name):
35 def method(self, *args, **kwds):
36 return getattr(self._obj, name)(*args, **kwds)
41 for methodname
in dir(theclass):
42 if not methodname.startswith(
'__'):
43 namespace[methodname] = make_method(methodname)
45 proxyclass = type(
"%s%s" % (theclass.__name__, base.__name__),
57 """ Implements a delegation pattern using a metaclass approach
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
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
70 memberclass = atts[
'memberclass']
72 def make_method(name):
73 def method(self, *args, **kwds):
74 return getattr(self._obj, name)(*args, **kwds)
75 method.__name__ = name
78 for methodname
in dir(memberclass):
79 if not methodname.startswith(
'__'):
80 atts[methodname] = make_method(methodname)
82 atts[
'__slots__'] = [
'_obj', ]
84 def initfun(self, *args, **kwds):
89 obj = memberclass(*args, **kwds)
93 """Instantiate %s and store the instance in 'self._obj'
95 """ % str(memberclass)
96 initfun.__name__ =
'__init__'
98 atts[
'__init__'] = initfun
100 return type(clsName, bases, atts)