Skip to content

Commit e66ca67

Browse files
committed
Add get_location to preferences
Commit b027650 (Replace PreferencesManager with module functions) lacked the ability to get the current preferences file location.
1 parent b027650 commit e66ca67

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

tests/test_utility/test_preferences.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def test_setmany_getmany(preferences):
2121
assert preferences.getmany(("spam", "eggs1", None), ("ham", "eggs3", None)) == (42, "antigravity")
2222

2323

24+
def test_get_location(tmp_path, preferences):
25+
path = tmp_path / "preferences2"
26+
preferences.set_location(path)
27+
assert preferences.get_location() == path
28+
29+
2430
def test_set_location(tmp_path, preferences):
2531
preferences.set("spam", "eggs", 42)
2632
preferences.set_location(tmp_path / "preferences2")

volumina/utility/preferences.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ def setmany(self, *args: Tuple[str, str, Any]) -> None:
7070
if not isinstance(e, IOError):
7171
self._path.unlink()
7272

73-
def set_location(self, path: pathlib.Path) -> None:
73+
def get_location(self) -> pathlib.Path:
7474
with self._lock:
75-
self._path = path
76-
self._data = _load_preferences(path)
75+
return self._path
76+
77+
def set_location(self, path: os.PathLike) -> None:
78+
with self._lock:
79+
self._path = pathlib.Path(path)
80+
self._data = _load_preferences(self._path)
7781

7882

7983
def _load_preferences(path: pathlib.Path) -> MutableMapping[str, MutableMapping[str, Any]]:
@@ -134,10 +138,22 @@ def setmany(*args: Tuple[str, str, Any]) -> None:
134138
return _preferences.setmany(*args)
135139

136140

141+
def get_location() -> pathlib.Path:
142+
"""Return the current preferences file location.
143+
144+
See Also:
145+
:func:`set_location`.
146+
"""
147+
return _preferences.get_location()
148+
149+
137150
def set_location(path: os.PathLike) -> None:
138151
"""Change the preferences file location.
139152
140153
Discard all existing preferences and read preferences from the new
141154
file. This file will be used for all subsequent preferences writes.
155+
156+
See Also:
157+
:func:`get_location`.
142158
"""
143-
_preferences.set_location(pathlib.Path(path))
159+
_preferences.set_location(path)

0 commit comments

Comments
 (0)