From 2822361b3bccc854548da3213fe8afb622d11a8a Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Tue, 20 Apr 2021 00:19:27 +0300 Subject: initial --- jstate/__init__.py | 1 + jstate/jstate.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 jstate/__init__.py create mode 100644 jstate/jstate.py (limited to 'jstate') 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 -- cgit v1.2.3