api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. """Base API."""
  2. from __future__ import annotations
  3. import os
  4. from abc import ABC, abstractmethod
  5. from pathlib import Path
  6. from typing import TYPE_CHECKING
  7. if TYPE_CHECKING:
  8. from collections.abc import Iterator
  9. from typing import Literal
  10. class PlatformDirsABC(ABC): # noqa: PLR0904
  11. """Abstract base class defining all platform directory properties, their :class:`~pathlib.Path` variants, and iterators.
  12. Platform-specific subclasses (e.g. :class:`~platformdirs.windows.Windows`, :class:`~platformdirs.macos.MacOS`,
  13. :class:`~platformdirs.unix.Unix`) implement the abstract properties to return the appropriate paths for each
  14. operating system.
  15. """
  16. def __init__( # noqa: PLR0913, PLR0917
  17. self,
  18. appname: str | None = None,
  19. appauthor: str | Literal[False] | None = None,
  20. version: str | None = None,
  21. roaming: bool = False, # noqa: FBT001, FBT002
  22. multipath: bool = False, # noqa: FBT001, FBT002
  23. opinion: bool = True, # noqa: FBT001, FBT002
  24. ensure_exists: bool = False, # noqa: FBT001, FBT002
  25. use_site_for_root: bool = False, # noqa: FBT001, FBT002
  26. ) -> None:
  27. """Create a new platform directory.
  28. :param appname: See `appname`.
  29. :param appauthor: See `appauthor`.
  30. :param version: See `version`.
  31. :param roaming: See `roaming`.
  32. :param multipath: See `multipath`.
  33. :param opinion: See `opinion`.
  34. :param ensure_exists: See `ensure_exists`.
  35. :param use_site_for_root: See `use_site_for_root`.
  36. """
  37. self.appname = appname #: The name of the application.
  38. self.appauthor = appauthor
  39. """The name of the app author or distributing body for this application.
  40. Typically, it is the owning company name. Defaults to `appname`. You may pass ``False`` to disable it.
  41. """
  42. self.version = version
  43. """An optional version path element to append to the path.
  44. You might want to use this if you want multiple versions of your app to be able to run independently. If used,
  45. this would typically be ``<major>.<minor>``.
  46. """
  47. self.roaming = roaming
  48. """Whether to use the roaming appdata directory on Windows.
  49. That means that for users on a Windows network setup for roaming profiles, this user data will be synced on
  50. login (see `here <https://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>`_).
  51. """
  52. self.multipath = multipath
  53. """An optional parameter which indicates that the entire list of data dirs should be returned.
  54. By default, the first item would only be returned. Only affects ``site_data_dir`` and ``site_config_dir`` on
  55. Unix and macOS.
  56. """
  57. self.opinion = opinion
  58. """Whether to use opinionated values.
  59. When enabled, appends an additional subdirectory for certain directories: e.g. ``Cache`` for cache and ``Logs``
  60. for logs on Windows, ``log`` for logs on Unix.
  61. """
  62. self.ensure_exists = ensure_exists
  63. """Optionally create the directory (and any missing parents) upon access if it does not exist.
  64. By default, no directories are created.
  65. """
  66. self.use_site_for_root = use_site_for_root
  67. """Whether to redirect ``user_*_dir`` calls to their ``site_*_dir`` equivalents when running as root (uid 0).
  68. Only has an effect on Unix. Disabled by default for backwards compatibility. When enabled, XDG user environment
  69. variables (e.g. ``XDG_DATA_HOME``) are bypassed for the redirected directories.
  70. """
  71. def _append_app_name_and_version(self, *base: str) -> str:
  72. params = list(base[1:])
  73. if self.appname:
  74. params.append(self.appname)
  75. if self.version:
  76. params.append(self.version)
  77. path = os.path.join(base[0], *params) # noqa: PTH118
  78. self._optionally_create_directory(path)
  79. return path
  80. def _optionally_create_directory(self, path: str) -> None:
  81. if self.ensure_exists:
  82. Path(path).mkdir(parents=True, exist_ok=True)
  83. def _first_item_as_path_if_multipath(self, directory: str) -> Path:
  84. if self.multipath:
  85. # If multipath is True, the first path is returned.
  86. directory = directory.partition(os.pathsep)[0]
  87. return Path(directory)
  88. @property
  89. @abstractmethod
  90. def user_data_dir(self) -> str:
  91. """:returns: data directory tied to the user"""
  92. @property
  93. @abstractmethod
  94. def site_data_dir(self) -> str:
  95. """:returns: data directory shared by users"""
  96. @property
  97. @abstractmethod
  98. def user_config_dir(self) -> str:
  99. """:returns: config directory tied to the user"""
  100. @property
  101. @abstractmethod
  102. def site_config_dir(self) -> str:
  103. """:returns: config directory shared by users"""
  104. @property
  105. @abstractmethod
  106. def user_cache_dir(self) -> str:
  107. """:returns: cache directory tied to the user"""
  108. @property
  109. @abstractmethod
  110. def site_cache_dir(self) -> str:
  111. """:returns: cache directory shared by users"""
  112. @property
  113. @abstractmethod
  114. def user_state_dir(self) -> str:
  115. """:returns: state directory tied to the user"""
  116. @property
  117. @abstractmethod
  118. def site_state_dir(self) -> str:
  119. """:returns: state directory shared by users"""
  120. @property
  121. @abstractmethod
  122. def user_log_dir(self) -> str:
  123. """:returns: log directory tied to the user"""
  124. @property
  125. @abstractmethod
  126. def site_log_dir(self) -> str:
  127. """:returns: log directory shared by users"""
  128. @property
  129. @abstractmethod
  130. def user_documents_dir(self) -> str:
  131. """:returns: documents directory tied to the user"""
  132. @property
  133. @abstractmethod
  134. def user_downloads_dir(self) -> str:
  135. """:returns: downloads directory tied to the user"""
  136. @property
  137. @abstractmethod
  138. def user_pictures_dir(self) -> str:
  139. """:returns: pictures directory tied to the user"""
  140. @property
  141. @abstractmethod
  142. def user_videos_dir(self) -> str:
  143. """:returns: videos directory tied to the user"""
  144. @property
  145. @abstractmethod
  146. def user_music_dir(self) -> str:
  147. """:returns: music directory tied to the user"""
  148. @property
  149. @abstractmethod
  150. def user_desktop_dir(self) -> str:
  151. """:returns: desktop directory tied to the user"""
  152. @property
  153. @abstractmethod
  154. def user_bin_dir(self) -> str:
  155. """:returns: bin directory tied to the user"""
  156. @property
  157. @abstractmethod
  158. def site_bin_dir(self) -> str:
  159. """:returns: bin directory shared by users"""
  160. @property
  161. @abstractmethod
  162. def user_applications_dir(self) -> str:
  163. """:returns: applications directory tied to the user"""
  164. @property
  165. @abstractmethod
  166. def site_applications_dir(self) -> str:
  167. """:returns: applications directory shared by users"""
  168. @property
  169. @abstractmethod
  170. def user_runtime_dir(self) -> str:
  171. """:returns: runtime directory tied to the user"""
  172. @property
  173. @abstractmethod
  174. def site_runtime_dir(self) -> str:
  175. """:returns: runtime directory shared by users"""
  176. @property
  177. def user_data_path(self) -> Path:
  178. """:returns: data path tied to the user"""
  179. return Path(self.user_data_dir)
  180. @property
  181. def site_data_path(self) -> Path:
  182. """:returns: data path shared by users"""
  183. return Path(self.site_data_dir)
  184. @property
  185. def user_config_path(self) -> Path:
  186. """:returns: config path tied to the user"""
  187. return Path(self.user_config_dir)
  188. @property
  189. def site_config_path(self) -> Path:
  190. """:returns: config path shared by users"""
  191. return Path(self.site_config_dir)
  192. @property
  193. def user_cache_path(self) -> Path:
  194. """:returns: cache path tied to the user"""
  195. return Path(self.user_cache_dir)
  196. @property
  197. def site_cache_path(self) -> Path:
  198. """:returns: cache path shared by users"""
  199. return Path(self.site_cache_dir)
  200. @property
  201. def user_state_path(self) -> Path:
  202. """:returns: state path tied to the user"""
  203. return Path(self.user_state_dir)
  204. @property
  205. def site_state_path(self) -> Path:
  206. """:returns: state path shared by users"""
  207. return Path(self.site_state_dir)
  208. @property
  209. def user_log_path(self) -> Path:
  210. """:returns: log path tied to the user"""
  211. return Path(self.user_log_dir)
  212. @property
  213. def site_log_path(self) -> Path:
  214. """:returns: log path shared by users"""
  215. return Path(self.site_log_dir)
  216. @property
  217. def user_documents_path(self) -> Path:
  218. """:returns: documents path tied to the user"""
  219. return Path(self.user_documents_dir)
  220. @property
  221. def user_downloads_path(self) -> Path:
  222. """:returns: downloads path tied to the user"""
  223. return Path(self.user_downloads_dir)
  224. @property
  225. def user_pictures_path(self) -> Path:
  226. """:returns: pictures path tied to the user"""
  227. return Path(self.user_pictures_dir)
  228. @property
  229. def user_videos_path(self) -> Path:
  230. """:returns: videos path tied to the user"""
  231. return Path(self.user_videos_dir)
  232. @property
  233. def user_music_path(self) -> Path:
  234. """:returns: music path tied to the user"""
  235. return Path(self.user_music_dir)
  236. @property
  237. def user_desktop_path(self) -> Path:
  238. """:returns: desktop path tied to the user"""
  239. return Path(self.user_desktop_dir)
  240. @property
  241. def user_bin_path(self) -> Path:
  242. """:returns: bin path tied to the user"""
  243. return Path(self.user_bin_dir)
  244. @property
  245. def site_bin_path(self) -> Path:
  246. """:returns: bin path shared by users"""
  247. return Path(self.site_bin_dir)
  248. @property
  249. def user_applications_path(self) -> Path:
  250. """:returns: applications path tied to the user"""
  251. return Path(self.user_applications_dir)
  252. @property
  253. def site_applications_path(self) -> Path:
  254. """:returns: applications path shared by users"""
  255. return Path(self.site_applications_dir)
  256. @property
  257. def user_runtime_path(self) -> Path:
  258. """:returns: runtime path tied to the user"""
  259. return Path(self.user_runtime_dir)
  260. @property
  261. def site_runtime_path(self) -> Path:
  262. """:returns: runtime path shared by users"""
  263. return Path(self.site_runtime_dir)
  264. def iter_config_dirs(self) -> Iterator[str]:
  265. """:yield: all user and site configuration directories."""
  266. yield self.user_config_dir
  267. yield self.site_config_dir
  268. def iter_data_dirs(self) -> Iterator[str]:
  269. """:yield: all user and site data directories."""
  270. yield self.user_data_dir
  271. yield self.site_data_dir
  272. def iter_cache_dirs(self) -> Iterator[str]:
  273. """:yield: all user and site cache directories."""
  274. yield self.user_cache_dir
  275. yield self.site_cache_dir
  276. def iter_state_dirs(self) -> Iterator[str]:
  277. """:yield: all user and site state directories."""
  278. yield self.user_state_dir
  279. yield self.site_state_dir
  280. def iter_log_dirs(self) -> Iterator[str]:
  281. """:yield: all user and site log directories."""
  282. yield self.user_log_dir
  283. yield self.site_log_dir
  284. def iter_runtime_dirs(self) -> Iterator[str]:
  285. """:yield: all user and site runtime directories."""
  286. yield self.user_runtime_dir
  287. yield self.site_runtime_dir
  288. def iter_config_paths(self) -> Iterator[Path]:
  289. """:yield: all user and site configuration paths."""
  290. for path in self.iter_config_dirs():
  291. yield Path(path)
  292. def iter_data_paths(self) -> Iterator[Path]:
  293. """:yield: all user and site data paths."""
  294. for path in self.iter_data_dirs():
  295. yield Path(path)
  296. def iter_cache_paths(self) -> Iterator[Path]:
  297. """:yield: all user and site cache paths."""
  298. for path in self.iter_cache_dirs():
  299. yield Path(path)
  300. def iter_state_paths(self) -> Iterator[Path]:
  301. """:yield: all user and site state paths."""
  302. for path in self.iter_state_dirs():
  303. yield Path(path)
  304. def iter_log_paths(self) -> Iterator[Path]:
  305. """:yield: all user and site log paths."""
  306. for path in self.iter_log_dirs():
  307. yield Path(path)
  308. def iter_runtime_paths(self) -> Iterator[Path]:
  309. """:yield: all user and site runtime paths."""
  310. for path in self.iter_runtime_dirs():
  311. yield Path(path)