|
| 1 | +# Copyright © 2016-2021 Scott Stevenson <[email protected]> |
| 2 | +# Modifications copyright © 2022 onwards Jeremy Howard |
| 3 | + |
| 4 | +"""XDG Base Directory Specification variables. |
| 5 | +
|
| 6 | +xdg_cache_home(), xdg_config_home(), xdg_data_home(), and xdg_state_home() |
| 7 | +return pathlib.Path objects containing the value of the environment variable |
| 8 | +named XDG_CACHE_HOME, XDG_CONFIG_HOME, XDG_DATA_HOME, and XDG_STATE_HOME |
| 9 | +respectively, or the default defined in the specification if the environment |
| 10 | +variable is unset, empty, or contains a relative path rather than absolute |
| 11 | +path. |
| 12 | +
|
| 13 | +xdg_config_dirs() and xdg_data_dirs() return a list of pathlib.Path |
| 14 | +objects containing the value, split on colons, of the environment |
| 15 | +variable named XDG_CONFIG_DIRS and XDG_DATA_DIRS respectively, or the |
| 16 | +default defined in the specification if the environment variable is |
| 17 | +unset or empty. Relative paths are ignored, as per the specification. |
| 18 | +
|
| 19 | +xdg_runtime_dir() returns a pathlib.Path object containing the value of |
| 20 | +the XDG_RUNTIME_DIR environment variable, or None if the environment |
| 21 | +variable is not set, or contains a relative path rather than absolute path. |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +from pathlib import Path |
| 26 | +from typing import List, Optional |
| 27 | + |
| 28 | +__all__ = [ "xdg_cache_home", "xdg_config_dirs", "xdg_config_home", "xdg_data_dirs", "xdg_data_home", "xdg_runtime_dir", "xdg_state_home", |
| 29 | + "XDG_CACHE_HOME", "XDG_CONFIG_DIRS", "XDG_CONFIG_HOME", "XDG_DATA_DIRS", "XDG_DATA_HOME", "XDG_RUNTIME_DIR" ] |
| 30 | + |
| 31 | + |
| 32 | +def _path_from_env(variable: str, default: Path) -> Path: |
| 33 | + value = os.environ.get(variable) |
| 34 | + if value and os.path.isabs(value): return Path(value) |
| 35 | + return default |
| 36 | + |
| 37 | +def _paths_from_env(variable: str, default: List[Path]) -> List[Path]: |
| 38 | + value = os.environ.get(variable) |
| 39 | + if value: |
| 40 | + paths = [ Path(path) for path in value.split(":") if os.path.isabs(path) ] |
| 41 | + if paths: return paths |
| 42 | + return default |
| 43 | + |
| 44 | +def xdg_cache_home() -> Path: |
| 45 | + """Path corresponding to XDG_CACHE_HOME.""" |
| 46 | + return _path_from_env("XDG_CACHE_HOME", Path.home()/".cache") |
| 47 | + |
| 48 | +def xdg_config_dirs() -> List[Path]: |
| 49 | + """Paths corresponding to XDG_CONFIG_DIRS.""" |
| 50 | + return _paths_from_env("XDG_CONFIG_DIRS", [Path("/etc/xdg")]) |
| 51 | + |
| 52 | +def xdg_config_home() -> Path: |
| 53 | + """Path corresponding to XDG_CONFIG_HOME.""" |
| 54 | + return _path_from_env("XDG_CONFIG_HOME", Path.home()/".config") |
| 55 | + |
| 56 | +def xdg_data_dirs() -> List[Path]: |
| 57 | + """Paths corresponding to XDG_DATA_DIRS.""" |
| 58 | + return _paths_from_env( "XDG_DATA_DIRS", [Path(path) for path in "/usr/local/share/:/usr/share/".split(":")]) |
| 59 | + |
| 60 | +def xdg_data_home() -> Path: |
| 61 | + """Path corresponding to XDG_DATA_HOME.""" |
| 62 | + return _path_from_env("XDG_DATA_HOME", Path.home()/".local"/"share") |
| 63 | + |
| 64 | +def xdg_runtime_dir() -> Optional[Path]: |
| 65 | + """Path corresponding to XDG_RUNTIME_DIR. """ |
| 66 | + value = os.getenv("XDG_RUNTIME_DIR") |
| 67 | + return Path(value) if value and os.path.isabs(value) else None |
| 68 | + |
| 69 | +def xdg_state_home() -> Path: |
| 70 | + """Path corresponding to XDG_STATE_HOME.""" |
| 71 | + return _path_from_env("XDG_STATE_HOME", Path.home()/".local"/"state") |
| 72 | + |
0 commit comments