Skip to content

Commit

Permalink
Unix: Fallback to default if XDG environment variable is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
papr committed Jul 28, 2021
1 parent f9c2074 commit 6e66a3f
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def user_data_dir(self) -> str:
:return: data directory tied to the user, e.g. ``~/.local/share/$appname/$version`` or
``$XDG_DATA_HOME/$appname/$version``
"""
if "XDG_DATA_HOME" in os.environ:
path = os.environ["XDG_DATA_HOME"]
else:
path = os.path.expanduser("~/.local/share")
path = self._xdg_dir_or_fallback_with_expanded_user_dir("XDG_DATA_HOME", "~/.local/share")
return self._append_app_name_and_version(path)

@property
Expand All @@ -36,10 +33,9 @@ def site_data_dir(self) -> str:
path separator), e.g. ``/usr/local/share/$appname/$version`` or ``/usr/share/$appname/$version``
"""
# XDG default for $XDG_DATA_DIRS; only first, if multipath is False
if "XDG_DATA_DIRS" in os.environ:
path = os.environ["XDG_DATA_DIRS"]
else:
path = f"/usr/local/share{os.pathsep}/usr/share"
path = self._xdg_dir_or_fallback_with_expanded_user_dir(
"XDG_DATA_DIRS", f"/usr/local/share{os.pathsep}/usr/share"
)
return self._with_multi_path(path)

def _with_multi_path(self, path: str) -> str:
Expand All @@ -55,10 +51,7 @@ def user_config_dir(self) -> str:
:return: config directory tied to the user, e.g. ``~/.config/$appname/$version`` or
``$XDG_CONFIG_HOME/$appname/$version``
"""
if "XDG_CONFIG_HOME" in os.environ:
path = os.environ["XDG_CONFIG_HOME"]
else:
path = os.path.expanduser("~/.config")
path = self._xdg_dir_or_fallback_with_expanded_user_dir("XDG_CONFIG_HOME", "~/.config")
return self._append_app_name_and_version(path)

@property
Expand All @@ -69,10 +62,7 @@ def site_config_dir(self) -> str:
path separator), e.g. ``/etc/xdg/$appname/$version``
"""
# XDG default for $XDG_CONFIG_DIRS only first, if multipath is False
if "XDG_CONFIG_DIRS" in os.environ:
path = os.environ["XDG_CONFIG_DIRS"]
else:
path = "/etc/xdg"
path = self._xdg_dir_or_fallback_with_expanded_user_dir("XDG_CONFIG_DIRS", "/etc/xdg")
return self._with_multi_path(path)

@property
Expand All @@ -81,10 +71,7 @@ def user_cache_dir(self) -> str:
:return: cache directory tied to the user, e.g. ``~/.cache/$appname/$version`` or
``~/$XDG_CACHE_HOME/$appname/$version``
"""
if "XDG_CACHE_HOME" in os.environ:
path = os.environ["XDG_CACHE_HOME"]
else:
path = os.path.expanduser("~/.cache")
path = self._xdg_dir_or_fallback_with_expanded_user_dir("XDG_CACHE_HOME", "~/.cache")
return self._append_app_name_and_version(path)

@property
Expand All @@ -93,10 +80,7 @@ def user_state_dir(self) -> str:
:return: state directory tied to the user, e.g. ``~/.local/state/$appname/$version`` or
``$XDG_STATE_HOME/$appname/$version``
"""
if "XDG_STATE_HOME" in os.environ:
path = os.environ["XDG_STATE_HOME"]
else:
path = os.path.expanduser("~/.local/state")
path = self._xdg_dir_or_fallback_with_expanded_user_dir("XDG_STATE_HOME", "~/.local/state")
return self._append_app_name_and_version(path)

@property
Expand Down Expand Up @@ -125,6 +109,10 @@ def _first_item_as_path_if_multipath(self, directory: str) -> Path:
directory = directory.split(os.pathsep)[0]
return Path(directory)

@staticmethod
def _xdg_dir_or_fallback_with_expanded_user_dir(xdg_variable: str, fallback: str) -> str:
return os.environ.get(xdg_variable) or os.path.expanduser(fallback)


__all__ = [
"Unix",
Expand Down

0 comments on commit 6e66a3f

Please sign in to comment.