Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/platformdirs/api.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

267 statements  

1"""Base API.""" 

2 

3from __future__ import annotations 

4 

5import os 

6from abc import ABC, abstractmethod 

7from pathlib import Path 

8from typing import TYPE_CHECKING 

9 

10if TYPE_CHECKING: 

11 from collections.abc import Iterable, Iterator 

12 from typing import Literal 

13 

14 

15class PlatformDirsABC(ABC): # ruff:ignore[too-many-public-methods] 

16 """Abstract base class defining all platform directory properties, their :class:`~pathlib.Path` variants, and iterators. 

17 

18 Platform-specific subclasses (e.g. :class:`~platformdirs.windows.Windows`, :class:`~platformdirs.macos.MacOS`, 

19 :class:`~platformdirs.unix.Unix`) implement the abstract properties to return the appropriate paths for each 

20 operating system. 

21 

22 """ 

23 

24 def __init__( # ruff:ignore[too-many-arguments, too-many-positional-arguments] 

25 self, 

26 appname: str | None = None, 

27 appauthor: str | Literal[False] | None = None, 

28 version: str | None = None, 

29 roaming: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] 

30 multipath: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] 

31 opinion: bool = True, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] 

32 ensure_exists: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] 

33 use_site_for_root: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] 

34 ) -> None: 

35 """Create a new platform directory. 

36 

37 :param appname: See `appname`. 

38 :param appauthor: See `appauthor`. 

39 :param version: See `version`. 

40 :param roaming: See `roaming`. 

41 :param multipath: See `multipath`. 

42 :param opinion: See `opinion`. 

43 :param ensure_exists: See `ensure_exists`. 

44 :param use_site_for_root: See `use_site_for_root`. 

45 

46 """ 

47 self.appname = appname #: The name of the application. 

48 self.appauthor = appauthor 

49 """The name of the app author or distributing body for this application. 

50 

51 Typically, it is the owning company name. Defaults to `appname`. You may pass ``False`` to disable it. 

52 

53 .. note:: 

54 

55 On Windows, the directory structure is ``<base>/<appauthor>/<appname>``. When ``appauthor`` is ``None`` (the 

56 default), it falls back to ``appname``, resulting in ``<base>/<appname>/<appname>`` (e.g. 

57 ``AppData/Local/myapp/myapp``). Pass ``appauthor=False`` to omit the author directory entirely and get 

58 ``<base>/<appname>``. 

59 

60 """ 

61 self.version = version 

62 """An optional version path element to append to the path. 

63 

64 You might want to use this if you want multiple versions of your app to be able to run independently. If used, 

65 this would typically be ``<major>.<minor>``. 

66 

67 """ 

68 self.roaming = roaming 

69 """Whether to use the roaming appdata directory on Windows. 

70 

71 That means that for users on a Windows network setup for roaming profiles, this user data will be synced on 

72 login (see `here <https://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>`_). 

73 

74 """ 

75 self.multipath = multipath 

76 """An optional parameter which indicates that the entire list of data dirs should be returned. 

77 

78 By default, the first item would only be returned. Only affects ``site_data_dir`` and ``site_config_dir`` on 

79 Unix and macOS. 

80 

81 """ 

82 self.opinion = opinion 

83 """Whether to use opinionated values. 

84 

85 When enabled, appends an additional subdirectory for certain directories: e.g. ``Cache`` for cache and ``Logs`` 

86 for logs on Windows, ``log`` for logs on Unix. 

87 

88 """ 

89 self.ensure_exists = ensure_exists 

90 """Optionally create the directory (and any missing parents) upon access if it does not exist. 

91 

92 By default, no directories are created. 

93 

94 """ 

95 self.use_site_for_root = use_site_for_root 

96 """Whether to redirect ``user_*_dir`` calls to their ``site_*_dir`` equivalents when running as root (uid 0). 

97 

98 Only has an effect on Unix. Disabled by default for backwards compatibility. When enabled, XDG user environment 

99 variables (e.g. ``XDG_DATA_HOME``) are bypassed for the redirected directories. 

100 

101 """ 

102 

103 def _append_app_name_and_version(self, *base: str) -> str: 

104 params = list(base[1:]) 

105 if self.appname: 

106 params.append(self.appname) 

107 if self.version: 

