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