diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-04-25 23:02:49 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-04-25 23:02:49 +0300 |
commit | 7d4f8da59d60156c2d45dc4a44891eea48082315 (patch) | |
tree | 81f46df8a796d7e1b85e4aac97926350fbdbf466 /ch1p/state.py |
initial
Diffstat (limited to 'ch1p/state.py')
-rw-r--r-- | ch1p/state.py | 28 |
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 |