__init__.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. """
  2. Utilities for determining application-specific dirs. See <https://github.com/platformdirs/platformdirs> for details and
  3. usage.
  4. """
  5. from __future__ import annotations
  6. import os
  7. import sys
  8. from typing import TYPE_CHECKING
  9. from .api import PlatformDirsABC
  10. from .version import __version__
  11. from .version import __version_tuple__ as __version_info__
  12. if TYPE_CHECKING:
  13. from pathlib import Path
  14. if sys.version_info >= (3, 8): # pragma: no cover (py38+)
  15. from typing import Literal
  16. else: # pragma: no cover (py38+)
  17. from typing_extensions import Literal
  18. def _set_platform_dir_class() -> type[PlatformDirsABC]:
  19. if sys.platform == "win32":
  20. from platformdirs.windows import Windows as Result
  21. elif sys.platform == "darwin":
  22. from platformdirs.macos import MacOS as Result
  23. else:
  24. from platformdirs.unix import Unix as Result
  25. if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
  26. if os.getenv("SHELL") or os.getenv("PREFIX"):
  27. return Result
  28. from platformdirs.android import _android_folder
  29. if _android_folder() is not None:
  30. from platformdirs.android import Android
  31. return Android # return to avoid redefinition of result
  32. return Result
  33. PlatformDirs = _set_platform_dir_class() #: Currently active platform
  34. AppDirs = PlatformDirs #: Backwards compatibility with appdirs
  35. def user_data_dir(
  36. appname: str | None = None,
  37. appauthor: str | None | Literal[False] = None,
  38. version: str | None = None,
  39. roaming: bool = False, # noqa: FBT001, FBT002
  40. ensure_exists: bool = False, # noqa: FBT001, FBT002
  41. ) -> str:
  42. """
  43. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  44. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  45. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  46. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  47. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  48. :returns: data directory tied to the user
  49. """
  50. return PlatformDirs(
  51. appname=appname,
  52. appauthor=appauthor,
  53. version=version,
  54. roaming=roaming,
  55. ensure_exists=ensure_exists,
  56. ).user_data_dir
  57. def site_data_dir(
  58. appname: str | None = None,
  59. appauthor: str | None | Literal[False] = None,
  60. version: str | None = None,
  61. multipath: bool = False, # noqa: FBT001, FBT002
  62. ensure_exists: bool = False, # noqa: FBT001, FBT002
  63. ) -> str:
  64. """
  65. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  66. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  67. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  68. :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  69. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  70. :returns: data directory shared by users
  71. """
  72. return PlatformDirs(
  73. appname=appname,
  74. appauthor=appauthor,
  75. version=version,
  76. multipath=multipath,
  77. ensure_exists=ensure_exists,
  78. ).site_data_dir
  79. def user_config_dir(
  80. appname: str | None = None,
  81. appauthor: str | None | Literal[False] = None,
  82. version: str | None = None,
  83. roaming: bool = False, # noqa: FBT001, FBT002
  84. ensure_exists: bool = False, # noqa: FBT001, FBT002
  85. ) -> str:
  86. """
  87. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  88. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  89. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  90. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  91. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  92. :returns: config directory tied to the user
  93. """
  94. return PlatformDirs(
  95. appname=appname,
  96. appauthor=appauthor,
  97. version=version,
  98. roaming=roaming,
  99. ensure_exists=ensure_exists,
  100. ).user_config_dir
  101. def site_config_dir(
  102. appname: str | None = None,
  103. appauthor: str | None | Literal[False] = None,
  104. version: str | None = None,
  105. multipath: 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 multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  113. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  114. :returns: config directory shared by the users
  115. """
  116. return PlatformDirs(
  117. appname=appname,
  118. appauthor=appauthor,
  119. version=version,
  120. multipath=multipath,
  121. ensure_exists=ensure_exists,
  122. ).site_config_dir
  123. def user_cache_dir(
  124. appname: str | None = None,
  125. appauthor: str | None | Literal[False] = None,
  126. version: str | None = None,
  127. opinion: bool = True, # noqa: FBT001, FBT002
  128. ensure_exists: bool = False, # noqa: FBT001, FBT002
  129. ) -> str:
  130. """
  131. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  132. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  133. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  134. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  135. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  136. :returns: cache directory tied to the user
  137. """
  138. return PlatformDirs(
  139. appname=appname,
  140. appauthor=appauthor,
  141. version=version,
  142. opinion=opinion,
  143. ensure_exists=ensure_exists,
  144. ).user_cache_dir
  145. def site_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 `opinion <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. ).site_cache_dir
  167. def user_state_dir(
  168. appname: str | None = None,
  169. appauthor: str | None | Literal[False] = None,
  170. version: str | None = None,
  171. roaming: bool = False, # noqa: FBT001, FBT002
  172. ensure_exists: bool = False, # noqa: FBT001, FBT002
  173. ) -> str:
  174. """
  175. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  176. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  177. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  178. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  179. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  180. :returns: state directory tied to the user
  181. """
  182. return PlatformDirs(
  183. appname=appname,
  184. appauthor=appauthor,
  185. version=version,
  186. roaming=roaming,
  187. ensure_exists=ensure_exists,
  188. ).user_state_dir
  189. def user_log_dir(
  190. appname: str | None = None,
  191. appauthor: str | None | Literal[False] = None,
  192. version: str | None = None,
  193. opinion: bool = True, # noqa: FBT001, FBT002
  194. ensure_exists: bool = False, # noqa: FBT001, FBT002
  195. ) -> str:
  196. """
  197. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  198. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  199. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  200. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  201. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  202. :returns: log directory tied to the user
  203. """
  204. return PlatformDirs(
  205. appname=appname,
  206. appauthor=appauthor,
  207. version=version,
  208. opinion=opinion,
  209. ensure_exists=ensure_exists,
  210. ).user_log_dir
  211. def user_documents_dir() -> str:
  212. """:returns: documents directory tied to the user"""
  213. return PlatformDirs().user_documents_dir
  214. def user_downloads_dir() -> str:
  215. """:returns: downloads directory tied to the user"""
  216. return PlatformDirs().user_downloads_dir
  217. def user_pictures_dir() -> str:
  218. """:returns: pictures directory tied to the user"""
  219. return PlatformDirs().user_pictures_dir
  220. def user_videos_dir() -> str:
  221. """:returns: videos directory tied to the user"""
  222. return PlatformDirs().user_videos_dir
  223. def user_music_dir() -> str:
  224. """:returns: music directory tied to the user"""
  225. return PlatformDirs().user_music_dir
  226. def user_runtime_dir(
  227. appname: str | None = None,
  228. appauthor: str | None | Literal[False] = None,
  229. version: str | None = None,
  230. opinion: bool = True, # noqa: FBT001, FBT002
  231. ensure_exists: bool = False, # noqa: FBT001, FBT002
  232. ) -> str:
  233. """
  234. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  235. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  236. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  237. :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
  238. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  239. :returns: runtime directory tied to the user
  240. """
  241. return PlatformDirs(
  242. appname=appname,
  243. appauthor=appauthor,
  244. version=version,
  245. opinion=opinion,
  246. ensure_exists=ensure_exists,
  247. ).user_runtime_dir
  248. def user_data_path(
  249. appname: str | None = None,
  250. appauthor: str | None | Literal[False] = None,
  251. version: str | None = None,
  252. roaming: bool = False, # noqa: FBT001, FBT002
  253. ensure_exists: bool = False, # noqa: FBT001, FBT002
  254. ) -> Path:
  255. """
  256. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  257. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  258. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  259. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  260. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  261. :returns: data path tied to the user
  262. """
  263. return PlatformDirs(
  264. appname=appname,
  265. appauthor=appauthor,
  266. version=version,
  267. roaming=roaming,
  268. ensure_exists=ensure_exists,
  269. ).user_data_path
  270. def site_data_path(
  271. appname: str | None = None,
  272. appauthor: str | None | Literal[False] = None,
  273. version: str | None = None,
  274. multipath: bool = False, # noqa: FBT001, FBT002
  275. ensure_exists: bool = False, # noqa: FBT001, FBT002
  276. ) -> Path:
  277. """
  278. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  279. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  280. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  281. :param multipath: See `multipath <platformdirs.api.PlatformDirsABC.multipath>`.
  282. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  283. :returns: data path shared by users
  284. """
  285. return PlatformDirs(
  286. appname=appname,
  287. appauthor=appauthor,
  288. version=version,
  289. multipath=multipath,
  290. ensure_exists=ensure_exists,
  291. ).site_data_path
  292. def user_config_path(
  293. appname: str | None = None,
  294. appauthor: str | None | Literal[False] = None,
  295. version: str | None = None,
  296. roaming: bool = False, # noqa: FBT001, FBT002
  297. ensure_exists: bool = False, # noqa: FBT001, FBT002
  298. ) -> Path:
  299. """
  300. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  301. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  302. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  303. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  304. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  305. :returns: config path tied to the user
  306. """
  307. return PlatformDirs(
  308. appname=appname,
  309. appauthor=appauthor,
  310. version=version,
  311. roaming=roaming,
  312. ensure_exists=ensure_exists,
  313. ).user_config_path
  314. def site_config_path(
  315. appname: str | None = None,
  316. appauthor: str | None | Literal[False] = None,
  317. version: str | None = None,
  318. multipath: bool = False, # noqa: FBT001, FBT002
  319. ensure_exists: bool = False, # noqa: FBT001, FBT002
  320. ) -> Path:
  321. """
  322. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  323. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  324. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  325. :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  326. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  327. :returns: config path shared by the users
  328. """
  329. return PlatformDirs(
  330. appname=appname,
  331. appauthor=appauthor,
  332. version=version,
  333. multipath=multipath,
  334. ensure_exists=ensure_exists,
  335. ).site_config_path
  336. def site_cache_path(
  337. appname: str | None = None,
  338. appauthor: str | None | Literal[False] = None,
  339. version: str | None = None,
  340. opinion: bool = True, # noqa: FBT001, FBT002
  341. ensure_exists: bool = False, # noqa: FBT001, FBT002
  342. ) -> Path:
  343. """
  344. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  345. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  346. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  347. :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
  348. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  349. :returns: cache directory tied to the user
  350. """
  351. return PlatformDirs(
  352. appname=appname,
  353. appauthor=appauthor,
  354. version=version,
  355. opinion=opinion,
  356. ensure_exists=ensure_exists,
  357. ).site_cache_path
  358. def user_cache_path(
  359. appname: str | None = None,
  360. appauthor: str | None | Literal[False] = None,
  361. version: str | None = None,
  362. opinion: bool = True, # noqa: FBT001, FBT002
  363. ensure_exists: bool = False, # noqa: FBT001, FBT002
  364. ) -> Path:
  365. """
  366. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  367. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  368. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  369. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  370. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  371. :returns: cache path tied to the user
  372. """
  373. return PlatformDirs(
  374. appname=appname,
  375. appauthor=appauthor,
  376. version=version,
  377. opinion=opinion,
  378. ensure_exists=ensure_exists,
  379. ).user_cache_path
  380. def user_state_path(
  381. appname: str | None = None,
  382. appauthor: str | None | Literal[False] = None,
  383. version: str | None = None,
  384. roaming: bool = False, # noqa: FBT001, FBT002
  385. ensure_exists: bool = False, # noqa: FBT001, FBT002
  386. ) -> Path:
  387. """
  388. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  389. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  390. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  391. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.roaming>`.
  392. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  393. :returns: state path tied to the user
  394. """
  395. return PlatformDirs(
  396. appname=appname,
  397. appauthor=appauthor,
  398. version=version,
  399. roaming=roaming,
  400. ensure_exists=ensure_exists,
  401. ).user_state_path
  402. def user_log_path(
  403. appname: str | None = None,
  404. appauthor: str | None | Literal[False] = None,
  405. version: str | None = None,
  406. opinion: bool = True, # noqa: FBT001, FBT002
  407. ensure_exists: bool = False, # noqa: FBT001, FBT002
  408. ) -> Path:
  409. """
  410. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  411. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  412. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  413. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  414. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  415. :returns: log path tied to the user
  416. """
  417. return PlatformDirs(
  418. appname=appname,
  419. appauthor=appauthor,
  420. version=version,
  421. opinion=opinion,
  422. ensure_exists=ensure_exists,
  423. ).user_log_path
  424. def user_documents_path() -> Path:
  425. """:returns: documents path tied to the user"""
  426. return PlatformDirs().user_documents_path
  427. def user_downloads_path() -> Path:
  428. """:returns: downloads path tied to the user"""
  429. return PlatformDirs().user_downloads_path
  430. def user_pictures_path() -> Path:
  431. """:returns: pictures path tied to the user"""
  432. return PlatformDirs().user_pictures_path
  433. def user_videos_path() -> Path:
  434. """:returns: videos path tied to the user"""
  435. return PlatformDirs().user_videos_path
  436. def user_music_path() -> Path:
  437. """:returns: music path tied to the user"""
  438. return PlatformDirs().user_music_path
  439. def user_runtime_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 `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
  451. :param ensure_exists: See `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  452. :returns: runtime 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_runtime_path
  461. __all__ = [
  462. "__version__",
  463. "__version_info__",
  464. "PlatformDirs",
  465. "AppDirs",
  466. "PlatformDirsABC",
  467. "user_data_dir",
  468. "user_config_dir",
  469. "user_cache_dir",
  470. "user_state_dir",
  471. "user_log_dir",
  472. "user_documents_dir",
  473. "user_downloads_dir",
  474. "user_pictures_dir",
  475. "user_videos_dir",
  476. "user_music_dir",
  477. "user_runtime_dir",
  478. "site_data_dir",
  479. "site_config_dir",
  480. "site_cache_dir",
  481. "user_data_path",
  482. "user_config_path",
  483. "user_cache_path",
  484. "user_state_path",
  485. "user_log_path",
  486. "user_documents_path",
  487. "user_downloads_path",
  488. "user_pictures_path",
  489. "user_videos_path",
  490. "user_music_path",
  491. "user_runtime_path",
  492. "site_data_path",
  493. "site_config_path",
  494. "site_cache_path",
  495. ]