summaryrefslogtreecommitdiff
path: root/ick2/persistent.py
diff options
context:
space:
mode:
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__(