macos.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """macOS."""
  2. from __future__ import annotations
  3. import os.path
  4. import sys
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from collections.abc import Iterator
  8. from ._xdg import XDGMixin
  9. from .api import PlatformDirsABC
  10. if TYPE_CHECKING:
  11. from pathlib import Path
  12. class _MacOSDefaults(PlatformDirsABC): # noqa: PLR0904
  13. """Default platform directories for macOS without XDG environment variable overrides.
  14. Follows the guidance from `Apple's File System Programming Guide
  15. <https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html>`_.
  16. The XDG env var handling is in :class:`~platformdirs._xdg.XDGMixin`.
  17. """
  18. @property
  19. def user_data_dir(self) -> str:
  20. """:returns: data directory tied to the user, e.g. ``~/Library/Application Support/$appname/$version``"""
  21. return self._append_app_name_and_version(os.path.expanduser("~/Library/Application Support")) # noqa: PTH111
  22. @property
  23. def _site_data_dirs(self) -> list[str]:
  24. is_homebrew = "/opt/python" in sys.prefix
  25. homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
  26. path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/share")] if is_homebrew else []
  27. path_list.append(self._append_app_name_and_version("/Library/Application Support"))
  28. return path_list
  29. @property
  30. def site_data_path(self) -> Path:
  31. """:returns: data path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
  32. return self._first_item_as_path_if_multipath(self.site_data_dir)
  33. @property
  34. def user_config_dir(self) -> str:
  35. """:returns: config directory tied to the user, same as `user_data_dir`"""
  36. return self.user_data_dir
  37. @property
  38. def _site_config_dirs(self) -> list[str]:
  39. return self._site_data_dirs
  40. @property
  41. def user_cache_dir(self) -> str:
  42. """:returns: cache directory tied to the user, e.g. ``~/Library/Caches/$appname/$version``"""
  43. return self._append_app_name_and_version(os.path.expanduser("~/Library/Caches")) # noqa: PTH111
  44. @property
  45. def site_cache_dir(self) -> str:
  46. """:returns: cache directory shared by users, e.g. ``/Library/Caches/$appname/$version``. If we're using a Python binary managed by `Homebrew <https://brew.sh>`_, the directory will be under the Homebrew prefix, e.g. ``$homebrew_prefix/var/cache/$appname/$version``. If `multipath <platformdirs.api.PlatformDirsABC.multipath>` is enabled, and we're in Homebrew, the response is a multi-path string separated by ":", e.g. ``$homebrew_prefix/var/cache/$appname/$version:/Library/Caches/$appname/$version``"""
  47. is_homebrew = "/opt/python" in sys.prefix
  48. homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
  49. path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/var/cache")] if is_homebrew else []
  50. path_list.append(self._append_app_name_and_version("/Library/Caches"))
  51. if self.multipath:
  52. return os.pathsep.join(path_list)
  53. return path_list[0]
  54. @property
  55. def site_cache_path(self) -> Path:
  56. """:returns: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
  57. return self._first_item_as_path_if_multipath(self.site_cache_dir)
  58. @property
  59. def user_state_dir(self) -> str:
  60. """:returns: state directory tied to the user, same as `user_data_dir`"""
  61. return self.user_data_dir
  62. @property
  63. def site_state_dir(self) -> str:
  64. """:returns: state directory shared by users, same as `site_data_dir`"""
  65. return self.site_data_dir
  66. @property
  67. def user_log_dir(self) -> str:
  68. """:returns: log directory tied to the user, e.g. ``~/Library/Logs/$appname/$version``"""
  69. return self._append_app_name_and_version(os.path.expanduser("~/Library/Logs")) # noqa: PTH111
  70. @property
  71. def site_log_dir(self) -> str:
  72. """:returns: log directory shared by users, e.g. ``/Library/Logs/$appname/$version``"""
  73. return self._append_app_name_and_version("/Library/Logs")
  74. @property
  75. def user_documents_dir(self) -> str:
  76. """:returns: documents directory tied to the user, e.g. ``~/Documents``"""
  77. return os.path.expanduser("~/Documents") # noqa: PTH111
  78. @property
  79. def user_downloads_dir(self) -> str:
  80. """:returns: downloads directory tied to the user, e.g. ``~/Downloads``"""
  81. return os.path.expanduser("~/Downloads") # noqa: PTH111
  82. @property
  83. def user_pictures_dir(self) -> str:
  84. """:returns: pictures directory tied to the user, e.g. ``~/Pictures``"""
  85. return os.path.expanduser("~/Pictures") # noqa: PTH111
  86. @property
  87. def user_videos_dir(self) -> str:
  88. """:returns: videos directory tied to the user, e.g. ``~/Movies``"""
  89. return os.path.expanduser("~/Movies") # noqa: PTH111
  90. @property
  91. def user_music_dir(self) -> str:
  92. """:returns: music directory tied to the user, e.g. ``~/Music``"""
  93. return os.path.expanduser("~/Music") # noqa: PTH111
  94. @property
  95. def user_desktop_dir(self) -> str:
  96. """:returns: desktop directory tied to the user, e.g. ``~/Desktop``"""
  97. return os.path.expanduser("~/Desktop") # noqa: PTH111
  98. @property
  99. def user_bin_dir(self) -> str:
  100. """:returns: bin directory tied to the user, e.g. ``~/.local/bin``"""
  101. return os.path.expanduser("~/.local/bin") # noqa: PTH111
  102. @property
  103. def site_bin_dir(self) -> str:
  104. """:returns: bin directory shared by users, e.g. ``/usr/local/bin``"""
  105. return "/usr/local/bin"
  106. @property
  107. def user_applications_dir(self) -> str:
  108. """:returns: applications directory tied to the user, e.g. ``~/Applications``"""
  109. return os.path.expanduser("~/Applications") # noqa: PTH111
  110. @property
  111. def _site_applications_dirs(self) -> list[str]:
  112. return ["/Applications"]
  113. @property
  114. def site_applications_dir(self) -> str:
  115. """:returns: applications directory shared by users, e.g. ``/Applications``"""
  116. dirs = self._site_applications_dirs
  117. return os.pathsep.join(dirs) if self.multipath else dirs[0]
  118. @property
  119. def user_runtime_dir(self) -> str:
  120. """:returns: runtime directory tied to the user, e.g. ``~/Library/Caches/TemporaryItems/$appname/$version``"""
  121. return self._append_app_name_and_version(os.path.expanduser("~/Library/Caches/TemporaryItems")) # noqa: PTH111
  122. @property
  123. def site_runtime_dir(self) -> str:
  124. """:returns: runtime directory shared by users, same as `user_runtime_dir`"""
  125. return self.user_runtime_dir
  126. def iter_config_dirs(self) -> Iterator[str]:
  127. """:yield: all user and site configuration directories."""
  128. yield self.user_config_dir
  129. yield from self._site_config_dirs
  130. def iter_data_dirs(self) -> Iterator[str]:
  131. """:yield: all user and site data directories."""
  132. yield self.user_data_dir
  133. yield from self._site_data_dirs
  134. class MacOS(XDGMixin, _MacOSDefaults):
  135. """Platform directories for the macOS operating system.
  136. Follows the guidance from `Apple documentation
  137. <https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html>`_.
  138. Makes use of the `appname <platformdirs.api.PlatformDirsABC.appname>`, `version
  139. <platformdirs.api.PlatformDirsABC.version>`, `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  140. XDG environment variables (e.g. ``$XDG_DATA_HOME``) are supported and take precedence over macOS defaults.
  141. """
  142. __all__ = [
  143. "MacOS",
  144. ]