-
Notifications
You must be signed in to change notification settings - Fork 0
/
di.py
executable file
·207 lines (147 loc) · 3.97 KB
/
di.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python3
def clear():
global rootContext
rootContext = Context()
def implements(feature, path):
global rootContext
handler = rootContext.subcontexts(path)
handler[0].implements(feature, "")
def inject(path):
"""
This function walks through the context tree to
the given path. As it found the certain node, it ask's
the context to provide the demanded feature.
"""
global rootContext
path = Path(path)
handler = rootContext.subcontexts(path)
i = 0
# Walk trough the hierarchy (backwards)
for h in handler:
try:
return h.provide(path.last(i))
except NotImplemented:
pass
i += 1
raise NotImplemented(str(path))
def context(path):
"""
A function to access a Context under a given path.
"""
global rootContext
path = Path(path)
return rootContext.subcontexts(path)[0]
class Context(object):
def __init__(self, parent=None, scope=""):
self.parent = parent
self.provider = None
self.children = {}
self.scope = Path(scope)
def inject(self, path="."):
"""
The function walks from the rootContext
trough the tree, because if there is no Context that
can provide the feature below in the tree, the Contexts
above these might have an Provider for the feature.
"""
return inject(self.scope.append(path))
def implements(self, feature, path="."):
path = Path(path)
if not path.match("."):
handler = self.subcontexts(path)
handler[0].implements(feature, "")
return
if issubclass(feature, Service):
self.provider = ServiceProvider(feature)
if issubclass(feature, Controller):
self.provider = ControllerProvider(feature)
if issubclass(feature, Provider):
self.provider = feature()
def provide(self, path):
"""
Should not be called from outside the library. This
function asks the provider (if there's one) for an
implementation of the feature under the relative path
specified by the variable named so.
"""
if self.provider is None:
raise NotImplemented
else:
return self.provider.provide(path)
def subcontexts(self, path):
"""
Returns a list including the the hierarchy specified
in the path, beginning with the full path itself. The
parent of the path is the second element, following up
to this Context.
"""
path = Path(path)
if path.empty():
return [self]
subdir = path.first()
if subdir not in self.children.keys():
self.children[subdir] = Context(self, self.scope.append(subdir))
element = self.children[subdir]
return element.subcontexts(path.sub()) + [self]
class Path(object):
def __init__(self, raw):
if isinstance(raw, Path):
self.data = raw.data
elif isinstance(raw, list):
self.data = raw
else:
self.data = raw.split("/")
if "." in self.data:
self.data.remove(".")
if "" in self.data:
self.data.remove("")
def __str__(self):
return "/".join(self.data)
def match(self, other):
other = Path(other)
return str(self) == str(other)
def first(self):
return self.data[0]
def last(self, n=0):
if n == 0:
return Path("")
return Path(self.data[-n:])
def sub(self):
return Path(self.data[1:])
def empty(self):
return len(self.data) == 0
def append(self, other):
other = Path(other)
return Path(self.data + other.data)
class Feature(object):
pass
class Service(Feature):
pass
class Controller(Feature):
pass
class Provider(object):
def provide(self):
raise NotImplemented
class ServiceProvider(Provider):
def __init__(self, service):
self.instance = None
self.service = service
def provide(self, path):
if not path.match("."):
raise NotImplemented
if self.instance is None:
self.instance = self.service()
return self.instance
class ControllerProvider(Provider):
def __init__(self, controller):
self.controller = controller
def provide(self, path):
if not path.match("."):
raise NotImplemented
return self.controller()
# Exceptions
class NotImplemented(RuntimeError):
def __init__(self, featureName=""):
self.featureName = featureName
# Initialize a new rootContext
clear()