aboutsummaryrefslogtreecommitdiff
path: root/ch1p/state.py
diff options
context:
space:
mode:
Diffstat (limited to 'ch1p/state.py')
-rw-r--r--ch1p/state.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/ch1p/state.py b/ch1p/state.py
new file mode 100644
index 0000000..f677f59
--- /dev/null
+++ b/ch1p/state.py
@@ -0,0 +1,28 @@
+import os, json
+
+from functions import _get_vars
+
+
+class State:
+ def __init__(self, default=None, **kwargs):
+ file, = _get_vars([
+ ('file', 'STATE_FILE')
+ ], kwargs)
+
+ if default is None:
+ default = {}
+
+ self.file = file
+ self.default = default
+
+ def read(self) -> dict:
+ if not os.path.exists(self.file):
+ self.write(self.default)
+ return self.default
+
+ with open(self.file, 'r') as f:
+ return json.loads(f.read())
+
+ def write(self, state: dict):
+ with open(self.file, 'w') as f:
+ f.write(json.dumps(state)) \ No newline at end of file