Skip to content

Commit 077bc34

Browse files
committed
fix: handle read only file system (f1c173b)
1 parent d763bc2 commit 077bc34

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

glv/providers.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@
1717

1818
class Cache:
1919
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)
2220
self._storage: dict = {}
21+
cache_dir = os.path.dirname(file_path)
2322
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:
2729
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)
3235

3336
def __getitem__(self, key) -> Any:
3437
return self._storage[key]
3538

3639
def __setitem__(self, key, value):
3740
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)
4044

4145

4246
class Provider():

0 commit comments

Comments
 (0)