api.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. ):
  21. """
  22. Create a new platform directory.
  23. :param appname: See `appname`.
  24. :param appauthor: See `appauthor`.
  25. :param version: See `version`.
  26. :param roaming: See `roaming`.
  27. :param multipath: See `multipath`.
  28. :param opinion: See `opinion`.
  29. """
  30. self.appname = appname #: The name of application.
  31. self.appauthor = appauthor
  32. """
  33. The name of the app author or distributing body for this application. Typically, it is the owning company name.
  34. Defaults to `appname`. You may pass ``False`` to disable it.
  35. """
  36. self.version = version
  37. """
  38. An optional version path element to append to the path. You might want to use this if you want multiple versions
  39. of your app to be able to run independently. If used, this would typically be ``<major>.<minor>``.
  40. """
  41. self.roaming = roaming
  42. """
  43. Whether to use the roaming appdata directory on Windows. That means that for users on a Windows network setup
  44. for roaming profiles, this user data will be synced on login (see
  45. `here <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>`_).
  46. """
  47. self.multipath = multipath
  48. """
  49. An optional parameter only applicable to Unix/Linux which indicates that the entire list of data dirs should be
  50. returned. By default, the first item would only be returned.
  51. """
  52. self.opinion = opinion #: A flag to indicating to use opinionated values.
  53. def _append_app_name_and_version(self, *base: str) -> str:
  54. params = list(base[1:])
  55. if self.appname:
  56. params.append(self.appname)
  57. if self.version:
  58. params.append(self.version)
  59. return os.path.join(base[0], *params)
  60. @property
  61. @abstractmethod
  62. def user_data_dir(self) -> str:
  63. """:return: data directory tied to the user"""
  64. @property
  65. @abstractmethod
  66. def site_data_dir(self) -> str:
  67. """:return: data directory shared by users"""
  68. @property
  69. @abstractmethod
  70. def user_config_dir(self) -> str:
  71. """:return: config directory tied to the user"""
  72. @property
  73. @abstractmethod
  74. def site_config_dir(self) -> str:
  75. """:return: config directory shared by the users"""
  76. @property
  77. @abstractmethod
  78. def user_cache_dir(self) -> str:
  79. """:return: cache directory tied to the user"""
  80. @property
  81. @abstractmethod
  82. def user_state_dir(self) -> str:
  83. """:return: state directory tied to the user"""
  84. @property
  85. @abstractmethod
  86. def user_log_dir(self) -> str:
  87. """:return: log directory tied to the user"""
  88. @property
  89. @abstractmethod
  90. def user_documents_dir(self) -> str:
  91. """:return: documents directory tied to the user"""
  92. @property
  93. @abstractmethod
  94. def user_runtime_dir(self) -> str:
  95. """:return: runtime directory tied to the user"""
  96. @property
  97. def user_data_path(self) -> Path:
  98. """:return: data path tied to the user"""
  99. return Path(self.user_data_dir)
  100. @property
  101. def site_data_path(self) -> Path:
  102. """:return: data path shared by users"""
  103. return Path(self.site_data_dir)
  104. @property
  105. def user_config_path(self) -> Path:
  106. """:return: config path tied to the user"""
  107. return Path(self.user_config_dir)
  108. @property
  109. def site_config_path(self) -> Path:
  110. """:return: config path shared by the users"""
  111. return Path(self.site_config_dir)
  112. @property
  113. def user_cache_path(self) -> Path:
  114. """:return: cache path tied to the user"""
  115. return Path(self.user_cache_dir)
  116. @property
  117. def user_state_path(self) -> Path:
  118. """:return: state path tied to the user"""
  119. return Path(self.user_state_dir)
  120. @property
  121. def user_log_path(self) -> Path:
  122. """:return: log path tied to the user"""
  123. return Path(self.user_log_dir)
  124. @property
  125. def user_documents_path(self) -> Path:
  126. """:return: documents path tied to the user"""
  127. return Path(self.user_documents_dir)
  128. @property
  129. def user_runtime_path(self) -> Path:
  130. """:return: runtime path tied to the user"""
  131. return Path(self.user_runtime_dir)