108 params.append(self.version) 

109 path = os.path.join(base[0], *params) # ruff:ignore[os-path-join] 

110 self._optionally_create_directory(path) 

111 return path 

112 

113 def _optionally_create_directory(self, path: str) -> None: 

114 if self.ensure_exists: 

115 Path(path).mkdir(parents=True, exist_ok=True) 

116 

117 def _first_item_as_path_if_multipath(self, directory: str) -> Path: 

118 if self.multipath: 

119 # If multipath is True, the first path is returned. 

120 directory = directory.partition(os.pathsep)[0] 

121 return Path(directory) 

122 

123 @property 

124 @abstractmethod 

125 def user_data_dir(self) -> str: 

126 """Data directory tied to the user.""" 

127 

128 @property 

129 @abstractmethod 

130 def site_data_dir(self) -> str: 

131 """Data directory shared by users.""" 

132 

133 @property 

134 def _site_data_dirs(self) -> list[str]: 

135 raise NotImplementedError 

136 

137 @property 

138 @abstractmethod 

139 def user_config_dir(self) -> str: 

140 """Config directory tied to the user.""" 

141 

142 @property 

143 @abstractmethod 

144 def site_config_dir(self) -> str: 

145 """Config directory shared by users.""" 

146 

147 @property 

148 def _site_config_dirs(self) -> list[str]: 

149 raise NotImplementedError 

150 

151 @property 

152 @abstractmethod 

153 def user_cache_dir(self) -> str: 

154 """Cache directory tied to the user.""" 

155 

156 @property 

157 @abstractmethod 

158 def site_cache_dir(self) -> str: 

159 """Cache directory shared by users.""" 

160 

161 @property 

162 @abstractmethod 

163 def user_state_dir(self) -> str: 

164 """State directory tied to the user.""" 

165 

166 @property 

167 @abstractmethod 

168 def site_state_dir(self) -> str: 

169 """State directory shared by users.""" 

170 

171 @property 

172 @abstractmethod 

173 def user_log_dir(self) -> str: 

174 """Log directory tied to the user.""" 

175 

176 @property 

177 @abstractmethod 

178 def site_log_dir(self) -> str: 

179 """Log directory shared by users.""" 

180 

181 @property 

182 @abstractmethod 

183 def user_documents_dir(self) -> str: 

184 """Documents directory tied to the user.""" 

185 

186 @property 

187 @abstractmethod 

188 def user_downloads_dir(self) -> str: 

189 """Downloads directory tied to the user.""" 

190 

191 @property 

192 @abstractmethod 

193 def user_pictures_dir(self) -> str: 

194 """Pictures directory tied to the user.""" 

195 

196 @property 

197 @abstractmethod 

198 def user_videos_dir(self) -> str: 

199 """Videos directory tied to the user.""" 

200 

201 @property 

202 @abstractmethod 

203 def user_music_dir(self) -> str: 

204 """Music directory tied to the user.""" 

205 

206 @property 

207 @abstractmethod 

208 def user_desktop_dir(self) -> str: 

209 """Desktop directory tied to the user.""" 

210 

211 @property 

212 @abstractmethod 

213 def user_projects_dir(self) -> str: 

214 """Projects directory tied to the user.""" 

215 

216 @property 

217 @abstractmethod 

218 def user_publicshare_dir(self) -> str: 

219 """Public share directory tied to the user.""" 

220 

221 @property 

222 @abstractmethod 

223 def user_templates_dir(self) -> str: 

224 """Templates directory tied to the user.""" 

225 

226 @property 

227 @abstractmethod 

228 def user_fonts_dir(self) -> str: 

229 """Fonts directory tied to the user.""" 

230 

231 @property 

232 @abstractmethod 

233 def user_preference_dir(self) -> str: 

234 """Preference directory tied to the user.""" 

235 

236 @property 

237 @abstractmethod 

238 def user_bin_dir(self) -> str: 

239 """Bin directory tied to the user.""" 

240 

241 @property 

242 @abstractmethod 

243 def site_bin_dir(self) -> str: 

244 """Bin directory shared by users.""" 

245 

246 @property 

247 @abstractmethod 

248 def user_applications_dir(self) -> str: 

249 """Applications directory tied to the user.""" 

250 

251 @property 

252 @abstractmethod 

