2from __future__
import annotations
6from configparser
import ConfigParser
7from pathlib
import Path
9from .api
import PlatformDirsABC
14 msg =
"should only be used on Unix"
15 raise RuntimeError(msg)
23 On Unix/Linux, we follow the
24 `XDG Basedir Spec <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_. The spec allows
25 overriding directories with environment variables. The examples show are the default values, alongside the name of
26 the environment variable that overrides them. Makes use of the
27 `appname <platformdirs.api.PlatformDirsABC.appname>`,
28 `version <platformdirs.api.PlatformDirsABC.version>`,
29 `multipath <platformdirs.api.PlatformDirsABC.multipath>`,
30 `opinion <platformdirs.api.PlatformDirsABC.opinion>`,
31 `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
37 :return: data directory tied to the user, e.g. ``~/.local/share/$appname/$version`` or
38 ``$XDG_DATA_HOME/$appname/$version``
48 :return: data directories shared by users (if `multipath <platformdirs.api.PlatformDirsABC.multipath>` is
49 enabled and ``XDG_DATA_DIR`` is set and a multi path the response is also a multi path separated by the OS
50 path separator), e.g. ``/usr/local/share/$appname/$version`` or ``/usr/share/$appname/$version``
55 path = f
"/usr/local/share{os.pathsep}/usr/share"
61 path_list = path_list[0:1]
68 :return: config directory tied to the user, e.g. ``~/.config/$appname/$version`` or
69 ``$XDG_CONFIG_HOME/$appname/$version``
79 :return: config directories shared by users (if `multipath <platformdirs.api.PlatformDirsABC.multipath>`
80 is enabled and ``XDG_DATA_DIR`` is set and a multi path the response is also a multi path separated by the OS
81 path separator), e.g. ``/etc/xdg/$appname/$version``
92 :return: cache directory tied to the user, e.g. ``~/.cache/$appname/$version`` or
93 ``~/$XDG_CACHE_HOME/$appname/$version``
102 """:return: cache directory shared by users, e.g. ``/var/tmp/$appname/$version``"""
108 :return: state directory tied to the user, e.g. ``~/.local/state/$appname/$version`` or
109 ``$XDG_STATE_HOME/$appname/$version``
118 """:return: log directory tied to the user, same as `user_state_dir` if not opinionated else ``log`` in it"""
126 """:return: documents directory tied to the user, e.g. ``~/Documents``"""
131 """:return: downloads directory tied to the user, e.g. ``~/Downloads``"""
136 """:return: pictures directory tied to the user, e.g. ``~/Pictures``"""
141 """:return: videos directory tied to the user, e.g. ``~/Videos``"""
146 """:return: music directory tied to the user, e.g. ``~/Music``"""
152 :return: runtime directory tied to the user, e.g. ``/run/user/$(id -u)/$appname/$version`` or
153 ``$XDG_RUNTIME_DIR/$appname/$version``.
155 For FreeBSD/OpenBSD/NetBSD, it would return ``/var/run/user/$(id -u)/$appname/$version`` if
156 exists, otherwise ``/tmp/runtime-$(id -u)/$appname/$version``, if``$XDG_RUNTIME_DIR``
162 path = f
"/var/run/user/{getuid()}"
163 if not Path(path).exists():
164 path = f
"/tmp/runtime-{getuid()}"
166 path = f
"/run/user/{getuid()}"
171 """:return: data path shared by users. Only return first item, even if ``multipath`` is set to ``True``"""
176 """:return: config path shared by the users. Only return first item, even if ``multipath`` is set to ``True``"""
181 """:return: cache path shared by users. Only return first item, even if ``multipath`` is set to ``True``"""
188 return Path(directory)
193 if media_dir
is None:
202 """Return directory from user-dirs.dirs config file. See https://freedesktop.org/wiki/Software/xdg-user-dirs/."""
203 user_dirs_config_path = Path(
Unix().user_config_dir) /
"user-dirs.dirs"
205 parser = ConfigParser()
211 if key
not in parser[
"top"]:
214 path = parser[
"top"][key].strip(
'"')