macos.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """macOS."""
  2. from __future__ import annotations
  3. import os.path
  4. import sys
  5. from typing import TYPE_CHECKING
  6. from .api import PlatformDirsABC
  7. if TYPE_CHECKING:
  8. from pathlib import Path
  9. class MacOS(PlatformDirsABC):
  10. """
  11. Platform directories for the macOS operating system.
  12. Follows the guidance from
  13. `Apple documentation <https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html>`_.
  14. Makes use of the `appname <platformdirs.api.PlatformDirsABC.appname>`,
  15. `version <platformdirs.api.PlatformDirsABC.version>`,
  16. `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
  17. """
  18. @property
  19. def user_data_dir(self) -> str:
  20. """:return: 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_dir(self) -> str:
  24. """
  25. :return: data directory shared by users, e.g. ``/Library/Application Support/$appname/$version``.
  26. If we're using a Python binary managed by `Homebrew <https://brew.sh>`_, the directory
  27. will be under the Homebrew prefix, e.g. ``$homebrew_prefix/share/$appname/$version``.
  28. If `multipath <platformdirs.api.PlatformDirsABC.multipath>` is enabled, and we're in Homebrew,
  29. the response is a multi-path string separated by ":", e.g.
  30. ``$homebrew_prefix/share/$appname/$version:/Library/Application Support/$appname/$version``
  31. """
  32. is_homebrew = "/opt/python" in sys.prefix
  33. homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
  34. path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/share")] if is_homebrew else []
  35. path_list.append(self._append_app_name_and_version("/Library/Application Support"))
  36. if self.multipath:
  37. return os.pathsep.join(path_list)
  38. return path_list[0]
  39. @property
  40. def site_data_path(self) -> Path:
  41. """:return: data path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
  42. return self._first_item_as_path_if_multipath(self.site_data_dir)
  43. @property
  44. def user_config_dir(self) -> str:
  45. """:return: config directory tied to the user, same as `user_data_dir`"""
  46. return self.user_data_dir
  47. @property
  48. def site_config_dir(self) -> str:
  49. """:return: config directory shared by the users, same as `site_data_dir`"""
  50. return self.site_data_dir
  51. @property
  52. def user_cache_dir(self) -> str:
  53. """:return: cache directory tied to the user, e.g. ``~/Library/Caches/$appname/$version``"""
  54. return self._append_app_name_and_version(os.path.expanduser("~/Library/Caches")) # noqa: PTH111
  55. @property
  56. def site_cache_dir(self) -> str:
  57. """
  58. :return: cache directory shared by users, e.g. ``/Library/Caches/$appname/$version``.
  59. If we're using a Python binary managed by `Homebrew <https://brew.sh>`_, the directory
  60. will be under the Homebrew prefix, e.g. ``$homebrew_prefix/var/cache/$appname/$version``.
  61. If `multipath <platformdirs.api.PlatformDirsABC.multipath>` is enabled, and we're in Homebrew,
  62. the response is a multi-path string separated by ":", e.g.
  63. ``$homebrew_prefix/var/cache/$appname/$version:/Library/Caches/$appname/$version``
  64. """
  65. is_homebrew = "/opt/python" in sys.prefix
  66. homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
  67. path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/var/cache")] if is_homebrew else []
  68. path_list.append(self._append_app_name_and_version("/Library/Caches"))
  69. if self.multipath:
  70. return os.pathsep.join(path_list)
  71. return path_list[0]
  72. @property
  73. def site_cache_path(self) -> Path:
  74. """:return: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
  75. return self._first_item_as_path_if_multipath(self.site_cache_dir)
  76. @property
  77. def user_state_dir(self) -> str:
  78. """:return: state directory tied to the user, same as `user_data_dir`"""
  79. return self.user_data_dir
  80. @property
  81. def user_log_dir(self) -> str:
  82. """:return: log directory tied to the user, e.g. ``~/Library/Logs/$appname/$version``"""
  83. return self._append_app_name_and_version(os.path.expanduser("~/Library/Logs")) # noqa: PTH111
  84. @property
  85. def user_documents_dir(self) -> str:
  86. """:return: documents directory tied to the user, e.g. ``~/Documents``"""
  87. return os.path.expanduser("~/Documents") # noqa: PTH111
  88. @property
  89. def user_downloads_dir(self) -> str:
  90. """:return: downloads directory tied to the user, e.g. ``~/Downloads``"""
  91. return os.path.expanduser("~/Downloads") # noqa: PTH111
  92. @property
  93. def user_pictures_dir(self) -> str:
  94. """:return: pictures directory tied to the user, e.g. ``~/Pictures``"""
  95. return os.path.expanduser("~/Pictures") # noqa: PTH111
  96. @property
  97. def user_videos_dir(self) -> str:
  98. """:return: videos directory tied to the user, e.g. ``~/Movies``"""
  99. return os.path.expanduser("~/Movies") # noqa: PTH111
  100. @property
  101. def user_music_dir(self) -> str:
  102. """:return: music directory tied to the user, e.g. ``~/Music``"""
  103. return os.path.expanduser("~/Music") # noqa: PTH111
  104. @property
  105. def user_desktop_dir(self) -> str:
  106. """:return: desktop directory tied to the user, e.g. ``~/Desktop``"""
  107. return os.path.expanduser("~/Desktop") # noqa: PTH111
  108. @property
  109. def user_runtime_dir(self) -> str:
  110. """:return: runtime directory tied to the user, e.g. ``~/Library/Caches/TemporaryItems/$appname/$version``"""
  111. return self._append_app_name_and_version(os.path.expanduser("~/Library/Caches/TemporaryItems")) # noqa: PTH111
  112. @property
  113. def site_runtime_dir(self) -> str:
  114. """:return: runtime directory shared by users, same as `user_runtime_dir`"""
  115. return self.user_runtime_dir
  116. __all__ = [
  117. "MacOS",
  118. ]