Mercurial > libervia-backend
comparison sat/memory/persistent.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | cbb988a6f507 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
49 raise MemoryNotInitializedError | 49 raise MemoryNotInitializedError |
50 self._cache = {} | 50 self._cache = {} |
51 self.namespace = namespace | 51 self.namespace = namespace |
52 self.profile = profile | 52 self.profile = profile |
53 | 53 |
54 def _setCache(self, data): | 54 def _set_cache(self, data): |
55 self._cache = data | 55 self._cache = data |
56 | 56 |
57 def load(self): | 57 def load(self): |
58 """Load persistent data from storage. | 58 """Load persistent data from storage. |
59 | 59 |
60 need to be called before any other operation | 60 need to be called before any other operation |
61 @return: defers the PersistentDict instance itself | 61 @return: defers the PersistentDict instance itself |
62 """ | 62 """ |
63 d = defer.ensureDeferred(self.storage.getPrivates( | 63 d = defer.ensureDeferred(self.storage.get_privates( |
64 self.namespace, binary=self.binary, profile=self.profile | 64 self.namespace, binary=self.binary, profile=self.profile |
65 )) | 65 )) |
66 d.addCallback(self._setCache) | 66 d.addCallback(self._set_cache) |
67 d.addCallback(lambda __: self) | 67 d.addCallback(lambda __: self) |
68 return d | 68 return d |
69 | 69 |
70 def iteritems(self): | 70 def iteritems(self): |
71 return iter(self._cache.items()) | 71 return iter(self._cache.items()) |
115 def __getitem__(self, key): | 115 def __getitem__(self, key): |
116 return self._cache.__getitem__(key) | 116 return self._cache.__getitem__(key) |
117 | 117 |
118 def __setitem__(self, key, value): | 118 def __setitem__(self, key, value): |
119 defer.ensureDeferred( | 119 defer.ensureDeferred( |
120 self.storage.setPrivateValue( | 120 self.storage.set_private_value( |
121 self.namespace, key, value, self.binary, self.profile | 121 self.namespace, key, value, self.binary, self.profile |
122 ) | 122 ) |
123 ) | 123 ) |
124 return self._cache.__setitem__(key, value) | 124 return self._cache.__setitem__(key, value) |
125 | 125 |
126 def __delitem__(self, key): | 126 def __delitem__(self, key): |
127 self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) | 127 self.storage.del_private_value(self.namespace, key, self.binary, self.profile) |
128 return self._cache.__delitem__(key) | 128 return self._cache.__delitem__(key) |
129 | 129 |
130 def clear(self): | 130 def clear(self): |
131 """Delete all values from this namespace""" | 131 """Delete all values from this namespace""" |
132 self._cache.clear() | 132 self._cache.clear() |
133 return self.storage.delPrivateNamespace(self.namespace, self.binary, self.profile) | 133 return self.storage.del_private_namespace(self.namespace, self.binary, self.profile) |
134 | 134 |
135 def get(self, key, default=None): | 135 def get(self, key, default=None): |
136 return self._cache.get(key, default) | 136 return self._cache.get(key, default) |
137 | 137 |
138 def aset(self, key, value): | 138 def aset(self, key, value): |
139 """Async set, return a Deferred fired when value is actually stored""" | 139 """Async set, return a Deferred fired when value is actually stored""" |
140 self._cache.__setitem__(key, value) | 140 self._cache.__setitem__(key, value) |
141 return defer.ensureDeferred( | 141 return defer.ensureDeferred( |
142 self.storage.setPrivateValue( | 142 self.storage.set_private_value( |
143 self.namespace, key, value, self.binary, self.profile | 143 self.namespace, key, value, self.binary, self.profile |
144 ) | 144 ) |
145 ) | 145 ) |
146 | 146 |
147 def adel(self, key): | 147 def adel(self, key): |
148 """Async del, return a Deferred fired when value is actually deleted""" | 148 """Async del, return a Deferred fired when value is actually deleted""" |
149 self._cache.__delitem__(key) | 149 self._cache.__delitem__(key) |
150 return self.storage.delPrivateValue( | 150 return self.storage.del_private_value( |
151 self.namespace, key, self.binary, self.profile) | 151 self.namespace, key, self.binary, self.profile) |
152 | 152 |
153 def setdefault(self, key, default): | 153 def setdefault(self, key, default): |
154 try: | 154 try: |
155 return self._cache[key] | 155 return self._cache[key] |
161 """Force saving of an attribute to storage | 161 """Force saving of an attribute to storage |
162 | 162 |
163 @return: deferred fired when data is actually saved | 163 @return: deferred fired when data is actually saved |
164 """ | 164 """ |
165 return defer.ensureDeferred( | 165 return defer.ensureDeferred( |
166 self.storage.setPrivateValue( | 166 self.storage.set_private_value( |
167 self.namespace, name, self._cache[name], self.binary, self.profile | 167 self.namespace, name, self._cache[name], self.binary, self.profile |
168 ) | 168 ) |
169 ) | 169 ) |
170 | 170 |
171 | 171 |
190 | 190 |
191 def iteritems(self): | 191 def iteritems(self): |
192 raise NotImplementedError | 192 raise NotImplementedError |
193 | 193 |
194 def items(self): | 194 def items(self): |
195 d = defer.ensureDeferred(self.storage.getPrivates( | 195 d = defer.ensureDeferred(self.storage.get_privates( |
196 self.namespace, binary=self.binary, profile=self.profile | 196 self.namespace, binary=self.binary, profile=self.profile |
197 )) | 197 )) |
198 d.addCallback(lambda data_dict: data_dict.items()) | 198 d.addCallback(lambda data_dict: data_dict.items()) |
199 return d | 199 return d |
200 | 200 |
201 def all(self): | 201 def all(self): |
202 return defer.ensureDeferred(self.storage.getPrivates( | 202 return defer.ensureDeferred(self.storage.get_privates( |
203 self.namespace, binary=self.binary, profile=self.profile | 203 self.namespace, binary=self.binary, profile=self.profile |
204 )) | 204 )) |
205 | 205 |
206 def __repr__(self): | 206 def __repr__(self): |
207 return self.__str__() | 207 return self.__str__() |
250 # into debugger in debug mode. | 250 # into debugger in debug mode. |
251 raise failure.Failure(e) | 251 raise failure.Failure(e) |
252 | 252 |
253 def __getitem__(self, key): | 253 def __getitem__(self, key): |
254 """get the value as a Deferred""" | 254 """get the value as a Deferred""" |
255 d = defer.ensureDeferred(self.storage.getPrivates( | 255 d = defer.ensureDeferred(self.storage.get_privates( |
256 self.namespace, keys=[key], binary=self.binary, profile=self.profile | 256 self.namespace, keys=[key], binary=self.binary, profile=self.profile |
257 )) | 257 )) |
258 d.addCallback(self._data2value, key) | 258 d.addCallback(self._data2value, key) |
259 return d | 259 return d |
260 | 260 |
261 def __setitem__(self, key, value): | 261 def __setitem__(self, key, value): |
262 defer.ensureDeferred( | 262 defer.ensureDeferred( |
263 self.storage.setPrivateValue( | 263 self.storage.set_private_value( |
264 self.namespace, key, value, self.binary, self.profile | 264 self.namespace, key, value, self.binary, self.profile |
265 ) | 265 ) |
266 ) | 266 ) |
267 | 267 |
268 def __delitem__(self, key): | 268 def __delitem__(self, key): |
269 self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) | 269 self.storage.del_private_value(self.namespace, key, self.binary, self.profile) |
270 | 270 |
271 def _defaultOrException(self, failure_, default): | 271 def _default_or_exception(self, failure_, default): |
272 failure_.trap(KeyError) | 272 failure_.trap(KeyError) |
273 return default | 273 return default |
274 | 274 |
275 def get(self, key, default=None): | 275 def get(self, key, default=None): |
276 d = self.__getitem__(key) | 276 d = self.__getitem__(key) |
277 d.addErrback(self._defaultOrException, default=default) | 277 d.addErrback(self._default_or_exception, default=default) |
278 return d | 278 return d |
279 | 279 |
280 def aset(self, key, value): | 280 def aset(self, key, value): |
281 """Async set, return a Deferred fired when value is actually stored""" | 281 """Async set, return a Deferred fired when value is actually stored""" |
282 # FIXME: redundant with force, force must be removed | 282 # FIXME: redundant with force, force must be removed |
283 # XXX: similar as PersistentDict.aset, but doesn't use cache | 283 # XXX: similar as PersistentDict.aset, but doesn't use cache |
284 return defer.ensureDeferred( | 284 return defer.ensureDeferred( |
285 self.storage.setPrivateValue( | 285 self.storage.set_private_value( |
286 self.namespace, key, value, self.binary, self.profile | 286 self.namespace, key, value, self.binary, self.profile |
287 ) | 287 ) |
288 ) | 288 ) |
289 | 289 |
290 def adel(self, key): | 290 def adel(self, key): |
291 """Async del, return a Deferred fired when value is actually deleted""" | 291 """Async del, return a Deferred fired when value is actually deleted""" |
292 # XXX: similar as PersistentDict.adel, but doesn't use cache | 292 # XXX: similar as PersistentDict.adel, but doesn't use cache |
293 return self.storage.delPrivateValue( | 293 return self.storage.del_private_value( |
294 self.namespace, key, self.binary, self.profile) | 294 self.namespace, key, self.binary, self.profile) |
295 | 295 |
296 def setdefault(self, key, default): | 296 def setdefault(self, key, default): |
297 raise NotImplementedError | 297 raise NotImplementedError |
298 | 298 |
301 | 301 |
302 @param value(object): value is needed for LazyPersistentBinaryDict | 302 @param value(object): value is needed for LazyPersistentBinaryDict |
303 @return: deferred fired when data is actually saved | 303 @return: deferred fired when data is actually saved |
304 """ | 304 """ |
305 return defer.ensureDeferred( | 305 return defer.ensureDeferred( |
306 self.storage.setPrivateValue( | 306 self.storage.set_private_value( |
307 self.namespace, name, value, self.binary, self.profile | 307 self.namespace, name, value, self.binary, self.profile |
308 ) | 308 ) |
309 ) | 309 ) |
310 | 310 |
311 def remove(self, key): | 311 def remove(self, key): |
312 """Delete a key from sotrage, and return a deferred called when it's done | 312 """Delete a key from sotrage, and return a deferred called when it's done |
313 | 313 |
314 @param key(unicode): key to delete | 314 @param key(unicode): key to delete |
315 @return (D): A deferred fired when delete is done | 315 @return (D): A deferred fired when delete is done |
316 """ | 316 """ |
317 return self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) | 317 return self.storage.del_private_value(self.namespace, key, self.binary, self.profile) |