253 def site_applications_dir(self) -> str: 

254 """Applications directory shared by users.""" 

255 

256 @property 

257 def _site_applications_dirs(self) -> list[str]: 

258 raise NotImplementedError 

259 

260 @property 

261 @abstractmethod 

262 def user_runtime_dir(self) -> str: 

263 """Runtime directory tied to the user.""" 

264 

265 @property 

266 @abstractmethod 

267 def site_runtime_dir(self) -> str: 

268 """Runtime directory shared by users.""" 

269 

270 @property 

271 def user_data_path(self) -> Path: 

272 """Data path tied to the user.""" 

273 return Path(self.user_data_dir) 

274 

275 @property 

276 def site_data_path(self) -> Path: 

277 """Data path shared by users.""" 

278 return Path(self.site_data_dir) 

279 

280 @property 

281 def user_config_path(self) -> Path: 

282 """Config path tied to the user.""" 

283 return Path(self.user_config_dir) 

284 

285 @property 

286 def site_config_path(self) -> Path: 

287 """Config path shared by users.""" 

288 return Path(self.site_config_dir) 

289 

290 @property 

291 def user_cache_path(self) -> Path: 

292 """Cache path tied to the user.""" 

293 return Path(self.user_cache_dir) 

294 

295 @property 

296 def site_cache_path(self) -> Path: 

297 """Cache path shared by users.""" 

298 return Path(self.site_cache_dir) 

299 

300 @property 

301 def user_state_path(self) -> Path: 

302 """State path tied to the user.""" 

303 return Path(self.user_state_dir) 

304 

305 @property 

306 def site_state_path(self) -> Path: 

307 """State path shared by users.""" 

308 return Path(self.site_state_dir) 

309 

310 @property 

311 def user_log_path(self) -> Path: 

312 """Log path tied to the user.""" 

313 return Path(self.user_log_dir) 

314 

315 @property 

316 def site_log_path(self) -> Path: 

317 """Log path shared by users.""" 

318 return Path(self.site_log_dir) 

319 

320 @property 

321 def user_documents_path(self) -> Path: 

322 """Documents path tied to the user.""" 

323 return Path(self.user_documents_dir) 

324 

325 @property 

326 def user_downloads_path(self) -> Path: 

327 """Downloads path tied to the user.""" 

328 return Path(self.user_downloads_dir) 

329 

330 @property 

331 def user_pictures_path(self) -> Path: 

332 """Pictures path tied to the user.""" 

333 return Path(self.user_pictures_dir) 

334 

335 @property 

336 def user_videos_path(self) -> Path: 

337 """Videos path tied to the user.""" 

338 return Path(self.user_videos_dir) 

339 

340 @property 

341 def user_music_path(self) -> Path: 

342 """Music path tied to the user.""" 

343 return Path(self.user_music_dir) 

344 

345 @property 

346 def user_desktop_path(self) -> Path: 

347 """Desktop path tied to the user.""" 

348 return Path(self.user_desktop_dir) 

349 

350 @property 

351 def user_projects_path(self) -> Path: 

352 """Projects path tied to the user.""" 

353 return Path(self.user_projects_dir) 

354 

355 @property 

356 def user_publicshare_path(self) -> Path: 

357 """Public share path tied to the user.""" 

358 return Path(self.user_publicshare_dir) 

359 

360 @property 

361 def user_templates_path(self) -> Path: 

362 """Templates path tied to the user.""" 

363 return Path(self.user_templates_dir) 

364 

365 @property 

366 def user_fonts_path(self) -> Path: 

367 """Fonts path tied to the user.""" 

368 return Path(self.user_fonts_dir) 

369 

370 @property 

371 def user_preference_path(self) -> Path: 

372 """Preference path tied to the user.""" 

373 return Path(self.user_preference_dir) 

374 

375 @property 

376 def user_bin_path(self) -> Path: 

377 """Bin path tied to the user.""" 

378 return Path(self.user_bin_dir) 

379 

380 @property 

381 def site_bin_path(self) -> Path: 

382 """Bin path shared by users.""" 

383 return Path(self.site_bin_dir) 

384 

385 @property 

386 def user_applications_path(self) -> Path: 

387 """Applications path tied to the user.""" 

388 return Path(self.user_applications_dir) 

