api.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from __future__ import annotations
  2. import os
  3. import sys
  4. from abc import ABC, abstractmethod
  5. from pathlib import Path
  6. if sys.version_info >= (3, 8): # pragma: no branch
  7. from typing import Literal # pragma: no cover
  8. class PlatformDirsABC(ABC):
  9. """
  10. Abstract base class for platform directories.
  11. """
  12. def __init__(
  13. self,
  14. appname: str | None = None,
  15. appauthor: str | None | Literal[False] = None,
  16. version: str | None = None,
  17. roaming: bool = False,
  18. multipath: bool = False,
  19. opinion: bool = True,
  20. ensure_exists: bool = False,
  21. ):
  22. """
  23. Create a new platform directory.
  24. :param appname: See `appname`.
  25. :param appauthor: See `appauthor`.
  26. :param version: See `version`.
  27. :param roaming: See `roaming`.
  28. :param multipath: See `multipath`.
  29. :param opinion: See `opinion`.
  30. :param ensure_exists: See `ensure_exists`.
  31. """
  32. self.appname = appname #: The name of application.
  33. self.appauthor = appauthor
  34. """
  35. The name of the app author or distributing body for this application. Typically, it is the owning company name.
  36. Defaults to `appname`. You may pass ``False`` to disable it.
  37. """
  38. self.version = version
  39. """
  40. An optional version path element to append to the path. You might want to use this if you want multiple versions
  41. of your app to be able to run independently. If used, this would typically be ``<major>.<minor>``.
  42. """
  43. self.roaming = roaming
  44. """
  45. Whether to use the roaming appdata directory on Windows. That means that for users on a Windows network setup
  46. for roaming profiles, this user data will be synced on login (see
  47. `here <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>`_).
  48. """
  49. self.multipath = multipath
  50. """
  51. An optional parameter only applicable to Unix/Linux which indicates that the entire list of data dirs should be
  52. returned. By default, the first item would only be returned.
  53. """
  54. self.opinion = opinion #: A flag to indicating to use opinionated values.
  55. self.ensure_exists = ensure_exists
  56. """
  57. Optionally create the directory (and any missing parents) upon access if it does not exist.
  58. By default, no directories are created.
  59. """
  60. def _append_app_name_and_version(self, *base: str) -> str:
  61. params = list(base[1:])
  62. if self.appname:
  63. params.append(self.appname)
  64. if self.version:
  65. params.append(self.version)
  66. path = os.path.join(base[0], *params)
  67. self._optionally_create_directory(path)
  68. return path
  69. def _optionally_create_directory(self, path: str) -> None:
  70. if self.ensure_exists:
  71. Path(path).mkdir(parents=True, exist_ok=True)
  72. @property
  73. @abstractmethod
  74. def user_data_dir(self) -> str:
  75. """:return: data directory tied to the user"""
  76. @property
  77. @abstractmethod
  78. def site_data_dir(self) -> str:
  79. """:return: data directory shared by users"""
  80. @property
  81. @abstractmethod
  82. def user_config_dir(self) -> str:
  83. """:return: config directory tied to the user"""
  84. @property
  85. @abstractmethod
  86. def site_config_dir(self) -> str:
  87. """:return: config directory shared by the users"""
  88. @property
  89. @abstractmethod
  90. def user_cache_dir(self) -> str:
  91. """:return: cache directory tied to the user"""
  92. @property
  93. @abstractmethod
  94. def site_cache_dir(self) -> str:
  95. """:return: cache directory shared by users"""
  96. @property
  97. @abstractmethod
  98. def user_state_dir(self) -> str:
  99. """:return: state directory tied to the user"""
  100. @property
  101. @abstractmethod
  102. def user_log_dir(self) -> str:
  103. """:return: log directory tied to the user"""
  104. @property
  105. @abstractmethod
  106. def user_documents_dir(self) -> str:
  107. """:return: documents directory tied to the user"""
  108. @property
  109. @abstractmethod
  110. def user_runtime_dir(self) -> str:
  111. """:return: runtime directory tied to the user"""
  112. @property
  113. def user_data_path(self) -> Path:
  114. """:return: data path tied to the user"""
  115. return Path(self.user_data_dir)
  116. @property
  117. def site_data_path(self) -> Path:
  118. """:return: data path shared by users"""
  119. return Path(self.site_data_dir)
  120. @property
  121. def user_config_path(self) -> Path:
  122. """:return: config path tied to the user"""
  123. return Path(self.user_config_dir)
  124. @property
  125. def site_config_path(self) -> Path:
  126. """:return: config path shared by the users"""
  127. return Path(self.site_config_dir)
  128. @property
  129. def user_cache_path(self) -> Path:
  130. """:return: cache path tied to the user"""
  131. return Path(self.user_cache_dir)
  132. @property
  133. def site_cache_path(self) -> Path:
  134. """:return: cache path shared by users"""
  135. return Path(self.site_cache_dir)
  136. @property
  137. def user_state_path(self) -> Path:
  138. """:return: state path tied to the user"""
  139. return Path(self.user_state_dir)
  140. @property
  141. def user_log_path(self) -> Path:
  142. """:return: log path tied to the user"""
  143. return Path(self.user_log_dir)
  144. @property
  145. def user_documents_path(self) -> Path:
  146. """:return: documents path tied to the user"""
  147. return Path(self.user_documents_dir)
  148. @property
  149. def user_runtime_path(self) -> Path:
  150. """:return: runtime path tied to the user"""
  151. return Path(self.user_runtime_dir)