Skip to content

Commit 7db9e4d

Browse files
committed
fix: check directory existence when loading files (close #227)
1 parent e8c3b65 commit 7db9e4d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

dcscope/gui/settings.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pathlib
2+
from PyQt6 import QtCore
3+
4+
5+
def get_dir(topic: str,
6+
settings: QtCore.QSettings
7+
) -> str:
8+
"""Given a QSettings instance, return a directory
9+
10+
If writable is `True`, return a writable directory
11+
"""
12+
location = settings.value(f"paths/{topic}", "")
13+
14+
if not location:
15+
# use default value
16+
location = QtCore.QStandardPaths.standardLocations(
17+
QtCore.QStandardPaths.StandardLocation.HomeLocation)[0]
18+
else:
19+
# check whether the location exists
20+
path = pathlib.Path(location)
21+
if not path.is_dir():
22+
for pp in path.parents:
23+
if pp.is_dir():
24+
location = str(pp)
25+
break
26+
else:
27+
location = "."
28+
29+
return location or "."
30+
31+
32+
def set_dir(topic: str,
33+
path: pathlib.Path | str,
34+
settings: QtCore.QSettings,
35+
):
36+
"""Given a QSettings instance, save a directory topic"""
37+
path = pathlib.Path(path)
38+
if path.exists():
39+
if not path.is_dir():
40+
path = path.parent
41+
settings.setValue(f"paths/{topic}", str(path))

0 commit comments

Comments
 (0)