forked from michael-donat/tf-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdi.py
29 lines (25 loc) · 996 Bytes
/
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
__author__ = 'thornag'
class ComponentContainer:
def __init__(self):
self.components = {}
def __getitem__(self, component):
try:
componentClass = self.components[component]
except KeyError:
raise KeyError, "Unknown component named %r" % component
return componentClass()
def register(self, component, componentClass, *args, **kwargs):
if callable(componentClass):
def componentInstance(): return componentClass(*args, **kwargs)
else:
def componentInstance(): return componentClass
self.components[component] = componentInstance
container = ComponentContainer()
class ComponentRequest(object):
def __init__(self, component):
self.component = component
def __get__(self, obj, T):
return self.result # <-- will request the feature upon first call
def __getattr__(self, name):
self.result = container[self.component]
return self.result