]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/platformdirs/windows.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / pip / _vendor / platformdirs / windows.py
1 """Windows."""
2 from __future__ import annotations
3
4 import ctypes
5 import os
6 import sys
7 from functools import lru_cache
8 from typing import TYPE_CHECKING
9
10 from .api import PlatformDirsABC
11
12 if TYPE_CHECKING:
13 from collections.abc import Callable
14
15
16 class Windows(PlatformDirsABC):
17 """
18 `MSDN on where to store app data files
19 <http://support.microsoft.com/default.aspx?scid=kb;en-us;310294#XSLTH3194121123120121120120>`_.
20 Makes use of the
21 `appname <platformdirs.api.PlatformDirsABC.appname>`,
22 `appauthor <platformdirs.api.PlatformDirsABC.appauthor>`,
23 `version <platformdirs.api.PlatformDirsABC.version>`,
24 `roaming <platformdirs.api.PlatformDirsABC.roaming>`,
25 `opinion <platformdirs.api.PlatformDirsABC.opinion>`,
26 `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
27 """
28
29 @property
30 def user_data_dir(self) -> str:
31 """
32 :return: data directory tied to the user, e.g.
33 ``%USERPROFILE%\\AppData\\Local\\$appauthor\\$appname`` (not roaming) or
34 ``%USERPROFILE%\\AppData\\Roaming\\$appauthor\\$appname`` (roaming)
35 """
36 const = "CSIDL_APPDATA" if self.roaming else "CSIDL_LOCAL_APPDATA"
37 path = os.path.normpath(get_win_folder(const))
38 return self._append_parts(path)
39
40 def _append_parts(self, path: str, *, opinion_value: str | None = None) -> str:
41 params = []
42 if self.appname:
43 if self.appauthor is not False:
44 author = self.appauthor or self.appname
45 params.append(author)
46 params.append(self.appname)
47 if opinion_value is not None and self.opinion:
48 params.append(opinion_value)
49 if self.version:
50 params.append(self.version)
51 path = os.path.join(path, *params) # noqa: PTH118
52 self._optionally_create_directory(path)
53 return path
54
55 @property
56 def site_data_dir(self) -> str:
57 """:return: data directory shared by users, e.g. ``C:\\ProgramData\\$appauthor\\$appname``"""
58 path = os.path.normpath(get_win_folder("CSIDL_COMMON_APPDATA"))
59 return self._append_parts(path)
60
61 @property
62 def user_config_dir(self) -> str:
63 """:return: config directory tied to the user, same as `user_data_dir`"""
64 return self.user_data_dir
65
66 @property
67 def site_config_dir(self) -> str:
68 """:return: config directory shared by the users, same as `site_data_dir`"""
69 return self.site_data_dir
70
71 @property
72 def user_cache_dir(self) -> str:
73 """
74 :return: cache directory tied to the user (if opinionated with ``Cache`` folder within ``$appname``) e.g.
75 ``%USERPROFILE%\\AppData\\Local\\$appauthor\\$appname\\Cache\\$version``
76 """
77 path = os.path.normpath(get_win_folder("CSIDL_LOCAL_APPDATA"))
78 return self._append_parts(path, opinion_value="Cache")
79
80 @property
81 def site_cache_dir(self) -> str:
82 """:return: cache directory shared by users, e.g. ``C:\\ProgramData\\$appauthor\\$appname\\Cache\\$version``"""
83 path = os.path.normpath(get_win_folder("CSIDL_COMMON_APPDATA"))
84 return self._append_parts(path, opinion_value="Cache")
85
86 @property
87 def user_state_dir(self) -> str:
88 """:return: state directory tied to the user, same as `user_data_dir`"""
89 return self.user_data_dir
90
91 @property
92 def user_log_dir(self) -> str:
93 """:return: log directory tied to the user, same as `user_data_dir` if not opinionated else ``Logs`` in it"""
94 path = self.user_data_dir
95 if self.opinion:
96 path = os.path.join(path, "Logs") # noqa: PTH118
97 self._optionally_create_directory(path)
98 return path
99
100 @property
101 def user_documents_dir(self) -> str:
102 """:return: documents directory tied to the user e.g. ``%USERPROFILE%\\Documents``"""
103 return os.path.normpath(get_win_folder("CSIDL_PERSONAL"))
104
105 @property
106 def user_downloads_dir(self) -> str:
107 """:return: downloads directory tied to the user e.g. ``%USERPROFILE%\\Downloads``"""
108 return os.path.normpath(get_win_folder("CSIDL_DOWNLOADS"))
109
110 @property
111 def user_pictures_dir(self) -> str:
112 """:return: pictures directory tied to the user e.g. ``%USERPROFILE%\\Pictures``"""
113 return os.path.normpath(get_win_folder("CSIDL_MYPICTURES"))
114
115 @property
116 def user_videos_dir(self) -> str:
117 """:return: videos directory tied to the user e.g. ``%USERPROFILE%\\Videos``"""
118 return os.path.normpath(get_win_folder("CSIDL_MYVIDEO"))
119
120 @property
121 def user_music_dir(self) -> str:
122 """:return: music directory tied to the user e.g. ``%USERPROFILE%\\Music``"""
123 return os.path.normpath(get_win_folder("CSIDL_MYMUSIC"))
124
125 @property
126 def user_runtime_dir(self) -> str:
127 """
128 :return: runtime directory tied to the user, e.g.
129 ``%USERPROFILE%\\AppData\\Local\\Temp\\$appauthor\\$appname``
130 """
131 path = os.path.normpath(os.path.join(get_win_folder("CSIDL_LOCAL_APPDATA"), "Temp")) # noqa: PTH118
132 return self._append_parts(path)
133
134
135 def get_win_folder_from_env_vars(csidl_name: str) -> str:
136 """Get folder from environment variables."""
137 result = get_win_folder_if_csidl_name_not_env_var(csidl_name)
138 if result is not None:
139 return result
140
141 env_var_name = {
142 "CSIDL_APPDATA": "APPDATA",
143 "CSIDL_COMMON_APPDATA": "ALLUSERSPROFILE",
144 "CSIDL_LOCAL_APPDATA": "LOCALAPPDATA",
145 }.get(csidl_name)
146 if env_var_name is None:
147 msg = f"Unknown CSIDL name: {csidl_name}"
148 raise ValueError(msg)
149 result = os.environ.get(env_var_name)
150 if result is None:
151 msg = f"Unset environment variable: {env_var_name}"
152 raise ValueError(msg)
153 return result
154
155
156 def get_win_folder_if_csidl_name_not_env_var(csidl_name: str) -> str | None:
157 """Get folder for a CSIDL name that does not exist as an environment variable."""
158 if csidl_name == "CSIDL_PERSONAL":
159 return os.path.join(os.path.normpath(os.environ["USERPROFILE"]), "Documents") # noqa: PTH118
160
161 if csidl_name == "CSIDL_DOWNLOADS":
162 return os.path.join(os.path.normpath(os.environ["USERPROFILE"]), "Downloads") # noqa: PTH118
163
164 if csidl_name == "CSIDL_MYPICTURES":
165 return os.path.join(os.path.normpath(os.environ["USERPROFILE"]), "Pictures") # noqa: PTH118
166
167 if csidl_name == "CSIDL_MYVIDEO":
168 return os.path.join(os.path.normpath(os.environ["USERPROFILE"]), "Videos") # noqa: PTH118
169
170 if csidl_name == "CSIDL_MYMUSIC":
171 return os.path.join(os.path.normpath(os.environ["USERPROFILE"]), "Music") # noqa: PTH118
172 return None
173
174
175 def get_win_folder_from_registry(csidl_name: str) -> str:
176 """
177 Get folder from the registry.
178
179 This is a fallback technique at best. I'm not sure if using the registry for these guarantees us the correct answer
180 for all CSIDL_* names.
181 """
182 shell_folder_name = {
183 "CSIDL_APPDATA": "AppData",
184 "CSIDL_COMMON_APPDATA": "Common AppData",
185 "CSIDL_LOCAL_APPDATA": "Local AppData",
186 "CSIDL_PERSONAL": "Personal",
187 "CSIDL_DOWNLOADS": "{374DE290-123F-4565-9164-39C4925E467B}",
188 "CSIDL_MYPICTURES": "My Pictures",
189 "CSIDL_MYVIDEO": "My Video",
190 "CSIDL_MYMUSIC": "My Music",
191 }.get(csidl_name)
192 if shell_folder_name is None:
193 msg = f"Unknown CSIDL name: {csidl_name}"
194 raise ValueError(msg)
195 if sys.platform != "win32": # only needed for mypy type checker to know that this code runs only on Windows
196 raise NotImplementedError
197 import winreg
198
199 key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
200 directory, _ = winreg.QueryValueEx(key, shell_folder_name)
201 return str(directory)
202
203
204 def get_win_folder_via_ctypes(csidl_name: str) -> str:
205 """Get folder with ctypes."""
206 # There is no 'CSIDL_DOWNLOADS'.
207 # Use 'CSIDL_PROFILE' (40) and append the default folder 'Downloads' instead.
208 # https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
209
210 csidl_const = {
211 "CSIDL_APPDATA": 26,
212 "CSIDL_COMMON_APPDATA": 35,
213 "CSIDL_LOCAL_APPDATA": 28,
214 "CSIDL_PERSONAL": 5,
215 "CSIDL_MYPICTURES": 39,
216 "CSIDL_MYVIDEO": 14,
217 "CSIDL_MYMUSIC": 13,
218 "CSIDL_DOWNLOADS": 40,
219 }.get(csidl_name)
220 if csidl_const is None:
221 msg = f"Unknown CSIDL name: {csidl_name}"
222 raise ValueError(msg)
223
224 buf = ctypes.create_unicode_buffer(1024)
225 windll = getattr(ctypes, "windll") # noqa: B009 # using getattr to avoid false positive with mypy type checker
226 windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
227
228 # Downgrade to short path name if it has highbit chars.
229 if any(ord(c) > 255 for c in buf): # noqa: PLR2004
230 buf2 = ctypes.create_unicode_buffer(1024)
231 if windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
232 buf = buf2
233
234 if csidl_name == "CSIDL_DOWNLOADS":
235 return os.path.join(buf.value, "Downloads") # noqa: PTH118
236
237 return buf.value
238
239
240 def _pick_get_win_folder() -> Callable[[str], str]:
241 if hasattr(ctypes, "windll"):
242 return get_win_folder_via_ctypes
243 try:
244 import winreg # noqa: F401
245 except ImportError:
246 return get_win_folder_from_env_vars
247 else:
248 return get_win_folder_from_registry
249
250
251 get_win_folder = lru_cache(maxsize=None)(_pick_get_win_folder())
252
253 __all__ = [
254 "Windows",
255 ]