m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
7.0 KiB

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