summaryrefslogtreecommitdiff
path: root/ick2/persistent.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2019-10-18 10:49:25 +0300
committerLars Wirzenius <liw@liw.fi>2019-10-18 10:49:25 +0300
commit67de4c99b46c5f77950393a0f5b88a400949c331 (patch)
tree0fcaa29580cc4cdbb69950e5a184a59360f7f7d4 /ick2/persistent.py
parent5822e79a9355970a1190194e351a4761e9db6859 (diff)
downloadick2-67de4c99b46c5f77950393a0f5b88a400949c331.tar.gz
Revert "Drop: use of old persistent state classes"
This reverts commit 8344a860d226d8a5172bcb2e9de5946717a950ca.
Diffstat (limited to 'ick2/persistent.py')
-rw-r--r--ick2/persistent.py61
1 files changed, 60 insertions, 1 deletions
diff --git a/ick2/persistent.py b/ick2/persistent.py
index 33903fe..e225b88 100644
--- a/ick2/persistent.py
+++ b/ick2/persistent.py
@@ -25,7 +25,66 @@ import yaml
import ick2
-class NotFound(Exception): # pragma: no cover
+class PersistentStateInterface: # pragma: no cover
+
+ def get_resource_names(self, token, kind):
+ raise NotImplementedError()
+
+ def has_resource(self, token, kind, name):
+ raise NotImplementedError()
+
+ def get_resource(self, token, kind, name):
+ raise NotImplementedError()
+
+ def get_resources(self, token, kind):
+ return [
+ self.get_resource(token, kind, name)
+ for name in self.get_resource_names(token, kind)
+ ]
+
+ def write_resource(self, token, kind, name, resource):
+ raise NotImplementedError()
+
+ def update_resource(self, token, kind, name, resource):
+ raise NotImplementedError()
+
+ def remove_resource(self, token, kind, name):
+ raise NotImplementedError()
+
+
+class MemoryPersistentState(PersistentStateInterface):
+
+ def __init__(self):
+ self._res = {}
+
+ def get_resource_names(self, token, kind):
+ if kind not in self._res:
+ return []
+ return list(self._res[kind].keys())
+
+ def has_resource(self, token, kind, name):
+ return kind in self._res and name in self._res[kind]
+
+ def get_resource(self, token, kind, name):
+ if kind not in self._res or name not in self._res[kind]:
+ raise ick2.NotFound(kind=kind, name=name)
+ return self._res[kind][name]
+
+ def write_resource(self, token, kind, name, resource):
+ if kind not in self._res:
+ self._res[kind] = {}
+ self._res[kind][name] = resource
+
+ def update_resource(self, token, kind, name, resource):
+ self.write_resource(token, kind, name, resource)
+
+ def remove_resource(self, token, kind, name):
+ if kind not in self._res or name not in self._res[kind]:
+ raise ick2.NotFound(kind=kind, name=name)
+ del self._res[kind][name]
+
+
+class NotFound(Exception):
def __init__(self, kind, name):
super().__init__(