from contextlib import contextmanagerXDG
XDG Base Directory Specification helpers.
Each helper reads one XDG_* environment variable and falls back to the spec’s default when the variable is unset or empty. The single-path helpers also fall back when the variable holds a relative path. The *_DIRS helpers split the variable on colons and drop relative entries. xdg_runtime_dir has no default and returns None.
See the XDG Base Directory Specification for more information.
Helpers
We’ll start by defining a context manager that temporarily sets an environment variable to demonstrate the behaviour of each helper function:
@contextmanager
def env(variable, value):
old = os.environ.get(variable, None)
try:
os.environ[variable] = value
yield
finally:
if old is None: del os.environ[variable]
else: os.environ[variable] = oldfrom fastcore.test import *test_eq(xdg_cache_home(), Path.home()/'.cache')
with env('XDG_CACHE_HOME', '/home/fastai/.cache'):
test_eq(xdg_cache_home(), Path('/home/fastai/.cache'))test_eq(xdg_config_dirs(), [Path('/etc/xdg')])
with env('XDG_CONFIG_DIRS', '/home/fastai/.xdg:/home/fastai/.config'):
test_eq(xdg_config_dirs(), [Path('/home/fastai/.xdg'), Path('/home/fastai/.config')])test_eq(xdg_config_home(), Path.home()/'.config')
with env('XDG_CONFIG_HOME', '/home/fastai/.config'):
test_eq(xdg_config_home(), Path('/home/fastai/.config'))test_eq(xdg_data_home(), Path.home()/'.local/share')
with env('XDG_DATA_HOME', '/home/fastai/.data'):
test_eq(xdg_data_home(), Path('/home/fastai/.data'))test_eq(xdg_state_home(), Path.home()/'.local/state')
with env('XDG_STATE_HOME', '/home/fastai/.state'):
test_eq(xdg_state_home(), Path('/home/fastai/.state'))Copyright © 2016-2021 Scott Stevenson [email protected]
Modifications copyright © 2022 onwards Jeremy Howard