-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmanager.py
68 lines (56 loc) · 2.1 KB
/
manager.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
import weakref
from collections import ChainMap
from numba.core import types
class DataModelManager(object):
"""Manages mapping of FE types to their corresponding data model
"""
def __init__(self, handlers=None):
"""
Parameters
-----------
handlers: Mapping[Type, DataModel] or None
Optionally provide the initial handlers mapping.
"""
# { numba type class -> model factory }
self._handlers = handlers or {}
# { numba type instance -> model instance }
self._cache = weakref.WeakKeyDictionary()
def register(self, fetypecls, handler):
"""Register the datamodel factory corresponding to a frontend-type class
"""
assert issubclass(fetypecls, types.Type)
self._handlers[fetypecls] = handler
def lookup(self, fetype):
"""Returns the corresponding datamodel given the frontend-type instance
"""
try:
return self._cache[fetype]
except KeyError:
pass
handler = self._handlers[type(fetype)]
model = self._cache[fetype] = handler(self, fetype)
return model
def __getitem__(self, fetype):
"""Shorthand for lookup()
"""
return self.lookup(fetype)
def copy(self):
"""
Make a copy of the manager.
Use this to inherit from the default data model and specialize it
for custom target.
"""
return DataModelManager(self._handlers.copy())
def chain(self, other_manager):
"""Create a new DataModelManager by chaining the handlers mapping of
`other_manager` with a fresh handlers mapping.
Any existing and new handlers inserted to `other_manager` will be
visible to the new manager. Any handlers inserted to the new manager
can override existing handlers in `other_manager` without actually
mutating `other_manager`.
Parameters
----------
other_manager: DataModelManager
"""
chained = ChainMap(self._handlers, other_manager._handlers)
return DataModelManager(chained)