From 33e519e176dbe9bd4250a3dd3dd2d8d415b4617d Mon Sep 17 00:00:00 2001 From: Andrew Zhou <0az@afzhou.com> Date: Sat, 4 Apr 2020 15:45:30 -0500 Subject: [PATCH 1/2] Add support for XDG_CONFIG_HOME --- jupyter_core/paths.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/jupyter_core/paths.py b/jupyter_core/paths.py index 40e7e64..93e03c0 100644 --- a/jupyter_core/paths.py +++ b/jupyter_core/paths.py @@ -53,7 +53,8 @@ def _mkdtemp_once(name): def jupyter_config_dir(): """Get the Jupyter config directory for this platform and user. - Returns JUPYTER_CONFIG_DIR if defined, else ~/.jupyter + Returns the first valid path from JUPYTER_CONFIG_DIR if defined, ~/.jupyter + if it exists, and XDG_CONFIG_HOME if defined, falling back to ~/.jupyter. """ env = os.environ @@ -65,8 +66,16 @@ def jupyter_config_dir(): if env.get('JUPYTER_CONFIG_DIR'): return env['JUPYTER_CONFIG_DIR'] - return pjoin(home_dir, '.jupyter') + default = pjoin(home_dir, '.jupyter') + xdg = env.get('XDG_CONFIG_HOME') + # Check XDG_CONFIG_HOME if and only if .jupyter doesn't exist. + # This ensures that existing installs keep working until explicit opt-in. + if not exists(default) and xdg: + return pjoin(xdg, 'jupyter') + + return default + def jupyter_data_dir(): """Get the config directory for Jupyter data files. From 8c2f87f0e8133d3c8c2df39d7e6e21ca2a0c6184 Mon Sep 17 00:00:00 2001 From: Andrew Zhou <0az@afzhou.com> Date: Sat, 4 Apr 2020 17:30:37 -0500 Subject: [PATCH 2/2] Add support for XDG_DATA_HOME --- jupyter_core/paths.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/jupyter_core/paths.py b/jupyter_core/paths.py index 93e03c0..dcb45e1 100644 --- a/jupyter_core/paths.py +++ b/jupyter_core/paths.py @@ -90,21 +90,26 @@ def jupyter_data_dir(): return env['JUPYTER_DATA_DIR'] home = get_home_dir() + xdg = env.get('XDG_DATA_HOME') if sys.platform == 'darwin': - return os.path.join(home, 'Library', 'Jupyter') - elif os.name == 'nt': + default = pjoin(home, 'Library', 'Jupyter') + + # Use XDG_DATA_HOME if and only if the default doesn't exist. + if not exists(default) and xdg: + return pjoin(xdg, 'jupyter') + return default + + if os.name == 'nt': appdata = os.environ.get('APPDATA', None) if appdata: return pjoin(appdata, 'jupyter') - else: - return pjoin(jupyter_config_dir(), 'data') - else: - # Linux, non-OS X Unix, AIX, etc. - xdg = env.get("XDG_DATA_HOME", None) - if not xdg: - xdg = pjoin(home, '.local', 'share') - return pjoin(xdg, 'jupyter') + return pjoin(jupyter_config_dir(), 'data') + + # Linux, non-OS X Unix, AIX, etc. + if not xdg: + xdg = pjoin(home, '.local', 'share') + return pjoin(xdg, 'jupyter') def jupyter_runtime_dir():