DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
proxy.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# DUNE DAQ modification notice:
3# This file has been modified from the original ATLAS config source for the DUNE DAQ project.
4# Fork baseline commit: 67a24e731 (2022-10-27).
5# Renamed since fork: yes (from python/config/proxy.py to python/conffwk/proxy.py).
6
7
8
9"""Proxing/Delegation tools
10
11Provide several tools to implement proxying/delegation of objects. The proxying
12instances expose the same public interface of the proxied object, but avoiding
13inheritance. This allows to control the reference counts of the proxied object.
14
15"""
16# Explicit proxying mechanism not using metaclasses
17
18
19class Proxy(object):
20 """A very basic holder class
21
22 Just holds the reference to a provided object.
23
24 """
25
26 def __init__(self, obj):
27 super(Proxy, self).__init__()
28 self._obj = obj
29
30
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
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)
__init__(self, obj)
Definition proxy.py:26
make_proxy_class(theclass)
Definition proxy.py:31
_DelegateMetaFunction(clsName, bases, atts)
Definition proxy.py:61