summaryrefslogtreecommitdiff
path: root/jstate
diff options
context:
space:
mode:
Diffstat (limited to 'jstate')
-rw-r--r--jstate/__init__.py1
-rw-r--r--jstate/jstate.py21
2 files changed, 22 insertions, 0 deletions
diff --git a/jstate/__init__.py b/jstate/__init__.py
new file mode 100644
index 0000000..8254a30
--- /dev/null
+++ b/jstate/__init__.py
@@ -0,0 +1 @@
+from .jstate import JState \ No newline at end of file
diff --git a/jstate/jstate.py b/jstate/jstate.py
new file mode 100644
index 0000000..799f924
--- /dev/null
+++ b/jstate/jstate.py
@@ -0,0 +1,21 @@
+import os, json
+
+
+class JState:
+ def __init__(self, file: str, default=None):
+ 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