Update platformdirs==4.2.2

This commit is contained in:
JonnyWong16 2024-05-18 11:15:03 -07:00
commit 7726195686
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 38 additions and 11 deletions

View file

@ -6,7 +6,7 @@ import os
import re import re
import sys import sys
from functools import lru_cache from functools import lru_cache
from typing import cast from typing import TYPE_CHECKING, cast
from .api import PlatformDirsABC from .api import PlatformDirsABC
@ -117,16 +117,33 @@ class Android(PlatformDirsABC):
@lru_cache(maxsize=1) @lru_cache(maxsize=1)
def _android_folder() -> str | None: def _android_folder() -> str | None: # noqa: C901, PLR0912
""":return: base folder for the Android OS or None if it cannot be found""" """:return: base folder for the Android OS or None if it cannot be found"""
result: str | None = None
# type checker isn't happy with our "import android", just don't do this when type checking see
# https://stackoverflow.com/a/61394121
if not TYPE_CHECKING:
try: try:
# First try to get a path to android app via pyjnius # First try to get a path to android app using python4android (if available)...
from android import mActivity # noqa: PLC0415
context = cast("android.content.Context", mActivity.getApplicationContext()) # noqa: F821
result = context.getFilesDir().getParentFile().getAbsolutePath()
except Exception: # noqa: BLE001
result = None
if result is None:
try:
# ...and fall back to using plain pyjnius, if python4android isn't available or doesn't deliver any useful
# result...
from jnius import autoclass # noqa: PLC0415 from jnius import autoclass # noqa: PLC0415
context = autoclass("android.content.Context") context = autoclass("android.content.Context")
result: str | None = context.getFilesDir().getParentFile().getAbsolutePath() result = context.getFilesDir().getParentFile().getAbsolutePath()
except Exception: # noqa: BLE001 except Exception: # noqa: BLE001
# if fails find an android folder looking a path on the sys.path result = None
if result is None:
# and if that fails, too, find an android folder looking at path on the sys.path
# warning: only works for apps installed under /data, not adopted storage etc.
pattern = re.compile(r"/data/(data|user/\d+)/(.+)/files") pattern = re.compile(r"/data/(data|user/\d+)/(.+)/files")
for path in sys.path: for path in sys.path:
if pattern.match(path): if pattern.match(path):
@ -134,6 +151,16 @@ def _android_folder() -> str | None:
break break
else: else:
result = None result = None
if result is None:
# one last try: find an android folder looking at path on the sys.path taking adopted storage paths into
# account
pattern = re.compile(r"/mnt/expand/[a-fA-F0-9-]{36}/(data|user/\d+)/(.+)/files")
for path in sys.path:
if pattern.match(path):
result = path.split("/files")[0]
break
else:
result = None
return result return result

View file

@ -12,5 +12,5 @@ __version__: str
__version_tuple__: VERSION_TUPLE __version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE version_tuple: VERSION_TUPLE
__version__ = version = '4.2.1' __version__ = version = '4.2.2'
__version_tuple__ = version_tuple = (4, 2, 1) __version_tuple__ = version_tuple = (4, 2, 2)