File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments