|
17 | 17 |
|
18 | 18 | class Cache: |
19 | 19 | def __init__(self, file_path: str) -> None: |
20 | | - cache_dir = os.path.dirname(file_path) |
21 | | - pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True) |
22 | 20 | self._storage: dict = {} |
| 21 | + cache_dir = os.path.dirname(file_path) |
23 | 22 | self._cache_file = file_path |
24 | | - if os.path.isfile(file_path): |
25 | | - with open(file_path, encoding='utf-8') as data_file: |
26 | | - try: |
| 23 | + self._ro_backend = False |
| 24 | + |
| 25 | + try: |
| 26 | + pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True) |
| 27 | + if os.path.isfile(file_path): # restore cache |
| 28 | + with open(file_path, encoding='utf-8') as data_file: |
27 | 29 | self._storage = json.loads(data_file.read()) |
28 | | - except json.decoder.JSONDecodeError as exc: |
29 | | - LOG.warning('Failed to parse %s: %s', data_file.name, |
30 | | - exc.msg) |
31 | | - self._storage = {} |
| 30 | + except PermissionError: |
| 31 | + LOG.warning('Read only git-dir, no data will be cached') |
| 32 | + self._ro_backend = True |
| 33 | + except json.decoder.JSONDecodeError as exc: |
| 34 | + LOG.warning('Failed to parse %s: %s', data_file.name, exc.msg) |
32 | 35 |
|
33 | 36 | def __getitem__(self, key) -> Any: |
34 | 37 | return self._storage[key] |
35 | 38 |
|
36 | 39 | def __setitem__(self, key, value): |
37 | 40 | self._storage[key] = value |
38 | | - with open(self._cache_file, 'w') as outfile: |
39 | | - json.dump(self._storage, outfile) |
| 41 | + if not self._ro_backend: |
| 42 | + with open(self._cache_file, 'w') as outfile: |
| 43 | + json.dump(self._storage, outfile) |
40 | 44 |
|
41 | 45 |
|
42 | 46 | class Provider(): |
|
0 commit comments