Skip to content

Commit 454179c

Browse files
committed
Add API calls that return pathlib.Path instead of str objects
Implements tox-dev#3
1 parent 6fe8168 commit 454179c

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

src/platformdirs/__init__.py

+120
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib
66
import os
77
import sys
8+
from pathlib import Path
89
from typing import TYPE_CHECKING, Optional, Type, Union
910

1011
if TYPE_CHECKING:
@@ -143,6 +144,118 @@ def user_log_dir(
143144
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_dir
144145

145146

147+
def user_data_path(
148+
appname: Optional[str] = None,
149+
appauthor: Union[str, None, "Literal[False]"] = None,
150+
version: Optional[str] = None,
151+
roaming: bool = False,
152+
) -> Path:
153+
"""
154+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
155+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
156+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
157+
:param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
158+
:returns: data directory tied to the user
159+
"""
160+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_path
161+
162+
163+
def site_data_path(
164+
appname: Optional[str] = None,
165+
appauthor: Union[str, None, "Literal[False]"] = None,
166+
version: Optional[str] = None,
167+
multipath: bool = False,
168+
) -> Path:
169+
"""
170+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
171+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
172+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
173+
:param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
174+
:returns: data directory shared by users
175+
"""
176+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_path
177+
178+
179+
def user_config_path(
180+
appname: Optional[str] = None,
181+
appauthor: Union[str, None, "Literal[False]"] = None,
182+
version: Optional[str] = None,
183+
roaming: bool = False,
184+
) -> Path:
185+
"""
186+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
187+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
188+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
189+
:param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
190+
:returns: config directory tied to the user
191+
"""
192+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_config_path
193+
194+
195+
def site_config_path(
196+
appname: Optional[str] = None,
197+
appauthor: Union[str, None, "Literal[False]"] = None,
198+
version: Optional[str] = None,
199+
multipath: bool = False,
200+
) -> Path:
201+
"""
202+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
203+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
204+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
205+
:param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
206+
:returns: config directory shared by the users
207+
"""
208+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_config_path
209+
210+
211+
def user_cache_path(
212+
appname: Optional[str] = None,
213+
appauthor: Union[str, None, "Literal[False]"] = None,
214+
version: Optional[str] = None,
215+
opinion: bool = True,
216+
) -> Path:
217+
"""
218+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
219+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
220+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
221+
:param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
222+
:returns: cache directory tied to the user
223+
"""
224+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_cache_path
225+
226+
227+
def user_state_path(
228+
appname: Optional[str] = None,
229+
appauthor: Union[str, None, "Literal[False]"] = None,
230+
version: Optional[str] = None,
231+
roaming: bool = False,
232+
) -> Path:
233+
"""
234+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
235+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
236+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
237+
:param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
238+
:returns: state directory tied to the user
239+
"""
240+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_state_path
241+
242+
243+
def user_log_path(
244+
appname: Optional[str] = None,
245+
appauthor: Union[str, None, "Literal[False]"] = None,
246+
version: Optional[str] = None,
247+
opinion: bool = True,
248+
) -> Path:
249+
"""
250+
:param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
251+
:param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
252+
:param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
253+
:param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
254+
:returns: log directory tied to the user
255+
"""
256+
return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_path
257+
258+
146259
__all__ = [
147260
"__version__",
148261
"__version_info__",
@@ -156,4 +269,11 @@ def user_log_dir(
156269
"user_log_dir",
157270
"site_data_dir",
158271
"site_config_dir",
272+
"user_data_path",
273+
"user_config_path",
274+
"user_cache_path",
275+
"user_state_path",
276+
"user_log_path",
277+
"site_data_path",
278+
"site_config_path",
159279
]

src/platformdirs/api.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
from abc import ABC, abstractmethod
4+
from pathlib import Path
45
from typing import Optional, Union
56

67
if sys.version_info >= (3, 8): # pragma: no branch
@@ -97,3 +98,38 @@ def user_state_dir(self) -> str:
9798
@abstractmethod
9899
def user_log_dir(self) -> str:
99100
""":return: log directory tied to the user"""
101+
102+
@property
103+
def user_data_path(self) -> Path:
104+
""":return: data directory tied to the user"""
105+
return Path(self.user_data_dir)
106+
107+
@property
108+
def site_data_path(self) -> Path:
109+
""":return: data directory shared by users"""
110+
return Path(self.site_data_dir)
111+
112+
@property
113+
def user_config_path(self) -> Path:
114+
""":return: config directory tied to the user"""
115+
return Path(self.user_config_dir)
116+
117+
@property
118+
def site_config_path(self) -> Path:
119+
""":return: config directory shared by the users"""
120+
return Path(self.site_config_dir)
121+
122+
@property
123+
def user_cache_path(self) -> Path:
124+
""":return: cache directory tied to the user"""
125+
return Path(self.user_cache_dir)
126+
127+
@property
128+
def user_state_path(self) -> Path:
129+
""":return: state directory tied to the user"""
130+
return Path(self.user_state_dir)
131+
132+
@property
133+
def user_log_path(self) -> Path:
134+
""":return: log directory tied to the user"""
135+
return Path(self.user_log_dir)

tests/conftest.py

+15
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@
1313
"site_config_dir",
1414
)
1515

16+
PROPS_PATH = (
17+
"user_data_path",
18+
"user_config_path",
19+
"user_cache_path",
20+
"user_state_path",
21+
"user_log_path",
22+
"site_data_path",
23+
"site_config_path",
24+
)
25+
1626

1727
@pytest.fixture(params=PROPS)
1828
def func(request: SubRequest) -> str:
1929
return cast(str, request.param)
2030

2131

32+
@pytest.fixture(params=PROPS_PATH)
33+
def func_path(request: SubRequest) -> str:
34+
return cast(str, request.param)
35+
36+
2237
@pytest.fixture()
2338
def props() -> Tuple[str, ...]:
2439
return PROPS

tests/test_api.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import Optional
23

34
import pytest
@@ -24,6 +25,18 @@ def test_property_result_is_str(func: str) -> None:
2425
assert isinstance(result, str)
2526

2627

28+
def test_method_result_is_path(func_path: str) -> None:
29+
method = getattr(platformdirs, func_path)
30+
result = method("MyApp", "MyCompany")
31+
assert isinstance(result, Path)
32+
33+
34+
def test_property_result_is_path(func_path: str) -> None:
35+
dirs = platformdirs.PlatformDirs("MyApp", "MyCompany", version="1.0")
36+
result = getattr(dirs, func_path)
37+
assert isinstance(result, Path)
38+
39+
2740
@pytest.mark.parametrize("root", ["A", "/system", None])
2841
@pytest.mark.parametrize("data", ["D", "/data", None])
2942
def test_android_active(monkeypatch: MonkeyPatch, root: Optional[str], data: Optional[str]) -> None:

0 commit comments

Comments
 (0)