summaryrefslogtreecommitdiff
path: root/jstate/jstate.py
blob: 799f92488e040b816771da7431284854d5fdcb0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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))