DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
conffwk.proxy Namespace Reference

Classes

class  Proxy

Functions

 make_proxy_class (theclass)
 _DelegateMetaFunction (clsName, bases, atts)

Variables

 _obj

Detailed Description

Proxing/Delegation tools

Provide several tools to implement proxying/delegation of objects. The proxying
instances expose the same public interface of the proxied object, but avoiding
inheritance. This allows to control the reference counts of the proxied object.

Function Documentation

◆ _DelegateMetaFunction()

_DelegateMetaFunction ( clsName,
bases,
atts )
protected
 Implements a delegation pattern using a metaclass approach

A class using this meta mechanism should have 'memberclass' class attribute
initialized at the class of the instance to proxied. The metaclass will
make sure the delegate class will expose all the public methods of the
proxied one.
Moreover, the metaclass will provide the delegate class with a '__init__'
function instantiating a 'memberclass' object, storing it in 'self._obj'.
The delegate class constructor method will therefore accept all the
arguments accepted by the proxied class constructor.
The delegate class uses slots

Definition at line 61 of file proxy.py.

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 # We force the proxied and delegate __init__
91 # functions take the same arguments
92 # This can be easily changed with some input from the
93 # delegate class
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)

◆ make_proxy_class()

make_proxy_class ( theclass)
Builds a delegation class out of a given type.

Uses the Proxy class to generate a new proxy class exposing the same
interface of the provided class and delegating the method calls to
the hosted object instance.

Definition at line 31 of file proxy.py.

31def make_proxy_class(theclass):
32 """Builds a delegation class out of a given type.
33
34 Uses the Proxy class to generate a new proxy class exposing the same
35 interface of the provided class and delegating the method calls to
36 the hosted object instance.
37
38 """
39 def make_method(name):
40 def method(self, *args, **kwds):
41 return getattr(self._obj, name)(*args, **kwds)
42 return method
43
44 base = Proxy
45 namespace = {}
46 for methodname in dir(theclass):
47 if not methodname.startswith('__'):
48 namespace[methodname] = make_method(methodname)
49
50 proxyclass = type("%s%s" % (theclass.__name__, base.__name__),
51 (base,), namespace)
52
53 return proxyclass
54
55
56# We do use a function instead of a class for the meta definition to avoid
57# problems with inheritance of the constructed classes. In fact, using a
58# function the type of the resulting classes is still 'type', therefore
59# the meta functionalities are not implemented in the subclasses
60

Variable Documentation

◆ _obj

conffwk.proxy._obj
protected

Definition at line 41 of file proxy.py.