1"""
2Utilities for determining application-specific dirs.
3
4See <https://github.com/platformdirs/platformdirs> for details and usage.
5
6"""
7
8from __future__ import annotations
9
10import os
11import sys
12from typing import TYPE_CHECKING
13
14from .api import PlatformDirsABC
15from .version import __version__
16from .version import __version_tuple__ as __version_info__
17
18if TYPE_CHECKING:
19 from pathlib import Path
20 from typing import Literal
21
22if sys.platform == "win32":
23 from platformdirs.windows import Windows as _Result
24elif sys.platform == "darwin":
25 from platformdirs.macos import MacOS as _Result
26else:
27 from platformdirs.unix import Unix as _Result
28
29
30def _set_platform_dir_class() -> type[PlatformDirsABC]:
31 if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
32 if os.getenv("SHELL") or os.getenv("PREFIX"):
33 return _Result
34
35 from platformdirs.android import _android_folder # noqa: PLC0415
36
37 if _android_folder() is not None:
38 from platformdirs.android import Android # noqa: PLC0415
39
40 return Android # return to avoid redefinition of a result
41
42 return _Result
43
44
45if TYPE_CHECKING:
46 # Work around mypy issue: https://github.com/python/mypy/issues/10962
47 PlatformDirs = _Result
48else:
49 PlatformDirs = _set_platform_dir_class() #: Currently active platform
50AppDirs = PlatformDirs #: Backwards compatibility with appdirs
51
52
53def user_data_dir(
54 appname: str | None = None,
55 appauthor: str | None | Literal[False] = None,
56 version: str | None = None,
57 roaming: bool = False, # noqa: FBT001, FBT002
58 ensure_exists: bool = False, # noqa: FBT001, FBT002
59) -> str:
60 """
61 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
62 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
63 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
64 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
65 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
66 :returns: data directory tied to the user
67 """
68 return PlatformDirs(
69 appname=appname,
70 appauthor=appauthor,
71 version=version,
72 roaming=roaming,
73 ensure_exists=ensure_exists,
74 ).user_data_dir
75
76
77def site_data_dir(
78 appname: str | None = None,
79 appauthor: str | None | Literal[False] = None,
80 version: str | None = None,
81 multipath: bool = False, # noqa: FBT001, FBT002
82 ensure_exists: bool = False, # noqa: FBT001, FBT002
83) -> str:
84 """
85 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
86 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
87 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
88 :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
89 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
90 :returns: data directory shared by users
91 """
92 return PlatformDirs(
93 appname=appname,
94 appauthor=appauthor,
95 version=version,
96 multipath=multipath,
97 ensure_exists=ensure_exists,
98 ).site_data_dir
99
100
101def user_config_dir(
102 appname: str | None = None,
103 appauthor: str | None | Literal[False] = None,
104 version: str | None = None,
105 roaming: bool = False, # noqa: FBT001, FBT002
106 ensure_exists: bool = False, # noqa: FBT001, FBT002
107) -> str:
108 """
109 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
110 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
111 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
112 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
113 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
114 :returns: config directory tied to the user
115 """
116 return PlatformDirs(
117 appname=appname,
118 appauthor=appauthor,
119 version=version,
120 roaming=roaming,
121 ensure_exists=ensure_exists,
122 ).user_config_dir
123
124
125def site_config_dir(
126 appname: str | None = None,
127 appauthor: str | None | Literal[False] = None,
128 version: str | None = None,
129 multipath: bool = False, # noqa: FBT001, FBT002
130 ensure_exists: bool = False, # noqa: FBT001, FBT002
131) -> str:
132 """
133 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
134 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
135 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
136 :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
137 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
138 :returns: config directory shared by the users
139 """
140 return PlatformDirs(
141 appname=appname,
142 appauthor=appauthor,
143 version=version,
144 multipath=multipath,
145 ensure_exists=ensure_exists,
146 ).site_config_dir
147
148
149def user_cache_dir(
150 appname: str | None = None,
151 appauthor: str | None | Literal[False] = None,
152 version: str | None = None,
153 opinion: bool = True, # noqa: FBT001, FBT002
154 ensure_exists: bool = False, # noqa: FBT001, FBT002
155) -> str:
156 """
157 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
158 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
159 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
160 :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
161 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
162 :returns: cache directory tied to the user
163 """
164 return PlatformDirs(
165 appname=appname,
166 appauthor=appauthor,
167 version=version,
168 opinion=opinion,
169 ensure_exists=ensure_exists,
170 ).user_cache_dir
171
172
173def site_cache_dir(
174 appname: str | None = None,
175 appauthor: str | None | Literal[False] = None,
176 version: str | None = None,
177 opinion: bool = True, # noqa: FBT001, FBT002
178 ensure_exists: bool = False, # noqa: FBT001, FBT002
179) -> str:
180 """
181 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
182 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
183 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
184 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
185 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
186 :returns: cache directory tied to the user
187 """
188 return PlatformDirs(
189 appname=appname,
190 appauthor=appauthor,
191 version=version,
192 opinion=opinion,
193 ensure_exists=ensure_exists,
194 ).site_cache_dir
195
196
197def user_state_dir(
198 appname: str | None = None,
199 appauthor: str | None | Literal[False] = None,
200 version: str | None = None,
201 roaming: bool = False, # noqa: FBT001, FBT002
202 ensure_exists: bool = False, # noqa: FBT001, FBT002
203) -> str:
204 """
205 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
206 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
207 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
208 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
209 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
210 :returns: state directory tied to the user
211 """
212 return PlatformDirs(
213 appname=appname,
214 appauthor=appauthor,
215 version=version,
216 roaming=roaming,
217 ensure_exists=ensure_exists,
218 ).user_state_dir
219
220
221def user_log_dir(
222 appname: str | None = None,
223 appauthor: str | None | Literal[False] = None,
224 version: str | None = None,
225 opinion: bool = True, # noqa: FBT001, FBT002
226 ensure_exists: bool = False, # noqa: FBT001, FBT002
227) -> str:
228 """
229 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
230 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
231 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
232 :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
233 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
234 :returns: log directory tied to the user
235 """
236 return PlatformDirs(
237 appname=appname,
238 appauthor=appauthor,
239 version=version,
240 opinion=opinion,
241 ensure_exists=ensure_exists,
242 ).user_log_dir
243
244
245def user_documents_dir() -> str:
246 """:returns: documents directory tied to the user"""
247 return PlatformDirs().user_documents_dir
248
249
250def user_downloads_dir() -> str:
251 """:returns: downloads directory tied to the user"""
252 return PlatformDirs().user_downloads_dir
253
254
255def user_pictures_dir() -> str:
256 """:returns: pictures directory tied to the user"""
257 return PlatformDirs().user_pictures_dir
258
259
260def user_videos_dir() -> str:
261 """:returns: videos directory tied to the user"""
262 return PlatformDirs().user_videos_dir
263
264
265def user_music_dir() -> str:
266 """:returns: music directory tied to the user"""
267 return PlatformDirs().user_music_dir
268
269
270def user_desktop_dir() -> str:
271 """:returns: desktop directory tied to the user"""
272 return PlatformDirs().user_desktop_dir
273
274
275def user_runtime_dir(
276 appname: str | None = None,
277 appauthor: str | None | Literal[False] = None,
278 version: str | None = None,
279 opinion: bool = True, # noqa: FBT001, FBT002
280 ensure_exists: bool = False, # noqa: FBT001, FBT002
281) -> str:
282 """
283 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
284 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
285 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
286 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
287 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
288 :returns: runtime directory tied to the user
289 """
290 return PlatformDirs(
291 appname=appname,
292 appauthor=appauthor,
293 version=version,
294 opinion=opinion,
295 ensure_exists=ensure_exists,
296 ).user_runtime_dir
297
298
299def site_runtime_dir(
300 appname: str | None = None,
301 appauthor: str | None | Literal[False] = None,
302 version: str | None = None,
303 opinion: bool = True, # noqa: FBT001, FBT002
304 ensure_exists: bool = False, # noqa: FBT001, FBT002
305) -> str:
306 """
307 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
308 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
309 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
310 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
311 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
312 :returns: runtime directory shared by users
313 """
314 return PlatformDirs(
315 appname=appname,
316 appauthor=appauthor,
317 version=version,
318 opinion=opinion,
319 ensure_exists=ensure_exists,
320 ).site_runtime_dir
321
322
323def user_data_path(
324 appname: str | None = None,
325 appauthor: str | None | Literal[False] = None,
326 version: str | None = None,
327 roaming: bool = False, # noqa: FBT001, FBT002
328 ensure_exists: bool = False, # noqa: FBT001, FBT002
329) -> Path:
330 """
331 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
332 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
333 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
334 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
335 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
336 :returns: data path tied to the user
337 """
338 return PlatformDirs(
339 appname=appname,
340 appauthor=appauthor,
341 version=version,
342 roaming=roaming,
343 ensure_exists=ensure_exists,
344 ).user_data_path
345
346
347def site_data_path(
348 appname: str | None = None,
349 appauthor: str | None | Literal[False] = None,
350 version: str | None = None,
351 multipath: bool = False, # noqa: FBT001, FBT002
352 ensure_exists: bool = False, # noqa: FBT001, FBT002
353) -> Path:
354 """
355 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
356 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
357 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
358 :param multipath: See `multipath <platformdirs.api.PlatformDirsABC.multipath>`.
359 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
360 :returns: data path shared by users
361 """
362 return PlatformDirs(
363 appname=appname,
364 appauthor=appauthor,
365 version=version,
366 multipath=multipath,
367 ensure_exists=ensure_exists,
368 ).site_data_path
369
370
371def user_config_path(
372 appname: str | None = None,
373 appauthor: str | None | Literal[False] = None,
374 version: str | None = None,
375 roaming: bool = False, # noqa: FBT001, FBT002
376 ensure_exists: bool = False, # noqa: FBT001, FBT002
377) -> Path:
378 """
379 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
380 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
381 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
382 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
383 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
384 :returns: config path tied to the user
385 """
386 return PlatformDirs(
387 appname=appname,
388 appauthor=appauthor,
389 version=version,
390 roaming=roaming,
391 ensure_exists=ensure_exists,
392 ).user_config_path
393
394
395def site_config_path(
396 appname: str | None = None,
397 appauthor: str | None | Literal[False] = None,
398 version: str | None = None,
399 multipath: bool = False, # noqa: FBT001, FBT002
400 ensure_exists: bool = False, # noqa: FBT001, FBT002
401) -> Path:
402 """
403 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
404 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
405 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
406 :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
407 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
408 :returns: config path shared by the users
409 """
410 return PlatformDirs(
411 appname=appname,
412 appauthor=appauthor,
413 version=version,
414 multipath=multipath,
415 ensure_exists=ensure_exists,
416 ).site_config_path
417
418
419def site_cache_path(
420 appname: str | None = None,
421 appauthor: str | None | Literal[False] = None,
422 version: str | None = None,
423 opinion: bool = True, # noqa: FBT001, FBT002
424 ensure_exists: bool = False, # noqa: FBT001, FBT002
425) -> Path:
426 """
427 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
428 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
429 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
430 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
431 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
432 :returns: cache directory tied to the user
433 """
434 return PlatformDirs(
435 appname=appname,
436 appauthor=appauthor,
437 version=version,
438 opinion=opinion,
439 ensure_exists=ensure_exists,
440 ).site_cache_path
441
442
443def user_cache_path(
444 appname: str | None = None,
445 appauthor: str | None | Literal[False] = None,
446 version: str | None = None,
447 opinion: bool = True, # noqa: FBT001, FBT002
448 ensure_exists: bool = False, # noqa: FBT001, FBT002
449) -> Path:
450 """
451 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
452 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
453 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
454 :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
455 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
456 :returns: cache path tied to the user
457 """
458 return PlatformDirs(
459 appname=appname,
460 appauthor=appauthor,
461 version=version,
462 opinion=opinion,
463 ensure_exists=ensure_exists,
464 ).user_cache_path
465
466
467def user_state_path(
468 appname: str | None = None,
469 appauthor: str | None | Literal[False] = None,
470 version: str | None = None,
471 roaming: bool = False, # noqa: FBT001, FBT002
472 ensure_exists: bool = False, # noqa: FBT001, FBT002
473) -> Path:
474 """
475 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
476 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
477 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
478 :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
479 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
480 :returns: state path tied to the user
481 """
482 return PlatformDirs(
483 appname=appname,
484 appauthor=appauthor,
485 version=version,
486 roaming=roaming,
487 ensure_exists=ensure_exists,
488 ).user_state_path
489
490
491def user_log_path(
492 appname: str | None = None,
493 appauthor: str | None | Literal[False] = None,
494 version: str | None = None,
495 opinion: bool = True, # noqa: FBT001, FBT002
496 ensure_exists: bool = False, # noqa: FBT001, FBT002
497) -> Path:
498 """
499 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
500 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
501 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
502 :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
503 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
504 :returns: log path tied to the user
505 """
506 return PlatformDirs(
507 appname=appname,
508 appauthor=appauthor,
509 version=version,
510 opinion=opinion,
511 ensure_exists=ensure_exists,
512 ).user_log_path
513
514
515def user_documents_path() -> Path:
516 """:returns: documents a path tied to the user"""
517 return PlatformDirs().user_documents_path
518
519
520def user_downloads_path() -> Path:
521 """:returns: downloads path tied to the user"""
522 return PlatformDirs().user_downloads_path
523
524
525def user_pictures_path() -> Path:
526 """:returns: pictures path tied to the user"""
527 return PlatformDirs().user_pictures_path
528
529
530def user_videos_path() -> Path:
531 """:returns: videos path tied to the user"""
532 return PlatformDirs().user_videos_path
533
534
535def user_music_path() -> Path:
536 """:returns: music path tied to the user"""
537 return PlatformDirs().user_music_path
538
539
540def user_desktop_path() -> Path:
541 """:returns: desktop path tied to the user"""
542 return PlatformDirs().user_desktop_path
543
544
545def user_runtime_path(
546 appname: str | None = None,
547 appauthor: str | None | Literal[False] = None,
548 version: str | None = None,
549 opinion: bool = True, # noqa: FBT001, FBT002
550 ensure_exists: bool = False, # noqa: FBT001, FBT002
551) -> Path:
552 """
553 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
554 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
555 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
556 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
557 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
558 :returns: runtime path tied to the user
559 """
560 return PlatformDirs(
561 appname=appname,
562 appauthor=appauthor,
563 version=version,
564 opinion=opinion,
565 ensure_exists=ensure_exists,
566 ).user_runtime_path
567
568
569def site_runtime_path(
570 appname: str | None = None,
571 appauthor: str | None | Literal[False] = None,
572 version: str | None = None,
573 opinion: bool = True, # noqa: FBT001, FBT002
574 ensure_exists: bool = False, # noqa: FBT001, FBT002
575) -> Path:
576 """
577 :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
578 :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
579 :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
580 :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
581 :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
582 :returns: runtime path shared by users
583 """
584 return PlatformDirs(
585 appname=appname,
586 appauthor=appauthor,
587 version=version,
588 opinion=opinion,
589 ensure_exists=ensure_exists,
590 ).site_runtime_path
591
592
593__all__ = [
594 "AppDirs",
595 "PlatformDirs",
596 "PlatformDirsABC",
597 "__version__",
598 "__version_info__",
599 "site_cache_dir",
600 "site_cache_path",
601 "site_config_dir",
602 "site_config_path",
603 "site_data_dir",
604 "site_data_path",
605 "site_runtime_dir",
606 "site_runtime_path",
607 "user_cache_dir",
608 "user_cache_path",
609 "user_config_dir",
610 "user_config_path",
611 "user_data_dir",
612 "user_data_path",
613 "user_desktop_dir",
614 "user_desktop_path",
615 "user_documents_dir",
616 "user_documents_path",
617 "user_downloads_dir",
618 "user_downloads_path",
619 "user_log_dir",
620 "user_log_path",
621 "user_music_dir",
622 "user_music_path",
623 "user_pictures_dir",
624 "user_pictures_path",
625 "user_runtime_dir",
626 "user_runtime_path",
627 "user_state_dir",
628 "user_state_path",
629 "user_videos_dir",
630 "user_videos_path",
631]