From 1c1d03693ee7a30e333c58e9252f5591b3acf95e Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Thu, 1 Aug 2024 22:42:21 +0200 Subject: [PATCH 1/4] feature(utils): add util to locate application folder --- qtribu/toolbelt/application_folder.py | 66 +++++++++++++++++++++ tests/qgis/test_utils_application_folder.py | 45 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 qtribu/toolbelt/application_folder.py create mode 100755 tests/qgis/test_utils_application_folder.py diff --git a/qtribu/toolbelt/application_folder.py b/qtribu/toolbelt/application_folder.py new file mode 100644 index 00000000..792a374a --- /dev/null +++ b/qtribu/toolbelt/application_folder.py @@ -0,0 +1,66 @@ +#! python3 # noqa: E265 + +"""Find application folder. + +Inspired from Click: https://github.com/pallets/click/blob/14f735cf59618941cf2930e633eb77651b1dc7cb/src/click/utils.py#L449 + +""" + +# ############################################################################ +# ########## Imports ############### +# ################################## + +# standard library +from functools import lru_cache +from os import getenv +from pathlib import Path + +# ############################################################################ +# ########## Functions ############# +# ################################## + + +def _posixify(in_name: str) -> str: + """Make sure a string is POSIX friendly. + + :param in_name: input string to posixify + :type name: str + + :return: posixyfied string + :rtype: str + """ + return "-".join(in_name.split()).lower() + + +@lru_cache +def get_app_dir( + dir_name: str, roaming: bool = True, app_prefix: str = ".geotribu" +) -> Path: + """Get application directory, typically cache or config folder. The default + behavior is to return whatever is most appropriate for the operating system. + + :param dir_name: the directory name. Could be cache of config for example. + :type dir_name: str + :param roaming: controls if the folder should be roaming or not on Windows. Has no + effect otherwise, defaults to True + :type roaming: bool, optional + :param app_prefix: application prefix, defaults to ".geotribu" + :type app_prefix: str, optional + + :return: application folder path + :rtype: Path + + :example: + + .. code-block:: python + + print(get_app_dir(dir_name="rss")) + # Mac OS X: ~/Library/Application Support/.geotribu/rss + # Unix: /home//.geotribu/rss + # Windows (roaming): C:\\Users\\\\AppData\\Roaming\\.geotribu\\rss + print(get_app_dir(dir_name="rss", roaming=False)) + # Windows (not roaming): C:\\Users\\\\AppData\\Local\\.geotribu\\rss + """ + key = "APPDATA" if roaming else "LOCALAPPDATA" + base_folder = Path(getenv(key, Path.home())) + return base_folder.joinpath(f"{app_prefix}/{_posixify(dir_name)}") diff --git a/tests/qgis/test_utils_application_folder.py b/tests/qgis/test_utils_application_folder.py new file mode 100755 index 00000000..d3af51b4 --- /dev/null +++ b/tests/qgis/test_utils_application_folder.py @@ -0,0 +1,45 @@ +#! python3 # noqa E265 + +""" + Usage from the repo root folder: + + .. code-block:: bash + # for whole tests + python -m unittest tests.qgis.test_utils_application_folder + # for specific test + python -m unittest tests.qgis.test_utils_application_folder.TestToolbeltApplicationFolder.test_get_app_folder +""" + +# standard library +import unittest +from os import getenv +from pathlib import Path + +# project +from oslandia.toolbelt.application_folder import _posixify, get_app_dir + +# ############################################################################ +# ########## Classes ############# +# ################################ + + +class TestToolbeltApplicationFolder(unittest.TestCase): + """Test toolbelt application folder module""" + + def test_get_app_folder(self): + """Test application folder retrieval.""" + self.assertEqual( + get_app_dir(dir_name="cache", roaming=False), + Path(getenv("LOCALAPPDATA", Path.home())).joinpath(".oslandia/cache"), + ) + + def test_posixify(self): + """Test posixify util.""" + self.assertEqual(_posixify("test PoSiXiFieD string"), "test-posixified-string") + + +# ############################################################################ +# ####### Stand-alone run ######## +# ################################ +if __name__ == "__main__": + unittest.main() From 22f3ccf8c5993fdc6893996be56c7d54d5ddfd54 Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Thu, 1 Aug 2024 22:43:12 +0200 Subject: [PATCH 2/4] feature(settings): add app folder to settings --- qtribu/toolbelt/preferences.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qtribu/toolbelt/preferences.py b/qtribu/toolbelt/preferences.py index 43efc07e..251166cf 100644 --- a/qtribu/toolbelt/preferences.py +++ b/qtribu/toolbelt/preferences.py @@ -6,6 +6,7 @@ # standard from dataclasses import asdict, dataclass, fields +from pathlib import Path # PyQGIS from qgis.core import QgsSettings @@ -13,6 +14,7 @@ # package import qtribu.toolbelt.log_handler as log_hdlr from qtribu.__about__ import __title__, __version__ +from qtribu.toolbelt.application_folder import get_app_dir # ############################################################################ # ########## Classes ############### @@ -26,6 +28,7 @@ class PlgSettingsStructure: # global debug_mode: bool = False version: str = __version__ + local_app_folder: Path = get_app_dir(dir_name="cache") # RSS feed rss_source: str = "https://geotribu.fr/feed_rss_created.xml" From c1b77d712d6132dcb36ef51091ca02252e27cd80 Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Thu, 1 Aug 2024 22:58:34 +0200 Subject: [PATCH 3/4] fix(tests): change plugin name --- tests/qgis/test_utils_application_folder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/qgis/test_utils_application_folder.py b/tests/qgis/test_utils_application_folder.py index d3af51b4..a8c1f4c6 100755 --- a/tests/qgis/test_utils_application_folder.py +++ b/tests/qgis/test_utils_application_folder.py @@ -16,7 +16,7 @@ from pathlib import Path # project -from oslandia.toolbelt.application_folder import _posixify, get_app_dir +from qtribu.toolbelt.application_folder import _posixify, get_app_dir # ############################################################################ # ########## Classes ############# From 06f1fccaa2739f83ea8f1741d9d8cc39f575b42b Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Fri, 2 Aug 2024 08:06:18 +0200 Subject: [PATCH 4/4] fix(tests): remove refs to oslandia --- tests/qgis/test_utils_application_folder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/qgis/test_utils_application_folder.py b/tests/qgis/test_utils_application_folder.py index a8c1f4c6..2a9fed32 100755 --- a/tests/qgis/test_utils_application_folder.py +++ b/tests/qgis/test_utils_application_folder.py @@ -30,7 +30,7 @@ def test_get_app_folder(self): """Test application folder retrieval.""" self.assertEqual( get_app_dir(dir_name="cache", roaming=False), - Path(getenv("LOCALAPPDATA", Path.home())).joinpath(".oslandia/cache"), + Path(getenv("LOCALAPPDATA", Path.home())).joinpath(".geotribu/cache"), ) def test_posixify(self):