389 

390 @property 

391 def site_applications_path(self) -> Path: 

392 """Applications path shared by users. Only return the first item, even if ``multipath`` is set to ``True``.""" 

393 return self._first_item_as_path_if_multipath(self.site_applications_dir) 

394 

395 @property 

396 def user_runtime_path(self) -> Path: 

397 """Runtime path tied to the user.""" 

398 return Path(self.user_runtime_dir) 

399 

400 @property 

401 def site_runtime_path(self) -> Path: 

402 """Runtime path shared by users.""" 

403 return Path(self.site_runtime_dir) 

404 

405 def iter_config_dirs(self) -> Iterator[str]: 

406 """:yield: all user and site configuration directories.""" 

407 yield from _unique(self._iter_config_dirs()) 

408 

409 def _iter_config_dirs(self) -> Iterator[str]: 

410 yield self.user_config_dir 

411 yield self.site_config_dir 

412 

413 def iter_data_dirs(self) -> Iterator[str]: 

414 """:yield: all user and site data directories.""" 

415 yield from _unique(self._iter_data_dirs()) 

416 

417 def _iter_data_dirs(self) -> Iterator[str]: 

418 yield self.user_data_dir 

419 yield self.site_data_dir 

420 

421 def iter_cache_dirs(self) -> Iterator[str]: 

422 """:yield: all user and site cache directories.""" 

423 yield from _unique(self._iter_cache_dirs()) 

424 

425 def _iter_cache_dirs(self) -> Iterator[str]: 

426 yield self.user_cache_dir 

427 yield self.site_cache_dir 

428 

429 def iter_state_dirs(self) -> Iterator[str]: 

430 """:yield: all user and site state directories.""" 

431 yield from _unique(self._iter_state_dirs()) 

432 

433 def _iter_state_dirs(self) -> Iterator[str]: 

434 yield self.user_state_dir 

435 yield self.site_state_dir 

436 

437 def iter_log_dirs(self) -> Iterator[str]: 

438 """:yield: all user and site log directories.""" 

439 yield from _unique(self._iter_log_dirs()) 

440 

441 def _iter_log_dirs(self) -> Iterator[str]: 

442 yield self.user_log_dir 

443 yield self.site_log_dir 

444 

445 def iter_runtime_dirs(self) -> Iterator[str]: 

446 """:yield: all user and site runtime directories.""" 

447 yield from _unique(self._iter_runtime_dirs()) 

448 

449 def _iter_runtime_dirs(self) -> Iterator[str]: 

450 yield self.user_runtime_dir 

451 yield self.site_runtime_dir 

452 

453 def iter_config_paths(self) -> Iterator[Path]: 

454 """:yield: all user and site configuration paths.""" 

455 for path in self.iter_config_dirs(): 

456 yield Path(path) 

457 

458 def iter_data_paths(self) -> Iterator[Path]: 

459 """:yield: all user and site data paths.""" 

460 for path in self.iter_data_dirs(): 

461 yield Path(path) 

462 

463 def iter_cache_paths(self) -> Iterator[Path]: 

464 """:yield: all user and site cache paths.""" 

465 for path in self.iter_cache_dirs(): 

466 yield Path(path) 

467 

468 def iter_state_paths(self) -> Iterator[Path]: 

469 """:yield: all user and site state paths.""" 

470 for path in self.iter_state_dirs(): 

471 yield Path(path) 

472 

473 def iter_log_paths(self) -> Iterator[Path]: 

474 """:yield: all user and site log paths.""" 

475 for path in self.iter_log_dirs(): 

476 yield Path(path) 

477 

478 def iter_runtime_paths(self) -> Iterator[Path]: 

479 """:yield: all user and site runtime paths.""" 

480 for path in self.iter_runtime_dirs(): 

481 yield Path(path) 

482 

483 

484def _unique(dirs: Iterable[str]) -> Iterator[str]: 

485 """:yield: ``dirs`` in order, skipping any directory already yielded.""" 

486 # Lazy on purpose: under ensure_exists reading a site_*_dir creates it, so draining ``dirs`` up front would 

487 # create directories for a caller that stops after the first entry. 

488 seen: set[str] = set() 

489 for path in dirs: 

490 if path not in seen: 

491 seen.add(path) 

492 yield path