Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Termux subsystem #63

Merged
merged 6 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/platformdirs/__init__.py
Expand Up @@ -28,7 +28,7 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]:
if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
from platformdirs.android import _android_folder

if _android_folder():
if _android_folder() is not None:
module, name = "platformdirs.android", "Android"

result: type[PlatformDirsABC] = getattr(importlib.import_module(module), name)
Expand Down
14 changes: 7 additions & 7 deletions src/platformdirs/android.py
Expand Up @@ -18,7 +18,7 @@ class Android(PlatformDirsABC):
@property
def user_data_dir(self) -> str:
""":return: data directory tied to the user, e.g. ``/data/user/<userid>/<packagename>/files/<AppName>``"""
return self._append_app_name_and_version(_android_folder(), "files")
return self._append_app_name_and_version(_android_folder() or "", "files")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the or here? I've understood that when this is called it's guaranteed that it will be not None, not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tox -e type swears

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy knows less than we, but probably a typing.cast for mypy likely is better choice than or 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, i think so too


@property
def site_data_dir(self) -> str:
Expand All @@ -30,7 +30,7 @@ def user_config_dir(self) -> str:
"""
:return: config directory tied to the user, e.g. ``/data/user/<userid>/<packagename>/shared_prefs/<AppName>``
"""
return self._append_app_name_and_version(_android_folder(), "shared_prefs")
return self._append_app_name_and_version(_android_folder() or "", "shared_prefs")

@property
def site_config_dir(self) -> str:
Expand All @@ -40,7 +40,7 @@ def site_config_dir(self) -> str:
@property
def user_cache_dir(self) -> str:
""":return: cache directory tied to the user, e.g. e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>``"""
return self._append_app_name_and_version(_android_folder(), "cache")
return self._append_app_name_and_version(_android_folder() or "", "cache")

@property
def user_state_dir(self) -> str:
Expand Down Expand Up @@ -78,14 +78,14 @@ def user_runtime_dir(self) -> str:


@lru_cache(maxsize=1)
def _android_folder() -> str:
""":return: base folder for the Android OS. If such cannot be found, an empty string is returned"""
def _android_folder() -> str | None:
""":return: base folder for the Android OS. If such cannot be found, `None` is returned"""
YariKartoshe4ka marked this conversation as resolved.
Show resolved Hide resolved
try:
# First try to get path to android app via pyjnius
from jnius import autoclass

Context = autoclass("android.content.Context") # noqa: N806
result: str = Context.getFilesDir().getParentFile().getAbsolutePath()
result: str | None = Context.getFilesDir().getParentFile().getAbsolutePath()
except Exception:
# if fails find an android folder looking path on the sys.path
pattern = re.compile(r"/data/(data|user/\d+)/(.+)/files")
Expand All @@ -94,7 +94,7 @@ def _android_folder() -> str:
result = path.split("/files")[0]
break
else:
result = ""
result = None
return result


Expand Down
2 changes: 1 addition & 1 deletion tests/test_android.py
Expand Up @@ -110,4 +110,4 @@ def test_android_folder_not_found(mocker: MockerFixture, monkeypatch: MonkeyPatc

_android_folder.cache_clear()
monkeypatch.setattr(sys, "path", [])
assert not _android_folder()
assert _android_folder() is None
2 changes: 1 addition & 1 deletion tests/test_api.py
Expand Up @@ -65,7 +65,7 @@ def test_android_active(monkeypatch: MonkeyPatch, root: str | None, data: str |
_android_folder.cache_clear()
monkeypatch.setattr(sys, "path", ["/A", "/B", path])

expected = root == "/system" and data == "/data" and _android_folder()
expected = root == "/system" and data == "/data" and _android_folder() is not None
if expected:
assert platformdirs._set_platform_dir_class() is Android
else:
Expand Down