__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 importlib
  7. import os
  8. import sys
  9. from pathlib import Path
  10. from typing import TYPE_CHECKING
  11. if TYPE_CHECKING:
  12. from typing_extensions import Literal # pragma: no cover
  13. from .api import PlatformDirsABC
  14. from .version import __version__, __version_info__
  15. def _set_platform_dir_class() -> type[PlatformDirsABC]:
  16. if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
  17. module, name = "platformdirs.android", "Android"
  18. elif sys.platform == "win32":
  19. module, name = "platformdirs.windows", "Windows"
  20. elif sys.platform == "darwin":
  21. module, name = "platformdirs.macos", "MacOS"
  22. else:
  23. module, name = "platformdirs.unix", "Unix"
  24. result: type[PlatformDirsABC] = getattr(importlib.import_module(module), name)
  25. return result
  26. PlatformDirs = _set_platform_dir_class() #: Currently active platform
  27. AppDirs = PlatformDirs #: Backwards compatibility with appdirs
  28. def user_data_dir(
  29. appname: str | None = None,
  30. appauthor: str | None | Literal[False] = None,
  31. version: str | None = None,
  32. roaming: bool = False,
  33. ) -> str:
  34. """
  35. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  36. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  37. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  38. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
  39. :returns: data directory tied to the user
  40. """
  41. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_dir
  42. def site_data_dir(
  43. appname: str | None = None,
  44. appauthor: str | None | Literal[False] = None,
  45. version: str | None = None,
  46. multipath: bool = False,
  47. ) -> str:
  48. """
  49. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  50. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  51. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  52. :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  53. :returns: data directory shared by users
  54. """
  55. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_dir
  56. def user_config_dir(
  57. appname: str | None = None,
  58. appauthor: str | None | Literal[False] = None,
  59. version: str | None = None,
  60. roaming: bool = False,
  61. ) -> str:
  62. """
  63. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  64. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  65. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  66. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
  67. :returns: config directory tied to the user
  68. """
  69. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_config_dir
  70. def site_config_dir(
  71. appname: str | None = None,
  72. appauthor: str | None | Literal[False] = None,
  73. version: str | None = None,
  74. multipath: bool = False,
  75. ) -> str:
  76. """
  77. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  78. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  79. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  80. :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  81. :returns: config directory shared by the users
  82. """
  83. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_config_dir
  84. def user_cache_dir(
  85. appname: str | None = None,
  86. appauthor: str | None | Literal[False] = None,
  87. version: str | None = None,
  88. opinion: bool = True,
  89. ) -> str:
  90. """
  91. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  92. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  93. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  94. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  95. :returns: cache directory tied to the user
  96. """
  97. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_cache_dir
  98. def user_state_dir(
  99. appname: str | None = None,
  100. appauthor: str | None | Literal[False] = None,
  101. version: str | None = None,
  102. roaming: bool = False,
  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.version>`.
  109. :returns: state directory tied to the user
  110. """
  111. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_state_dir
  112. def user_log_dir(
  113. appname: str | None = None,
  114. appauthor: str | None | Literal[False] = None,
  115. version: str | None = None,
  116. opinion: bool = True,
  117. ) -> str:
  118. """
  119. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  120. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  121. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  122. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  123. :returns: log directory tied to the user
  124. """
  125. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_dir
  126. def user_documents_dir() -> str:
  127. """
  128. :returns: documents directory tied to the user
  129. """
  130. return PlatformDirs().user_documents_dir
  131. def user_runtime_dir(
  132. appname: str | None = None,
  133. appauthor: str | None | Literal[False] = None,
  134. version: str | None = None,
  135. opinion: bool = True,
  136. ) -> str:
  137. """
  138. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  139. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  140. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  141. :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
  142. :returns: runtime directory tied to the user
  143. """
  144. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_runtime_dir
  145. def user_data_path(
  146. appname: str | None = None,
  147. appauthor: str | None | Literal[False] = None,
  148. version: str | None = None,
  149. roaming: bool = False,
  150. ) -> Path:
  151. """
  152. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  153. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  154. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  155. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
  156. :returns: data path tied to the user
  157. """
  158. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_path
  159. def site_data_path(
  160. appname: str | None = None,
  161. appauthor: str | None | Literal[False] = None,
  162. version: str | None = None,
  163. multipath: bool = False,
  164. ) -> Path:
  165. """
  166. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  167. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  168. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  169. :param multipath: See `multipath <platformdirs.api.PlatformDirsABC.multipath>`.
  170. :returns: data path shared by users
  171. """
  172. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_path
  173. def user_config_path(
  174. appname: str | None = None,
  175. appauthor: str | None | Literal[False] = None,
  176. version: str | None = None,
  177. roaming: bool = False,
  178. ) -> Path:
  179. """
  180. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  181. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  182. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  183. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
  184. :returns: config path tied to the user
  185. """
  186. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_config_path
  187. def site_config_path(
  188. appname: str | None = None,
  189. appauthor: str | None | Literal[False] = None,
  190. version: str | None = None,
  191. multipath: bool = False,
  192. ) -> Path:
  193. """
  194. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  195. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  196. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  197. :param multipath: See `roaming <platformdirs.api.PlatformDirsABC.multipath>`.
  198. :returns: config path shared by the users
  199. """
  200. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_config_path
  201. def user_cache_path(
  202. appname: str | None = None,
  203. appauthor: str | None | Literal[False] = None,
  204. version: str | None = None,
  205. opinion: bool = True,
  206. ) -> Path:
  207. """
  208. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  209. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  210. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  211. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  212. :returns: cache path tied to the user
  213. """
  214. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_cache_path
  215. def user_state_path(
  216. appname: str | None = None,
  217. appauthor: str | None | Literal[False] = None,
  218. version: str | None = None,
  219. roaming: bool = False,
  220. ) -> Path:
  221. """
  222. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  223. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  224. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  225. :param roaming: See `roaming <platformdirs.api.PlatformDirsABC.version>`.
  226. :returns: state path tied to the user
  227. """
  228. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_state_path
  229. def user_log_path(
  230. appname: str | None = None,
  231. appauthor: str | None | Literal[False] = None,
  232. version: str | None = None,
  233. opinion: bool = True,
  234. ) -> Path:
  235. """
  236. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  237. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  238. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  239. :param opinion: See `roaming <platformdirs.api.PlatformDirsABC.opinion>`.
  240. :returns: log path tied to the user
  241. """
  242. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_log_path
  243. def user_documents_path() -> Path:
  244. """
  245. :returns: documents path tied to the user
  246. """
  247. return PlatformDirs().user_documents_path
  248. def user_runtime_path(
  249. appname: str | None = None,
  250. appauthor: str | None | Literal[False] = None,
  251. version: str | None = None,
  252. opinion: bool = True,
  253. ) -> Path:
  254. """
  255. :param appname: See `appname <platformdirs.api.PlatformDirsABC.appname>`.
  256. :param appauthor: See `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`.
  257. :param version: See `version <platformdirs.api.PlatformDirsABC.version>`.
  258. :param opinion: See `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
  259. :returns: runtime path tied to the user
  260. """
  261. return PlatformDirs(appname=appname, appauthor=appauthor, version=version, opinion=opinion).user_runtime_path
  262. __all__ = [
  263. "__version__",
  264. "__version_info__",
  265. "PlatformDirs",
  266. "AppDirs",
  267. "PlatformDirsABC",
  268. "user_data_dir",
  269. "user_config_dir",
  270. "user_cache_dir",
  271. "user_state_dir",
  272. "user_log_dir",
  273. "user_documents_dir",
  274. "user_runtime_dir",
  275. "site_data_dir",
  276. "site_config_dir",
  277. "user_data_path",
  278. "user_config_path",
  279. "user_cache_path",
  280. "user_state_path",
  281. "user_log_path",
  282. "user_documents_path",
  283. "user_runtime_path",
  284. "site_data_path",
  285. "site_config_path",
  286. ]