| 23 | self.raise_dummy_exception() |
| 24 | |
| 25 | class JSONStorageBackend(BasicStorageBackend): |
| 26 | def __init__(self) -> None: |
| 27 | self.refresh() |
| 28 | |
| 29 | def refresh(self): |
| 30 | self.json_path = relative_path("local_storage.json") |
| 31 | self.json_data = {} |
| 32 | |
| 33 | if not os.path.isfile(self.json_path): |
| 34 | self.commit_to_disk() |
| 35 | |
| 36 | with open(self.json_path, "r") as json_file: |
| 37 | self.json_data = json.load(json_file) |
| 38 | |
| 39 | def commit_to_disk(self): |
| 40 | with open(self.json_path, "w") as json_file: |
| 41 | json.dump(self.json_data, json_file, indent=4) |
| 42 | |
| 43 | def get_item(self, key: str, default = None) -> str: |
| 44 | if key in self.json_data: |
| 45 | return self.json_data[key] |
| 46 | return default |
| 47 | |
| 48 | |
| 49 | def items(self): |
| 50 | return self.json_data |
| 51 | |
| 52 | def set_item(self, key: str, value: any) -> None: |
| 53 | self.json_data[key] = value |
| 54 | self.commit_to_disk() |
| 55 | |
| 56 | def remove_item(self, key: str) -> None: |
| 57 | if key in self.json_data: |
| 58 | self.json_data.pop(key) |
| 59 | self.commit_to_disk() |
| 60 | |
| 61 | |
| 62 | # def get_new_number(self): |
| 63 | # seen = self.get_item('seen', []) |
| 64 | |
| 65 | # if len(seen) == 0: |
| 66 | # max_seen = 0 |
| 67 | # else: |
| 68 | # max_seen = max(seen) |
| 69 | |
| 70 | # new = max_seen + 1 |
| 71 | # self.set_item('seen', seen + [new]) |
| 72 | # return new |
| 73 | |
| 74 | def clear(self) -> None: |
| 75 | if os.path.isfile(self.json_path): |
| 76 | os.remove(self.json_path) |
| 77 | self.json_data = {} |
| 78 | self.commit_to_disk() |
| 79 | |
| 80 | class _LocalStorage: |
| 81 | def __init__(self) -> None: |