]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/platformdirs/android.py
572559f8d4edde3621f336c3f9e1be3c3adcf667
2 from __future__
import annotations
7 from functools
import lru_cache
8 from typing
import cast
10 from .api
import PlatformDirsABC
13 class Android(PlatformDirsABC
):
15 Follows the guidance `from here <https://android.stackexchange.com/a/216132>`_. Makes use of the
16 `appname <platformdirs.api.PlatformDirsABC.appname>`,
17 `version <platformdirs.api.PlatformDirsABC.version>`,
18 `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
22 def user_data_dir(self
) -> str:
23 """:return: data directory tied to the user, e.g. ``/data/user/<userid>/<packagename>/files/<AppName>``"""
24 return self
._append
_app
_name
_and
_version
(cast(str, _android_folder()), "files")
27 def site_data_dir(self
) -> str:
28 """:return: data directory shared by users, same as `user_data_dir`"""
29 return self
.user_data_dir
32 def user_config_dir(self
) -> str:
34 :return: config directory tied to the user, e.g. \
35 ``/data/user/<userid>/<packagename>/shared_prefs/<AppName>``
37 return self
._append
_app
_name
_and
_version
(cast(str, _android_folder()), "shared_prefs")
40 def site_config_dir(self
) -> str:
41 """:return: config directory shared by the users, same as `user_config_dir`"""
42 return self
.user_config_dir
45 def user_cache_dir(self
) -> str:
46 """:return: cache directory tied to the user, e.g. e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>``"""
47 return self
._append
_app
_name
_and
_version
(cast(str, _android_folder()), "cache")
50 def site_cache_dir(self
) -> str:
51 """:return: cache directory shared by users, same as `user_cache_dir`"""
52 return self
.user_cache_dir
55 def user_state_dir(self
) -> str:
56 """:return: state directory tied to the user, same as `user_data_dir`"""
57 return self
.user_data_dir
60 def user_log_dir(self
) -> str:
62 :return: log directory tied to the user, same as `user_cache_dir` if not opinionated else ``log`` in it,
63 e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>/log``
65 path
= self
.user_cache_dir
67 path
= os
.path
.join(path
, "log") # noqa: PTH118
71 def user_documents_dir(self
) -> str:
72 """:return: documents directory tied to the user e.g. ``/storage/emulated/0/Documents``"""
73 return _android_documents_folder()
76 def user_downloads_dir(self
) -> str:
77 """:return: downloads directory tied to the user e.g. ``/storage/emulated/0/Downloads``"""
78 return _android_downloads_folder()
81 def user_pictures_dir(self
) -> str:
82 """:return: pictures directory tied to the user e.g. ``/storage/emulated/0/Pictures``"""
83 return _android_pictures_folder()
86 def user_videos_dir(self
) -> str:
87 """:return: videos directory tied to the user e.g. ``/storage/emulated/0/DCIM/Camera``"""
88 return _android_videos_folder()
91 def user_music_dir(self
) -> str:
92 """:return: music directory tied to the user e.g. ``/storage/emulated/0/Music``"""
93 return _android_music_folder()
96 def user_desktop_dir(self
) -> str:
97 """:return: desktop directory tied to the user e.g. ``/storage/emulated/0/Desktop``"""
98 return "/storage/emulated/0/Desktop"
101 def user_runtime_dir(self
) -> str:
103 :return: runtime directory tied to the user, same as `user_cache_dir` if not opinionated else ``tmp`` in it,
104 e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>/tmp``
106 path
= self
.user_cache_dir
108 path
= os
.path
.join(path
, "tmp") # noqa: PTH118
112 def site_runtime_dir(self
) -> str:
113 """:return: runtime directory shared by users, same as `user_runtime_dir`"""
114 return self
.user_runtime_dir
117 @lru_cache(maxsize
=1)
118 def _android_folder() -> str |
None:
119 """:return: base folder for the Android OS or None if it cannot be found"""
121 # First try to get path to android app via pyjnius
122 from jnius
import autoclass
124 context
= autoclass("android.content.Context")
125 result
: str |
None = context
.getFilesDir().getParentFile().getAbsolutePath()
126 except Exception: # noqa: BLE001
127 # if fails find an android folder looking path on the sys.path
128 pattern
= re
.compile(r
"/data/(data|user/\d+)/(.+)/files")
129 for path
in sys
.path
:
130 if pattern
.match(path
):
131 result
= path
.split("/files")[0]
138 @lru_cache(maxsize
=1)
139 def _android_documents_folder() -> str:
140 """:return: documents folder for the Android OS"""
141 # Get directories with pyjnius
143 from jnius
import autoclass
145 context
= autoclass("android.content.Context")
146 environment
= autoclass("android.os.Environment")
147 documents_dir
: str = context
.getExternalFilesDir(environment
.DIRECTORY_DOCUMENTS
).getAbsolutePath()
148 except Exception: # noqa: BLE001
149 documents_dir
= "/storage/emulated/0/Documents"
154 @lru_cache(maxsize
=1)
155 def _android_downloads_folder() -> str:
156 """:return: downloads folder for the Android OS"""
157 # Get directories with pyjnius
159 from jnius
import autoclass
161 context
= autoclass("android.content.Context")
162 environment
= autoclass("android.os.Environment")
163 downloads_dir
: str = context
.getExternalFilesDir(environment
.DIRECTORY_DOWNLOADS
).getAbsolutePath()
164 except Exception: # noqa: BLE001
165 downloads_dir
= "/storage/emulated/0/Downloads"
170 @lru_cache(maxsize
=1)
171 def _android_pictures_folder() -> str:
172 """:return: pictures folder for the Android OS"""
173 # Get directories with pyjnius
175 from jnius
import autoclass
177 context
= autoclass("android.content.Context")
178 environment
= autoclass("android.os.Environment")
179 pictures_dir
: str = context
.getExternalFilesDir(environment
.DIRECTORY_PICTURES
).getAbsolutePath()
180 except Exception: # noqa: BLE001
181 pictures_dir
= "/storage/emulated/0/Pictures"
186 @lru_cache(maxsize
=1)
187 def _android_videos_folder() -> str:
188 """:return: videos folder for the Android OS"""
189 # Get directories with pyjnius
191 from jnius
import autoclass
193 context
= autoclass("android.content.Context")
194 environment
= autoclass("android.os.Environment")
195 videos_dir
: str = context
.getExternalFilesDir(environment
.DIRECTORY_DCIM
).getAbsolutePath()
196 except Exception: # noqa: BLE001
197 videos_dir
= "/storage/emulated/0/DCIM/Camera"
202 @lru_cache(maxsize
=1)
203 def _android_music_folder() -> str:
204 """:return: music folder for the Android OS"""
205 # Get directories with pyjnius
207 from jnius
import autoclass
209 context
= autoclass("android.content.Context")
210 environment
= autoclass("android.os.Environment")
211 music_dir
: str = context
.getExternalFilesDir(environment
.DIRECTORY_MUSIC
).getAbsolutePath()
212 except Exception: # noqa: BLE001
213 music_dir
= "/storage/emulated/0/Music"