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