| 30 | self.raise_dummy_exception() |
| 31 | |
| 32 | class JSONStorageBackend(BasicStorageBackend): |
| 33 | def __init__(self) -> None: |
| 34 | self.refresh() |
| 35 | |
| 36 | def refresh(self): |
| 37 | self.json_path = get_cache_file_path() |
| 38 | self.json_data = {} |
| 39 | |
| 40 | if not os.path.isfile(self.json_path): |
| 41 | self.commit_to_disk() |
| 42 | |
| 43 | with open(self.json_path, "r") as json_file: |
| 44 | self.json_data = json.load(json_file) |
| 45 | |
| 46 | def commit_to_disk(self): |
| 47 | with open(self.json_path, "w") as json_file: |
| 48 | json.dump(self.json_data, json_file, indent=4) |
| 49 | |
| 50 | def get_item(self, key: str, default = None) -> str: |
| 51 | if key in self.json_data: |
| 52 | return self.json_data[key] |
| 53 | return default |
| 54 | |
| 55 | |
| 56 | def items(self): |
| 57 | return self.json_data |
| 58 | |
| 59 | def set_item(self, key: str, value: any) -> None: |
| 60 | self.json_data[key] = value |
| 61 | self.commit_to_disk() |
| 62 | |
| 63 | def remove_item(self, key: str) -> None: |
| 64 | if key in self.json_data: |
| 65 | self.json_data.pop(key) |
| 66 | self.commit_to_disk() |
| 67 | |
| 68 | |
| 69 | def clear(self) -> None: |
| 70 | if os.path.isfile(self.json_path): |
| 71 | os.remove(self.json_path) |
| 72 | self.json_data = {} |
| 73 | self.commit_to_disk() |
| 74 | |
| 75 | class _LocalStorage: |
| 76 | def __init__(self) -> None: |