-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
179 lines (143 loc) · 4.99 KB
/
__init__.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
"""Store package.
Contains all packages and modules dealing with user metadata.
.. packageauthor:: Dave Zimmelman <zimmed@zimmed.io>
Exports:
"""
from .db import db, DBLookupError
from core.datamodel import DataModelController
class ControllerCollection(object):
MAX_CACHE = 100
def __init__(self, store, max_cache=None):
if not max_cache:
max_cache = self.__class__.MAX_CACHE
self._max_cache = max_cache
self._controllers = []
self._ctrl_map = {}
self._store = store
def prune(self):
while len(self._controllers) > self._max_cache:
c = self._controllers.pop()
print 'Pruning: ' + repr(c)
del self._ctrl_map[c.uid]
self._store.save(c.__class__, c.model)
def shift_down(self, index):
for k in self._ctrl_map.iterkeys():
if self._ctrl_map[k] > index:
self._ctrl_map[k] -= 1
def shift_up(self):
for k in self._ctrl_map.iterkeys():
self._ctrl_map[k] += 1
def insert(self, ctrl):
if ctrl.uid in self._ctrl_map.iterkeys():
return
self._controllers.insert(0, ctrl)
self.shift_up()
self._ctrl_map[ctrl.uid] = 0
self.prune()
def get(self, uid):
index = self._ctrl_map[uid]
return self._controllers[index]
def remove(self, uid):
index = self._ctrl_map[uid]
del self._controllers[index]
del self._ctrl_map[uid]
self.shift_down(index)
class DataStore(object):
_CACHE = {}
_db = None
@classmethod
# __new__ is automatically setting both first and second args to class.
# no idea why.
def __new__(cls, _, db_host='localhost',
db_port=27017,
db_name='zimmed-test1'):
if not cls._db:
cls._db = db(db_host, db_port, db_name)
return cls
def __init__(self, *args, **kwargs):
pass
@classmethod
def _key(cls, doc_type):
if type(doc_type) is str:
return doc_type
elif type(doc_type) is type:
return doc_type.__name__
else:
raise ValueError('Invalid doc_type supplied to ModelStore.')
@classmethod
def uid(cls, doc_type):
doc_type = cls._key(doc_type)
return cls._db.generate_uid(doc_type)
@classmethod
def get_controller(cls, doc_type, uid):
if not issubclass(doc_type, DataModelController):
raise TypeError("`doc_type` must be a sub class of "
"DataModelController.")
key = cls._key(doc_type)
ctrl = cls.get_strict_controller(key, uid)
if not ctrl:
model = cls.get_strict_model(key, uid)
if not model:
raise DBLookupError("Object does not exist: " + key + ' -- ' + uid)
ctrl = doc_type.restore(cls, model)
return ctrl
@classmethod
def get_model(cls, doc_type, uid):
doc_type = cls._key(doc_type)
ctrl = cls.get_strict_controller(doc_type, uid)
if ctrl:
return ctrl.model
return cls.get_strict_model(doc_type, uid)
@classmethod
def get_model_data(cls, doc_type, uid):
doc_type = cls._key(doc_type)
ctrl = cls.get_strict_controller(doc_type, uid)
if ctrl:
return dict(ctrl.model)
return cls._db.get_model_data(doc_type, uid)
@classmethod
def get_strict_controller(cls, doc_type, uid):
doc_type = cls._key(doc_type)
try:
return cls._CACHE[doc_type].get(uid)
except KeyError:
return None
@classmethod
def set_controller(cls, doc_type, ctrl):
doc_type = cls._key(doc_type)
if doc_type not in cls._CACHE:
cls._CACHE[doc_type] = ControllerCollection(cls)
return cls._CACHE[doc_type].insert(ctrl)
@classmethod
def delete_controller(cls, doc_type, uid):
doc_type = cls._key(doc_type)
try:
cls._CACHE[doc_type].remove(uid)
return True
except KeyError:
return False
@classmethod
def save(cls, doc_type, model):
doc_type = cls._key(doc_type)
cls._db.upsert_model(doc_type, model)
@classmethod
def store_model(cls, doc_type, model):
doc_type = cls._key(doc_type)
cls._db.insert_model(doc_type, model)
@classmethod
def update_model(cls, doc_type, model):
doc_type = cls._key(doc_type)
cls._db.update_model(doc_type, model)
@classmethod
def delete_model(cls, doc_type, uid):
doc_type = cls._key(doc_type)
cls._db.remove_model(doc_type, uid)
@classmethod
def get_strict_model(cls, doc_type, uid):
doc_type = cls._key(doc_type)
return cls._db.get_model(doc_type, uid)
# ----------------------------------------------------------------------------
__version__ = 0.2
__license__ = "MIT"
__credits__ = ["zimmed"]
# ----------------------------------------------------------------------------