From 454179ce7fbc3f5427a64db884b95b3591eae814 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Thu, 22 Jul 2021 22:33:31 +0200 Subject: [PATCH 01/14] Add API calls that return pathlib.Path instead of str objects Implements https://github.com/platformdirs/platformdirs/issues/3 --- src/platformdirs/__init__.py | 120 +++++++++++++++++++++++++++++++++++ src/platformdirs/api.py | 36 +++++++++++ tests/conftest.py | 15 +++++ tests/test_api.py | 13 ++++ 4 files changed, 184 insertions(+) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index b2a265ed..e2293149 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -5,6 +5,7 @@ import importlib import os import sys +from pathlib import Path from typing import TYPE_CHECKING, Optional, Type, Union if TYPE_CHECKING: @@ -143,6 +144,118 @@ def user_log_dir( return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_dir +def user_data_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + roaming: bool = False, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param roaming: See `roaming `. + :returns: data directory tied to the user + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_path + + +def site_data_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + multipath: bool = False, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param multipath: See `roaming `. + :returns: data directory shared by users + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_path + + +def user_config_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + roaming: bool = False, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param roaming: See `roaming `. + :returns: config directory tied to the user + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_config_path + + +def site_config_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + multipath: bool = False, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param multipath: See `roaming `. + :returns: config directory shared by the users + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_config_path + + +def user_cache_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + opinion: bool = True, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param opinion: See `roaming `. + :returns: cache directory tied to the user + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_cache_path + + +def user_state_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + roaming: bool = False, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param roaming: See `roaming `. + :returns: state directory tied to the user + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_state_path + + +def user_log_path( + appname: Optional[str] = None, + appauthor: Union[str, None, "Literal[False]"] = None, + version: Optional[str] = None, + opinion: bool = True, +) -> Path: + """ + :param appname: See `appname `. + :param appauthor: See `appauthor `. + :param version: See `version `. + :param opinion: See `roaming `. + :returns: log directory tied to the user + """ + return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_path + + __all__ = [ "__version__", "__version_info__", @@ -156,4 +269,11 @@ def user_log_dir( "user_log_dir", "site_data_dir", "site_config_dir", + "user_data_path", + "user_config_path", + "user_cache_path", + "user_state_path", + "user_log_path", + "site_data_path", + "site_config_path", ] diff --git a/src/platformdirs/api.py b/src/platformdirs/api.py index 159a7fc2..3d79917c 100644 --- a/src/platformdirs/api.py +++ b/src/platformdirs/api.py @@ -1,6 +1,7 @@ import os import sys from abc import ABC, abstractmethod +from pathlib import Path from typing import Optional, Union if sys.version_info >= (3, 8): # pragma: no branch @@ -97,3 +98,38 @@ def user_state_dir(self) -> str: @abstractmethod def user_log_dir(self) -> str: """:return: log directory tied to the user""" + + @property + def user_data_path(self) -> Path: + """:return: data directory tied to the user""" + return Path(self.user_data_dir) + + @property + def site_data_path(self) -> Path: + """:return: data directory shared by users""" + return Path(self.site_data_dir) + + @property + def user_config_path(self) -> Path: + """:return: config directory tied to the user""" + return Path(self.user_config_dir) + + @property + def site_config_path(self) -> Path: + """:return: config directory shared by the users""" + return Path(self.site_config_dir) + + @property + def user_cache_path(self) -> Path: + """:return: cache directory tied to the user""" + return Path(self.user_cache_dir) + + @property + def user_state_path(self) -> Path: + """:return: state directory tied to the user""" + return Path(self.user_state_dir) + + @property + def user_log_path(self) -> Path: + """:return: log directory tied to the user""" + return Path(self.user_log_dir) diff --git a/tests/conftest.py b/tests/conftest.py index 8392fe87..e1309c73 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,12 +13,27 @@ "site_config_dir", ) +PROPS_PATH = ( + "user_data_path", + "user_config_path", + "user_cache_path", + "user_state_path", + "user_log_path", + "site_data_path", + "site_config_path", +) + @pytest.fixture(params=PROPS) def func(request: SubRequest) -> str: return cast(str, request.param) +@pytest.fixture(params=PROPS_PATH) +def func_path(request: SubRequest) -> str: + return cast(str, request.param) + + @pytest.fixture() def props() -> Tuple[str, ...]: return PROPS diff --git a/tests/test_api.py b/tests/test_api.py index 665a9576..4f4953ab 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import Optional import pytest @@ -24,6 +25,18 @@ def test_property_result_is_str(func: str) -> None: assert isinstance(result, str) +def test_method_result_is_path(func_path: str) -> None: + method = getattr(platformdirs, func_path) + result = method("MyApp", "MyCompany") + assert isinstance(result, Path) + + +def test_property_result_is_path(func_path: str) -> None: + dirs = platformdirs.PlatformDirs("MyApp", "MyCompany", version="1.0") + result = getattr(dirs, func_path) + assert isinstance(result, Path) + + @pytest.mark.parametrize("root", ["A", "/system", None]) @pytest.mark.parametrize("data", ["D", "/data", None]) def test_android_active(monkeypatch: MonkeyPatch, root: Optional[str], data: Optional[str]) -> None: From 4ac814df0978f4147f6969b3b3ae427bccc239b4 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Thu, 22 Jul 2021 23:12:01 +0200 Subject: [PATCH 02/14] Add documentation --- CHANGES.rst | 5 +++++ docs/api.rst | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 1e2cae5f..dcc7cb52 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ platformdirs Changelog ====================== +platformdirs 2.2.0 +------------------ +- [Issue 3] Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` + - by @papr + platformdirs 2.1.0 ------------------ - Add ``readthedocs.org`` documentation via Sphinx diff --git a/docs/api.rst b/docs/api.rst index 536544c8..92e9c1e5 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -10,26 +10,31 @@ User data directory ------------------- .. autofunction:: platformdirs.user_data_dir +.. autofunction:: platformdirs.user_data_path User config directory --------------------- .. autofunction:: platformdirs.user_config_dir +.. autofunction:: platformdirs.user_config_path Cache directory ------------------- .. autofunction:: platformdirs.user_cache_dir +.. autofunction:: platformdirs.user_cache_path State directory ------------------- .. autofunction:: platformdirs.user_state_dir +.. autofunction:: platformdirs.user_state_path Logs directory ------------------- .. autofunction:: platformdirs.user_log_dir +.. autofunction:: platformdirs.user_log_path Shared directories ~~~~~~~~~~~~~~~~~~ @@ -40,11 +45,13 @@ Shared data directory --------------------- .. autofunction:: platformdirs.site_data_dir +.. autofunction:: platformdirs.site_data_path Shared config directory ----------------------- .. autofunction:: platformdirs.site_config_dir +.. autofunction:: platformdirs.site_config_path Platforms ~~~~~~~~~ From bbd3f5e5ae1959e14071d6de33cab00402723226 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 10:55:46 +0200 Subject: [PATCH 03/14] Add pathlib to flake8-spellcheck whitlist --- whitelist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/whitelist.txt b/whitelist.txt index df3a104d..4cbe9a7f 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -23,6 +23,7 @@ normpath ord param params +pathlib pathsep platformdirs pyjnius From 5986fd4262e57fa3d2727d6e7bb91560dc5d10bc Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 11:43:24 +0200 Subject: [PATCH 04/14] Support multipath handling in platformdirs.site_data_path() See https://github.com/platformdirs/platformdirs/issues/24 --- src/platformdirs/__init__.py | 2 +- src/platformdirs/api.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index e2293149..8b63c760 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -170,7 +170,7 @@ def site_data_path( :param appname: See `appname `. :param appauthor: See `appauthor `. :param version: See `version `. - :param multipath: See `roaming `. + :param multipath: See `multipath `. :returns: data directory shared by users """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_path diff --git a/src/platformdirs/api.py b/src/platformdirs/api.py index 3d79917c..2708c2c8 100644 --- a/src/platformdirs/api.py +++ b/src/platformdirs/api.py @@ -52,7 +52,10 @@ def __init__( self.multipath = multipath """ An optional parameter only applicable to Unix/Linux which indicates that the entire list of data dirs should be - returned. By default, the first item would only be returned. + returned. By default, the first item would only be returned. This parameter does not affect the + :class:`pathlib.Path `-returning methods, e.g. :meth:`platformdirs.site_data_path`. These always + return the first item. See `platformdirs/platformdirs#24 + `_ for a discussion. """ self.opinion = opinion #: A flag to indicating to use opinionated values. @@ -107,7 +110,12 @@ def user_data_path(self) -> Path: @property def site_data_path(self) -> Path: """:return: data directory shared by users""" - return Path(self.site_data_dir) + site_data_dir = self.site_data_dir + if self.multipath: + # If multipath is True, the first path is returned. See + # https://github.com/platformdirs/platformdirs/issues/24 for a discussion. + site_data_dir = site_data_dir.split(os.pathsep)[0] + return Path(site_data_dir) @property def user_config_path(self) -> Path: From b38fc3bb12c00f5fc5e1aa15b8222a17dc9d7a05 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 12:16:08 +0200 Subject: [PATCH 05/14] Extend changelog --- CHANGES.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index dcc7cb52..09bfd2f5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,17 @@ platformdirs Changelog platformdirs 2.2.0 ------------------ - [Issue 3] Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` - - by @papr + - by `@papr `_ + + * **NEW** :func:`user_data_path ` + * **NEW** :func:`user_config_path ` + * **NEW** :func:`user_cache_path ` + * **NEW** :func:`user_state_path ` + * **NEW** :func:`user_log_path ` + * **NEW** :func:`site_data_path ` (**Note:** Always returns first item, even if the + ``multipath`` argument is set to ``True``) + * **NEW** :func:`site_config_path ` + * **NEW** Add the according properties to :class:`PlatformDirsABC ` platformdirs 2.1.0 ------------------ From d87f66c0bb4e75143f200b107a04319dc3af5862 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 12:43:01 +0200 Subject: [PATCH 06/14] Simplified changelog and moved changes to 2.1.0 release --- CHANGES.rst | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 09bfd2f5..5dd7f074 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,21 +1,6 @@ platformdirs Changelog ====================== -platformdirs 2.2.0 ------------------- -- [Issue 3] Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` - - by `@papr `_ - - * **NEW** :func:`user_data_path ` - * **NEW** :func:`user_config_path ` - * **NEW** :func:`user_cache_path ` - * **NEW** :func:`user_state_path ` - * **NEW** :func:`user_log_path ` - * **NEW** :func:`site_data_path ` (**Note:** Always returns first item, even if the - ``multipath`` argument is set to ``True``) - * **NEW** :func:`site_config_path ` - * **NEW** Add the according properties to :class:`PlatformDirsABC ` - platformdirs 2.1.0 ------------------ - Add ``readthedocs.org`` documentation via Sphinx @@ -27,6 +12,9 @@ platformdirs 2.1.0 :class:`PlatformDirsABC ` and it's implementations: :class:`Android `, :class:`MacOS `, :class:`Unix ` and :class:`Windows ` +- [Issue 3] Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` + (``user_data_path``, ``user_config_path``, ``user_cache_path``, ``user_state_path``, ``user_log_path``, + ``site_data_path``, ``site_config_path``) - by `@papr `_ platformdirs 2.0.2 ------------------ From 8a46dcfa4d7c7f564375d3f6a526c1385cd98dc7 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 12:51:47 +0200 Subject: [PATCH 07/14] Use `path` instead of `directory` in docstrings for naming consistency --- src/platformdirs/__init__.py | 14 +++++++------- src/platformdirs/api.py | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index 8b63c760..f2f6abc9 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -155,7 +155,7 @@ def user_data_path( :param appauthor: See `appauthor `. :param version: See `version `. :param roaming: See `roaming `. - :returns: data directory tied to the user + :returns: data path tied to the user """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_path @@ -171,7 +171,7 @@ def site_data_path( :param appauthor: See `appauthor `. :param version: See `version `. :param multipath: See `multipath `. - :returns: data directory shared by users + :returns: data path shared by users """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_path @@ -187,7 +187,7 @@ def user_config_path( :param appauthor: See `appauthor `. :param version: See `version `. :param roaming: See `roaming `. - :returns: config directory tied to the user + :returns: config path tied to the user """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_config_path @@ -203,7 +203,7 @@ def site_config_path( :param appauthor: See `appauthor `. :param version: See `version `. :param multipath: See `roaming `. - :returns: config directory shared by the users + :returns: config path shared by the users """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_config_path @@ -219,7 +219,7 @@ def user_cache_path( :param appauthor: See `appauthor `. :param version: See `version `. :param opinion: See `roaming `. - :returns: cache directory tied to the user + :returns: cache path tied to the user """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_cache_path @@ -235,7 +235,7 @@ def user_state_path( :param appauthor: See `appauthor `. :param version: See `version `. :param roaming: See `roaming `. - :returns: state directory tied to the user + :returns: state path tied to the user """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_state_path @@ -251,7 +251,7 @@ def user_log_path( :param appauthor: See `appauthor `. :param version: See `version `. :param opinion: See `roaming `. - :returns: log directory tied to the user + :returns: log path tied to the user """ return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_path diff --git a/src/platformdirs/api.py b/src/platformdirs/api.py index 2708c2c8..68dd5fc0 100644 --- a/src/platformdirs/api.py +++ b/src/platformdirs/api.py @@ -104,12 +104,12 @@ def user_log_dir(self) -> str: @property def user_data_path(self) -> Path: - """:return: data directory tied to the user""" + """:return: data path tied to the user""" return Path(self.user_data_dir) @property def site_data_path(self) -> Path: - """:return: data directory shared by users""" + """:return: data path shared by users""" site_data_dir = self.site_data_dir if self.multipath: # If multipath is True, the first path is returned. See @@ -119,25 +119,25 @@ def site_data_path(self) -> Path: @property def user_config_path(self) -> Path: - """:return: config directory tied to the user""" + """:return: config path tied to the user""" return Path(self.user_config_dir) @property def site_config_path(self) -> Path: - """:return: config directory shared by the users""" + """:return: config path shared by the users""" return Path(self.site_config_dir) @property def user_cache_path(self) -> Path: - """:return: cache directory tied to the user""" + """:return: cache path tied to the user""" return Path(self.user_cache_dir) @property def user_state_path(self) -> Path: - """:return: state directory tied to the user""" + """:return: state path tied to the user""" return Path(self.user_state_dir) @property def user_log_path(self) -> Path: - """:return: log directory tied to the user""" + """:return: log path tied to the user""" return Path(self.user_log_dir) From 3af7fca32eb4a5fd68c01e097b6136ef245725af Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 13:33:25 +0200 Subject: [PATCH 08/14] Move XDG/Unix-specific multipath code to unix module --- src/platformdirs/api.py | 12 ++---------- src/platformdirs/unix.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/platformdirs/api.py b/src/platformdirs/api.py index 68dd5fc0..7b291734 100644 --- a/src/platformdirs/api.py +++ b/src/platformdirs/api.py @@ -52,10 +52,7 @@ def __init__( self.multipath = multipath """ An optional parameter only applicable to Unix/Linux which indicates that the entire list of data dirs should be - returned. By default, the first item would only be returned. This parameter does not affect the - :class:`pathlib.Path `-returning methods, e.g. :meth:`platformdirs.site_data_path`. These always - return the first item. See `platformdirs/platformdirs#24 - `_ for a discussion. + returned. By default, the first item would only be returned. """ self.opinion = opinion #: A flag to indicating to use opinionated values. @@ -110,12 +107,7 @@ def user_data_path(self) -> Path: @property def site_data_path(self) -> Path: """:return: data path shared by users""" - site_data_dir = self.site_data_dir - if self.multipath: - # If multipath is True, the first path is returned. See - # https://github.com/platformdirs/platformdirs/issues/24 for a discussion. - site_data_dir = site_data_dir.split(os.pathsep)[0] - return Path(site_data_dir) + return Path(self.site_data_dir) @property def user_config_path(self) -> Path: diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 2dde7493..06ea4aee 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from .api import PlatformDirsABC @@ -66,7 +67,6 @@ def site_config_dir(self) -> str: :return: config directories shared by users (if `multipath ` is enabled and ``XDG_DATA_DIR`` is set and a multi path the response is also a multi path separated by the OS path separator), e.g. ``/etc/xdg/$appname/$version`` - :return: """ # XDG default for $XDG_CONFIG_DIRS only first, if multipath is False if "XDG_CONFIG_DIRS" in os.environ: @@ -109,6 +109,15 @@ def user_log_dir(self) -> str: path = os.path.join(path, "log") return path + @property + def site_data_path(self) -> Path: + """:return: data path shared by users. Only return first item, even if ``multipath`` is set to ``True``""" + site_data_dir = self.site_data_dir + if self.multipath: + # If multipath is True, the first path is returned. + site_data_dir = site_data_dir.split(os.pathsep)[0] + return Path(site_data_dir) + __all__ = [ "Unix", From b915a0cad46e9d68dae321dc0c37d8cd1c6c5119 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 13:33:57 +0200 Subject: [PATCH 09/14] Handle multipath in site_config_dir --- src/platformdirs/unix.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 06ea4aee..8bbc0a44 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -118,6 +118,15 @@ def site_data_path(self) -> Path: site_data_dir = site_data_dir.split(os.pathsep)[0] return Path(site_data_dir) + @property + def site_config_path(self) -> Path: + """:return: config path shared by the users. Only return first item, even if ``multipath`` is set to ``True``""" + site_config_dir = self.site_config_dir + if self.multipath: + # If multipath is True, the first path is returned. + site_config_dir = site_config_dir.split(os.pathsep)[0] + return Path(site_config_dir) + __all__ = [ "Unix", From 401761516c719d94bc4b2b1096735a329a7b83bc Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 14:33:11 +0200 Subject: [PATCH 10/14] Tests: Make sure that there is a "path" variant for every "dir" variant --- tests/conftest.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e1309c73..d3e9eae6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,25 +13,17 @@ "site_config_dir", ) -PROPS_PATH = ( - "user_data_path", - "user_config_path", - "user_cache_path", - "user_state_path", - "user_log_path", - "site_data_path", - "site_config_path", -) - @pytest.fixture(params=PROPS) def func(request: SubRequest) -> str: return cast(str, request.param) -@pytest.fixture(params=PROPS_PATH) +@pytest.fixture(params=PROPS) def func_path(request: SubRequest) -> str: - return cast(str, request.param) + prop = cast(str, request.param) + prop = prop.replace("_dir", "_path") + return prop @pytest.fixture() From e4f91f0e758e760157f4c6329ddae53718bdac79 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 15:04:01 +0200 Subject: [PATCH 11/14] Test if interfaces of directory/path function pairs are in sync --- tests/test_api.py | 11 +++++++++++ whitelist.txt | 1 + 2 files changed, 12 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index 4f4953ab..3eab8543 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,3 +1,4 @@ +import inspect from pathlib import Path from typing import Optional @@ -37,6 +38,16 @@ def test_property_result_is_path(func_path: str) -> None: assert isinstance(result, Path) +def test_function_interface_is_in_sync(func: str) -> None: + function_dir = getattr(platformdirs, func) + function_path = getattr(platformdirs, func.replace("_dir", "_path")) + assert inspect.isfunction(function_dir) + assert inspect.isfunction(function_path) + function_dir_signature = inspect.Signature.from_callable(function_dir) + function_path_signature = inspect.Signature.from_callable(function_path) + assert function_dir_signature.parameters == function_path_signature.parameters + + @pytest.mark.parametrize("root", ["A", "/system", None]) @pytest.mark.parametrize("data", ["D", "/data", None]) def test_android_active(monkeypatch: MonkeyPatch, root: Optional[str], data: Optional[str]) -> None: diff --git a/whitelist.txt b/whitelist.txt index 4cbe9a7f..7428da1c 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -14,6 +14,7 @@ highbit HKEY impl intersphinx +isfunction jnius kernel32 lru From 7c37dd49b6c57c0a08c2041f3785282ce800d7f6 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 15:06:27 +0200 Subject: [PATCH 12/14] Remove "[Issue 3]" from changelog --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 5dd7f074..761d251b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,7 +12,7 @@ platformdirs 2.1.0 :class:`PlatformDirsABC ` and it's implementations: :class:`Android `, :class:`MacOS `, :class:`Unix ` and :class:`Windows ` -- [Issue 3] Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` +- Add ``*_path`` API, returning :class:`pathlib.Path ` objects instead of :class:`str` (``user_data_path``, ``user_config_path``, ``user_cache_path``, ``user_state_path``, ``user_log_path``, ``site_data_path``, ``site_config_path``) - by `@papr `_ From 4eedd915bdfea46cac9a38fed8eb454503748ab4 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 15:20:20 +0200 Subject: [PATCH 13/14] Unix: Extract multipath handling for path properties into private function --- src/platformdirs/unix.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 8bbc0a44..998e0b52 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -112,20 +112,21 @@ def user_log_dir(self) -> str: @property def site_data_path(self) -> Path: """:return: data path shared by users. Only return first item, even if ``multipath`` is set to ``True``""" - site_data_dir = self.site_data_dir - if self.multipath: - # If multipath is True, the first path is returned. - site_data_dir = site_data_dir.split(os.pathsep)[0] - return Path(site_data_dir) + return self._first_item_as_path_if_multipath(self.site_data_dir) @property def site_config_path(self) -> Path: """:return: config path shared by the users. Only return first item, even if ``multipath`` is set to ``True``""" - site_config_dir = self.site_config_dir + return self._first_item_as_path_if_multipath(self.site_config_dir) + + def _first_item_as_path_if_multipath(self, directory: str) -> Path: + """ + :return: first directory as path, even if ``multipath`` is set to ``True`` + """ if self.multipath: # If multipath is True, the first path is returned. - site_config_dir = site_config_dir.split(os.pathsep)[0] - return Path(site_config_dir) + directory = directory.split(os.pathsep)[0] + return Path(directory) __all__ = [ From f90a11a422c5ff23eb3f372471ae03abf0969b34 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Fri, 23 Jul 2021 16:54:45 +0200 Subject: [PATCH 14/14] Remove docstring from private method --- src/platformdirs/unix.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 998e0b52..02beca29 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -120,9 +120,6 @@ def site_config_path(self) -> Path: return self._first_item_as_path_if_multipath(self.site_config_dir) def _first_item_as_path_if_multipath(self, directory: str) -> Path: - """ - :return: first directory as path, even if ``multipath`` is set to ``True`` - """ if self.multipath: # If multipath is True, the first path is returned. directory = directory.split(os.pathsep)[0]