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/jstate.py |
Diffstat (limited to 'jstate/jstate.py')
-rw-r--r-- | jstate/jstate.py | 21 |
1 files changed, 21 insertions, 0 deletions
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 |