-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathxdg.py
More file actions
64 lines (50 loc) · 2.54 KB
/
Copy pathxdg.py
File metadata and controls
64 lines (50 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""XDG Base Directory Specification helpers.
`xdg_cache_home`, `xdg_config_home`, `xdg_data_home`, `xdg_state_home`, and `xdg_runtime_dir` each return the `Path` given by their `XDG_*` environment variable, falling back to the [spec](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)'s default (`~/.cache`, `~/.config`, `~/.local/share`, `~/.local/state`; `None` for the runtime dir) when it's unset, empty, or relative. `xdg_config_dirs` and `xdg_data_dirs` likewise return the colon-split search-path lists.
Docs: https://fastcore.fast.ai/xdg.html.md"""
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/07_xdg.ipynb.
# %% auto #0
__all__ = ['xdg_cache_home', 'xdg_config_dirs', 'xdg_config_home', 'xdg_data_dirs', 'xdg_data_home', 'xdg_runtime_dir',
'xdg_state_home']
# %% ../nbs/07_xdg.ipynb #6520e8e7
from .utils import *
# %% ../nbs/07_xdg.ipynb #ec626e17
def _path_from_env(variable, default):
value = os.environ.get(variable)
if value and os.path.isabs(value): return Path(value)
return default
# %% ../nbs/07_xdg.ipynb #2231c39d
def _paths_from_env(variable, default):
value = os.environ.get(variable)
if value:
paths = [Path(o) for o in value.split(":") if os.path.isabs(o)]
if paths: return paths
return default
# %% ../nbs/07_xdg.ipynb #c57ecc16
def xdg_cache_home():
"Path corresponding to `XDG_CACHE_HOME`"
return _path_from_env("XDG_CACHE_HOME", Path.home()/".cache")
# %% ../nbs/07_xdg.ipynb #d2b25813
def xdg_config_dirs():
"Paths corresponding to `XDG_CONFIG_DIRS`"
return _paths_from_env("XDG_CONFIG_DIRS", [Path("/etc/xdg")])
# %% ../nbs/07_xdg.ipynb #5fb9fadd
def xdg_config_home():
"Path corresponding to `XDG_CONFIG_HOME`"
return _path_from_env("XDG_CONFIG_HOME", Path.home()/".config")
# %% ../nbs/07_xdg.ipynb #d4d26c69
def xdg_data_dirs():
"Paths corresponding to XDG_DATA_DIRS`"
return _paths_from_env( "XDG_DATA_DIRS", [Path(o) for o in "/usr/local/share/:/usr/share/".split(":")])
# %% ../nbs/07_xdg.ipynb #6d9c9c0b
def xdg_data_home():
"Path corresponding to `XDG_DATA_HOME`"
return _path_from_env("XDG_DATA_HOME", Path.home()/".local"/"share")
# %% ../nbs/07_xdg.ipynb #2ded0956
def xdg_runtime_dir():
"Path corresponding to `XDG_RUNTIME_DIR`"
value = os.getenv("XDG_RUNTIME_DIR")
return Path(value) if value and os.path.isabs(value) else None
# %% ../nbs/07_xdg.ipynb #9025dca2
def xdg_state_home():
"Path corresponding to `XDG_STATE_HOME`"
return _path_from_env("XDG_STATE_HOME", Path.home()/".local"/"state")