-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
29 lines (23 loc) · 866 Bytes
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
from types import MethodType
class Proxy:
def __init__(self, target, handler):
self.target = target
self.handler = handler
def __call__(self, func):
def invoke(*args, **kwargs):
return self.handler(self.target, func, *args, **kwargs)
return invoke
def __getattr__(self, attr):
if hasattr(self.target, attr):
prop = getattr(self.target, attr)
return self.__call__(prop) if isinstance(prop, MethodType) else prop
else:
raise AttributeError("'{}' object has no attribute '{}'".format(self.target.__class__, attr))
def ProxyFactory(handler):
def init(cls):
def create_proxy(*args, **kwargs):
target = cls(*args, **kwargs)
return Proxy(target, handler)
return create_proxy
return init