diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-04-20 00:19:27 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-04-20 00:21:17 +0300 |
commit | 2822361b3bccc854548da3213fe8afb622d11a8a (patch) | |
tree | e9940162a69d0f77f0f6e36e9e69bdb646ce63a4 /jstate |
Diffstat (limited to 'jstate')
-rw-r--r-- | jstate/__init__.py | 1 | ||||
-rw-r--r-- | jstate/jstate.py | 21 |
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 |