_xdg.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """XDG environment variable mixin for Unix and macOS."""
  2. from __future__ import annotations
  3. import os
  4. from .api import PlatformDirsABC
  5. class XDGMixin(PlatformDirsABC):
  6. """Mixin that checks XDG environment variables, falling back to platform-specific defaults via ``super()``."""
  7. @property
  8. def user_data_dir(self) -> str:
  9. """:returns: data directory tied to the user, from ``$XDG_DATA_HOME`` if set, else platform default"""
  10. if path := os.environ.get("XDG_DATA_HOME", "").strip():
  11. return self._append_app_name_and_version(path)
  12. return super().user_data_dir
  13. @property
  14. def _site_data_dirs(self) -> list[str]:
  15. if xdg_dirs := os.environ.get("XDG_DATA_DIRS", "").strip():
  16. return [self._append_app_name_and_version(p) for p in xdg_dirs.split(os.pathsep) if p.strip()]
  17. return super()._site_data_dirs # type: ignore[misc]
  18. @property
  19. def site_data_dir(self) -> str:
  20. """:returns: data directories shared by users, from ``$XDG_DATA_DIRS`` if set, else platform default"""
  21. dirs = self._site_data_dirs
  22. return os.pathsep.join(dirs) if self.multipath else dirs[0]
  23. @property
  24. def user_config_dir(self) -> str:
  25. """:returns: config directory tied to the user, from ``$XDG_CONFIG_HOME`` if set, else platform default"""
  26. if path := os.environ.get("XDG_CONFIG_HOME", "").strip():
  27. return self._append_app_name_and_version(path)
  28. return super().user_config_dir
  29. @property
  30. def _site_config_dirs(self) -> list[str]:
  31. if xdg_dirs := os.environ.get("XDG_CONFIG_DIRS", "").strip():
  32. return [self._append_app_name_and_version(p) for p in xdg_dirs.split(os.pathsep) if p.strip()]
  33. return super()._site_config_dirs # type: ignore[misc]
  34. @property
  35. def site_config_dir(self) -> str:
  36. """:returns: config directories shared by users, from ``$XDG_CONFIG_DIRS`` if set, else platform default"""
  37. dirs = self._site_config_dirs
  38. return os.pathsep.join(dirs) if self.multipath else dirs[0]
  39. @property
  40. def user_cache_dir(self) -> str:
  41. """:returns: cache directory tied to the user, from ``$XDG_CACHE_HOME`` if set, else platform default"""
  42. if path := os.environ.get("XDG_CACHE_HOME", "").strip():
  43. return self._append_app_name_and_version(path)
  44. return super().user_cache_dir
  45. @property
  46. def user_state_dir(self) -> str:
  47. """:returns: state directory tied to the user, from ``$XDG_STATE_HOME`` if set, else platform default"""
  48. if path := os.environ.get("XDG_STATE_HOME", "").strip():
  49. return self._append_app_name_and_version(path)
  50. return super().user_state_dir
  51. @property
  52. def user_runtime_dir(self) -> str:
  53. """:returns: runtime directory tied to the user, from ``$XDG_RUNTIME_DIR`` if set, else platform default"""
  54. if path := os.environ.get("XDG_RUNTIME_DIR", "").strip():
  55. return self._append_app_name_and_version(path)
  56. return super().user_runtime_dir
  57. @property
  58. def site_runtime_dir(self) -> str:
  59. """:returns: runtime directory shared by users, from ``$XDG_RUNTIME_DIR`` if set, else platform default"""
  60. if path := os.environ.get("XDG_RUNTIME_DIR", "").strip():
  61. return self._append_app_name_and_version(path)
  62. return super().site_runtime_dir
  63. @property
  64. def user_documents_dir(self) -> str:
  65. """:returns: documents directory tied to the user, from ``$XDG_DOCUMENTS_DIR`` if set, else platform default"""
  66. if path := os.environ.get("XDG_DOCUMENTS_DIR", "").strip():
  67. return os.path.expanduser(path) # noqa: PTH111
  68. return super().user_documents_dir
  69. @property
  70. def user_downloads_dir(self) -> str:
  71. """:returns: downloads directory tied to the user, from ``$XDG_DOWNLOAD_DIR`` if set, else platform default"""
  72. if path := os.environ.get("XDG_DOWNLOAD_DIR", "").strip():
  73. return os.path.expanduser(path) # noqa: PTH111
  74. return super().user_downloads_dir
  75. @property
  76. def user_pictures_dir(self) -> str:
  77. """:returns: pictures directory tied to the user, from ``$XDG_PICTURES_DIR`` if set, else platform default"""
  78. if path := os.environ.get("XDG_PICTURES_DIR", "").strip():
  79. return os.path.expanduser(path) # noqa: PTH111
  80. return super().user_pictures_dir
  81. @property
  82. def user_videos_dir(self) -> str:
  83. """:returns: videos directory tied to the user, from ``$XDG_VIDEOS_DIR`` if set, else platform default"""
  84. if path := os.environ.get("XDG_VIDEOS_DIR", "").strip():
  85. return os.path.expanduser(path) # noqa: PTH111
  86. return super().user_videos_dir
  87. @property
  88. def user_music_dir(self) -> str:
  89. """:returns: music directory tied to the user, from ``$XDG_MUSIC_DIR`` if set, else platform default"""
  90. if path := os.environ.get("XDG_MUSIC_DIR", "").strip():
  91. return os.path.expanduser(path) # noqa: PTH111
  92. return super().user_music_dir
  93. @property
  94. def user_desktop_dir(self) -> str:
  95. """:returns: desktop directory tied to the user, from ``$XDG_DESKTOP_DIR`` if set, else platform default"""
  96. if path := os.environ.get("XDG_DESKTOP_DIR", "").strip():
  97. return os.path.expanduser(path) # noqa: PTH111
  98. return super().user_desktop_dir
  99. @property
  100. def user_applications_dir(self) -> str:
  101. """:returns: applications directory tied to the user, from ``$XDG_DATA_HOME`` if set, else platform default"""
  102. if path := os.environ.get("XDG_DATA_HOME", "").strip():
  103. return os.path.join(os.path.expanduser(path), "applications") # noqa: PTH111, PTH118
  104. return super().user_applications_dir
  105. @property
  106. def _site_applications_dirs(self) -> list[str]:
  107. if xdg_dirs := os.environ.get("XDG_DATA_DIRS", "").strip():
  108. return [os.path.join(p, "applications") for p in xdg_dirs.split(os.pathsep) if p.strip()] # noqa: PTH118
  109. return super()._site_applications_dirs # type: ignore[misc]
  110. @property
  111. def site_applications_dir(self) -> str:
  112. """:returns: applications directories shared by users, from ``$XDG_DATA_DIRS`` if set, else platform default"""
  113. dirs = self._site_applications_dirs
  114. return os.pathsep.join(dirs) if self.multipath else dirs[0]
  115. __all__ = [
  116. "XDGMixin",
  117. ]