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

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

89 statements  

1"""XDG environment variable mixin for Unix and macOS.""" 

2 

3from __future__ import annotations 

4 

5import os 

6 

7from .api import PlatformDirsABC 

8 

9 

10class XDGMixin(PlatformDirsABC): 

11 """Mixin that checks XDG environment variables, falling back to platform-specific defaults via ``super()``.""" 

12 

13 @property 

14 def user_data_dir(self) -> str: 

15 """:return: data directory tied to the user, from ``$XDG_DATA_HOME`` if set, else platform default""" 

16 if path := os.environ.get("XDG_DATA_HOME", "").strip(): 

17 return self._append_app_name_and_version(path) 

18 return super().user_data_dir 

19 

20 @property 

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

22 if xdg_dirs := os.environ.get("XDG_DATA_DIRS", "").strip(): 

23 return [self._append_app_name_and_version(p) for p in xdg_dirs.split(os.pathsep) if p.strip()] 

24 return super()._site_data_dirs # type: ignore[misc] 

25 

26 @property 

27 def site_data_dir(self) -> str: 

28 """:return: data directories shared by users, from ``$XDG_DATA_DIRS`` if set, else platform default""" 

29 dirs = self._site_data_dirs 

30 return os.pathsep.join(dirs) if self.multipath else dirs[0] 

31 

32 @property 

33 def user_config_dir(self) -> str: 

34 """:return: config directory tied to the user, from ``$XDG_CONFIG_HOME`` if set, else platform default""" 

35 if path := os.environ.get("XDG_CONFIG_HOME", "").strip(): 

36 return self._append_app_name_and_version(path) 

37 return super().user_config_dir 

38 

39 @property 

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

41 if xdg_dirs := os.environ.get("XDG_CONFIG_DIRS", "").strip(): 

42 return [self._append_app_name_and_version(p) for p in xdg_dirs.split(os.pathsep) if p.strip()] 

43 return super()._site_config_dirs # type: ignore[misc] 

44 

45 @property 

46 def site_config_dir(self) -> str: 

47 """:return: config directories shared by users, from ``$XDG_CONFIG_DIRS`` if set, else platform default""" 

48 dirs = self._site_config_dirs 

49 return os.pathsep.join(dirs) if self.multipath else dirs[0] 

50 

51 @property 

52 def user_cache_dir(self) -> str: 

53 """:return: cache directory tied to the user, from ``$XDG_CACHE_HOME`` if set, else platform default""" 

54 if path := os.environ.get("XDG_CACHE_HOME", "").strip(): 

55 return self._append_app_name_and_version(path) 

56 return super().user_cache_dir 

57 

58 @property 

59 def user_state_dir(self) -> str: 

60 """:return: state directory tied to the user, from ``$XDG_STATE_HOME`` if set, else platform default""" 

61 if path := os.environ.get("XDG_STATE_HOME", "").strip(): 

62 return self._append_app_name_and_version(path) 

63 return super().user_state_dir 

64 

65 @property 

66 def user_runtime_dir(self) -> str: 

67 """:return: runtime directory tied to the user, from ``$XDG_RUNTIME_DIR`` if set, else platform default""" 

68 if path := os.environ.get("XDG_RUNTIME_DIR", "").strip(): 

69 return self._append_app_name_and_version(path) 

70 return super().user_runtime_dir 

71 

72 @property 

73 def site_runtime_dir(self) -> str: 

74 """:return: runtime directory shared by users, from ``$XDG_RUNTIME_DIR`` if set, else platform default""" 

75 if path := os.environ.get("XDG_RUNTIME_DIR", "").strip(): 

76 return self._append_app_name_and_version(path) 

77 return super().site_runtime_dir 

78 

79 @property 

80 def user_documents_dir(self) -> str: 

81 """:return: documents directory tied to the user, from ``$XDG_DOCUMENTS_DIR`` if set, else platform default""" 

82 if path := os.environ.get("XDG_DOCUMENTS_DIR", "").strip(): 

83 return os.path.expanduser(path) # noqa: PTH111 

84 return super().user_documents_dir 

85 

86 @property 

87 def user_downloads_dir(self) -> str: 

88 """:return: downloads directory tied to the user, from ``$XDG_DOWNLOAD_DIR`` if set, else platform default""" 

89 if path := os.environ.get("XDG_DOWNLOAD_DIR", "").strip(): 

90 return os.path.expanduser(path) # noqa: PTH111 

91 return super().user_downloads_dir 

92 

93 @property 

94 def user_pictures_dir(self) -> str: 

95 """:return: pictures directory tied to the user, from ``$XDG_PICTURES_DIR`` if set, else platform default""" 

96 if path := os.environ.get("XDG_PICTURES_DIR", "").strip(): 

97 return os.path.expanduser(path) # noqa: PTH111 

98 return super().user_pictures_dir 

99 

100 @property 

101 def user_videos_dir(self) -> str: 

102 """:return: videos directory tied to the user, from ``$XDG_VIDEOS_DIR`` if set, else platform default""" 

103 if path := os.environ.get("XDG_VIDEOS_DIR", "").strip(): 

104 return os.path.expanduser(path) # noqa: PTH111 

105 return super().user_videos_dir 

106 

107 @property 

108 def user_music_dir(self) -> str: 

109 """:return: music directory tied to the user, from ``$XDG_MUSIC_DIR`` if set, else platform default""" 

110 if path := os.environ.get("XDG_MUSIC_DIR", "").strip(): 

111 return os.path.expanduser(path) # noqa: PTH111 

112 return super().user_music_dir 

113 

114 @property 

115 def user_desktop_dir(self) -> str: 

116 """:return: desktop directory tied to the user, from ``$XDG_DESKTOP_DIR`` if set, else platform default""" 

117 if path := os.environ.get("XDG_DESKTOP_DIR", "").strip(): 

118 return os.path.expanduser(path) # noqa: PTH111 

119 return super().user_desktop_dir 

120 

121 @property 

122 def user_applications_dir(self) -> str: 

123 """:return: applications directory tied to the user, from ``$XDG_DATA_HOME`` if set, else platform default""" 

124 if path := os.environ.get("XDG_DATA_HOME", "").strip(): 

125 return os.path.join(os.path.expanduser(path), "applications") # noqa: PTH111, PTH118 

126 return super().user_applications_dir 

127 

128 

129__all__ = [ 

130 "XDGMixin", 

131]