|
@@ -0,0 +1,109 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import types
|
|
4
|
+import importlib
|
|
5
|
+
|
|
6
|
+## @brief Example implementation of an ACL class
|
|
7
|
+class ACL(object):
|
|
8
|
+ _singleton = None
|
|
9
|
+
|
|
10
|
+ ## @brief dummy singleton contructor
|
|
11
|
+ def __init__(self, **kwargs):
|
|
12
|
+ ACL._singleton = self
|
|
13
|
+
|
|
14
|
+ ## @brief dummy singleton getter
|
|
15
|
+ @classmethod
|
|
16
|
+ def instance(cls):
|
|
17
|
+ if cls._singleton is None:
|
|
18
|
+ cls()
|
|
19
|
+ return cls._singleton
|
|
20
|
+
|
|
21
|
+ ## @brief Callback that checks a call to wrapped classes methods
|
|
22
|
+ @classmethod
|
|
23
|
+ def check_call(cls, call_object, method_name, args, kwargs):
|
|
24
|
+ return ACL.instance()._check_call(call_object, method_name, args, kwargs)
|
|
25
|
+
|
|
26
|
+ ## @brief Callback that checks calls to wrapped classes attributes
|
|
27
|
+ @classmethod
|
|
28
|
+ def check_attr(cls, call_object, attr_name):
|
|
29
|
+ return ACL.instance()._check_attr(call_object, attr_name)
|
|
30
|
+
|
|
31
|
+ ## @brief instance version of check_attr()
|
|
32
|
+ # @note this method is the one that fetch and return the attribute
|
|
33
|
+ def _check_attr(self, call_object, attr_name):
|
|
34
|
+ print("Getting attribute '%s' on %s" % (attr_name, call_object))
|
|
35
|
+ return getattr(call_object, attr_name)
|
|
36
|
+
|
|
37
|
+ ## @brief instance version of check_call()
|
|
38
|
+ # @note this method is the one that call the method and return the returned value
|
|
39
|
+ # @param call_object : the object on wich the method is called (class if class methode else instance)
|
|
40
|
+ # @param method_name str : method name
|
|
41
|
+ # @param *args : positional method arguments
|
|
42
|
+ # @param **kwargs : keyword method arguments
|
|
43
|
+ def _check_call(self, call_object, method_name, args, kwargs):
|
|
44
|
+ print("Calling '%s' on %s with %s %s" % (method_name, call_object, args, kwargs))
|
|
45
|
+ #return call_object.__getattribute__(method_name)(*args, **kwargs)
|
|
46
|
+ if method_name == '__init__':
|
|
47
|
+ return call_object(*args, **kwargs)
|
|
48
|
+ return getattr(call_object, method_name)(*args, **kwargs)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+## @brief A class designed as a wrapper for a method
|
|
52
|
+class AclMethodWrapper(object):
|
|
53
|
+
|
|
54
|
+ ## @brief Constructor
|
|
55
|
+ # @param wrapped_object PythonClass : A python class
|
|
56
|
+ # @param method_name str : the wrapped method name
|
|
57
|
+ def __init__(self, wrapped_object, method_name):
|
|
58
|
+ self._wrapped_object = wrapped_object
|
|
59
|
+ self._method_name = method_name
|
|
60
|
+
|
|
61
|
+ ## @brief Triggered when the wrapped method is called
|
|
62
|
+ def __call__(self, *args, **kwargs):
|
|
63
|
+ return ACL.check_call(self._wrapped_object, self._method_name, args, kwargs)
|
|
64
|
+
|
|
65
|
+## @brief Return a wrapped class
|
|
66
|
+#
|
|
67
|
+# @param module_name str : LeAPI dyn mopdule name
|
|
68
|
+# @param wrapped_class_name str : wrapped class name
|
|
69
|
+def get_wrapper(module_name, wrapped_class_name):
|
|
70
|
+
|
|
71
|
+ module = importlib.import_module(module_name)
|
|
72
|
+ wrapped_class = getattr(module, wrapped_class_name)
|
|
73
|
+
|
|
74
|
+ ## @brief Meta class that handles accesses to wrapped class/static attributes
|
|
75
|
+ class MetaAclWrapper(type):
|
|
76
|
+ ## @brief Called when we get a class attribute
|
|
77
|
+ def __getattribute__(cls, name):
|
|
78
|
+ try:
|
|
79
|
+ attr = getattr(wrapped_class, name)
|
|
80
|
+ if isinstance(attr, types.MethodType):
|
|
81
|
+ return AclMethodWrapper(wrapped_class, name)
|
|
82
|
+ else:
|
|
83
|
+ return ACL.check_attr(wrapped_class, name)
|
|
84
|
+ except Exception as e:
|
|
85
|
+ #Here we can process the exception or log it
|
|
86
|
+ raise e
|
|
87
|
+
|
|
88
|
+ #class AclWrapper(metaclass=MetaAclWrapper):
|
|
89
|
+ class AclWrapper(object, metaclass=MetaAclWrapper):
|
|
90
|
+
|
|
91
|
+ def __init__(self, *args, **kwargs):
|
|
92
|
+ self._wrapped_instance = ACL.check_call(wrapped_class, '__init__', args, kwargs)
|
|
93
|
+ #self._wrapped_instance = wrapped_class(*args, **kwargs)
|
|
94
|
+
|
|
95
|
+ def __getattribute__(self, name):
|
|
96
|
+ try:
|
|
97
|
+ attr = getattr(
|
|
98
|
+ super().__getattribute__('_wrapped_instance'),
|
|
99
|
+ name
|
|
100
|
+ )
|
|
101
|
+ if isinstance(attr, types.MethodType):
|
|
102
|
+ return AclMethodWrapper(super().__getattribute__('_wrapped_instance'), name)
|
|
103
|
+ else:
|
|
104
|
+ return ACL.check_attr(super().__getattribute__('_wrapped_instance'), name)
|
|
105
|
+ except Exception as e:
|
|
106
|
+ #do what you want
|
|
107
|
+ raise e
|
|
108
|
+ return AclWrapper
|
|
109
|
+
|