def make_inst_proxy(cls, exposed=None, method_to_typeid=None): ''' Return a custom proxy for wrapping access to a shared instance Parameters ---------- cls : type Class for which a proxy object should be created exposed : list Sequence of methods which should be made public via the proxy object. If not provided public methods are automatically retrieved from the class' declared interface Returns ------- proxy : a subclass of mp.managers.BaseProxy with a getattr/setattr interface (see mp.managers.py for details) ''' if exposed is None: try: exposed = cls._exposed except AttributeError: exposed = managers.public_methods(cls) # auto-attach listed methods ProxyBase = managers.MakeProxyType('ProxyBase', exposed) # make mutable to extend exposed = list(ProxyBase._exposed_) class InstProxy(ProxyBase): _exposed_ = tuple(exposed + ['__getattribute__', '__setattr__', '__dir__']) _attr_redirect = {} __repr__ = _repr __dir__ = _proxy_dir def __getattr__(self, key): try: return object.__getattribute__(self, key) except AttributeError: callmethod = object.__getattribute__(self, '_callmethod') # handle attr redirects declared by this proxy if key in self._attr_redirect: method = self._attr_redirect[key] return callmethod(method) else: method = '__getattribute__' return callmethod(method, (key,)) def __setattr__(self, key, value): if key[0] == '_': # this is critical do not change return object.__setattr__(self, key, value) else: callmethod = object.__getattribute__(self, '_callmethod') return callmethod('__setattr__', (key, value)) # mark shared 'sub-proxy' attributes if method_to_typeid: InstProxy._method_to_typeid_.update(method_to_typeid) return InstProxy
class QueueProxy(BaseProxy): _exposed_ = public_methods(queue.Queue)
def make_inst_proxy(cls, exposed=None, method_to_typeid=None): ''' Return a custom proxy for wrapping access to a shared instance Parameters ---------- cls : type Class for which a proxy object should be created exposed : list Sequence of methods which should be made public via the proxy object. If not provided public methods are automatically retrieved from the class' declared interface Returns ------- proxy : a subclass of mp.managers.BaseProxy with a getattr/setattr interface (see mp.managers.py for details) ''' if exposed is None: try: exposed = cls._exposed except AttributeError: exposed = managers.public_methods(cls) # auto-attach listed methods ProxyBase = managers.MakeProxyType('ProxyBase', exposed) # make mutable to extend exposed = list(ProxyBase._exposed_) class InstProxy(ProxyBase): _exposed_ = tuple(exposed + ['__getattribute__', '__setattr__', '__dir__']) _attr_redirect = {} __repr__ = _repr __dir__ = _proxy_dir def __getattr__(self, key): try: return object.__getattribute__(self, key) except AttributeError: callmethod = object.__getattribute__(self, '_callmethod') # handle attr redirects declared by this proxy if key in self._attr_redirect: method = self._attr_redirect[key] return callmethod(method) else: method = '__getattribute__' return callmethod(method, (key, )) def __setattr__(self, key, value): if key[0] == '_': # this is critical do not change return object.__setattr__(self, key, value) else: callmethod = object.__getattribute__(self, '_callmethod') return callmethod('__setattr__', (key, value)) # mark shared 'sub-proxy' attributes if method_to_typeid: InstProxy._method_to_typeid_.update(method_to_typeid) return InstProxy