from contextlib import contextmanager
XDG
See the XDG Base Directory Specification for more information.
Overview
xdg_cache_home
, xdg_config_home
, xdg_data_home
, and xdg_state_home
return pathlib.Path
objects containing the value of the environment variable named XDG_CACHE_HOME
, XDG_CONFIG_HOME
, XDG_DATA_HOME
, and XDG_STATE_HOME
respectively, or the default defined in the specification if the environment variable is unset, empty, or contains a relative path rather than absolute path.
xdg_config_dirs
and xdg_data_dirs
return a list of pathlib.Path
objects containing the value, split on colons, of the environment variable named XDG_CONFIG_DIRS
and XDG_DATA_DIRS
respectively, or the default defined in the specification if the environment variable is unset or empty. Relative paths are ignored, as per the specification.
xdg_runtime_dir
returns a pathlib.Path
object containing the value of the XDG_RUNTIME_DIR
environment variable, or None
if the environment variable is not set, or contains a relative path rather than absolute path.
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):
= os.environ.get(variable, None)
old try:
= value
os.environ[variable] yield
finally:
if old is None: del os.environ[variable]
else: os.environ[variable] = old
xdg_cache_home
xdg_cache_home ()
Path corresponding to XDG_CACHE_HOME
from fastcore.test import *
/'.cache')
test_eq(xdg_cache_home(), Path.home()with env('XDG_CACHE_HOME', '/home/fastai/.cache'):
'/home/fastai/.cache')) test_eq(xdg_cache_home(), Path(
xdg_config_dirs
xdg_config_dirs ()
Paths corresponding to XDG_CONFIG_DIRS
'/etc/xdg')])
test_eq(xdg_config_dirs(), [Path(with env('XDG_CONFIG_DIRS', '/home/fastai/.xdg:/home/fastai/.config'):
'/home/fastai/.xdg'), Path('/home/fastai/.config')]) test_eq(xdg_config_dirs(), [Path(
xdg_config_home
xdg_config_home ()
Path corresponding to XDG_CONFIG_HOME
/'.config')
test_eq(xdg_config_home(), Path.home()with env('XDG_CONFIG_HOME', '/home/fastai/.config'):
'/home/fastai/.config')) test_eq(xdg_config_home(), Path(
xdg_data_dirs
xdg_data_dirs ()
Paths corresponding to XDG_DATA_DIRS`
xdg_data_home
xdg_data_home ()
Path corresponding to XDG_DATA_HOME
/'.local/share')
test_eq(xdg_data_home(), Path.home()with env('XDG_DATA_HOME', '/home/fastai/.data'):
'/home/fastai/.data')) test_eq(xdg_data_home(), Path(
xdg_runtime_dir
xdg_runtime_dir ()
Path corresponding to XDG_RUNTIME_DIR
xdg_state_home
xdg_state_home ()
Path corresponding to XDG_STATE_HOME
/'.local/state')
test_eq(xdg_state_home(), Path.home()with env('XDG_STATE_HOME', '/home/fastai/.state'):
'/home/fastai/.state')) test_eq(xdg_state_home(), Path(
Copyright © 2016-2021 Scott Stevenson [email protected]
Modifications copyright © 2022 onwards Jeremy Howard