Skip to content

Commit 51be5af

Browse files
blink1073pre-commit-ci[bot]jasongrout
authored
Use platformdirs for path locations (#292)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jason Grout <[email protected]> Co-authored-by: Jason Grout <[email protected]>
1 parent ea89d5e commit 51be5af

File tree

6 files changed

+271
-102
lines changed

6 files changed

+271
-102
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ repos:
6060
- id: mypy
6161
args: ["--config-file", "pyproject.toml"]
6262
stages: [manual]
63-
additional_dependencies: [pytest]
63+
additional_dependencies: [pytest, platformdirs]
6464
exclude: |
6565
exclude: |
6666
(?x)^(

jupyter_core/command.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ def main():
231231
if args.debug:
232232
env = os.environ
233233

234+
if paths.use_platform_dirs():
235+
print(
236+
"JUPYTER_PLATFORM_DIRS is set to a true value, so we use platformdirs to find platform-specific directories"
237+
)
238+
else:
239+
print(
240+
"JUPYTER_PLATFORM_DIRS is set to a false value, or is not set, so we use hardcoded legacy paths for platform-specific directories"
241+
)
242+
234243
if paths.prefer_environment_over_user():
235244
print(
236245
"JUPYTER_PREFER_ENV_PATH is set to a true value, or JUPYTER_PREFER_ENV_PATH is not set and we detected a virtual environment, making the environment-level path preferred over the user-level path for data and config"

jupyter_core/paths.py

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
from pathlib import Path
2020
from typing import Optional
2121

22+
import platformdirs
23+
24+
from jupyter_core.utils import deprecation
25+
2226
pjoin = os.path.join
2327

28+
# Capitalize Jupyter in paths only on Windows and MacOS
29+
APPNAME = "Jupyter" if sys.platform in ("win32", "darwin") else "jupyter"
30+
2431
# UF_HIDDEN is a stat flag not defined in the stat module.
2532
# It is used by BSD to indicate hidden files.
2633
UF_HIDDEN = getattr(stat, "UF_HIDDEN", 32768)
@@ -40,6 +47,15 @@ def envset(name, default=False):
4047
return os.environ[name].lower() not in ["no", "n", "false", "off", "0", "0.0"]
4148

4249

50+
def use_platform_dirs():
51+
"""Determine if platformdirs should be used for system-specific paths.
52+
53+
We plan for this to default to False in jupyter_core version 5 and to True
54+
in jupyter_core version 6.
55+
"""
56+
return envset("JUPYTER_PLATFORM_DIRS", False)
57+
58+
4359
def get_home_dir():
4460
"""Get the real path of the home directory"""
4561
homedir = os.path.expanduser("~")
@@ -85,7 +101,8 @@ def _mkdtemp_once(name):
85101
def jupyter_config_dir():
86102
"""Get the Jupyter config directory for this platform and user.
87103
88-
Returns JUPYTER_CONFIG_DIR if defined, else ~/.jupyter
104+
Returns JUPYTER_CONFIG_DIR if defined, otherwise the appropriate
105+
directory for the platform.
89106
"""
90107

91108
env = os.environ
@@ -95,6 +112,9 @@ def jupyter_config_dir():
95112
if env.get("JUPYTER_CONFIG_DIR"):
96113
return env["JUPYTER_CONFIG_DIR"]
97114

115+
if use_platform_dirs():
116+
return platformdirs.user_config_dir(APPNAME, appauthor=False)
117+
98118
home_dir = get_home_dir()
99119
return pjoin(home_dir, ".jupyter")
100120

@@ -111,6 +131,9 @@ def jupyter_data_dir():
111131
if env.get("JUPYTER_DATA_DIR"):
112132
return env["JUPYTER_DATA_DIR"]
113133

134+
if use_platform_dirs():
135+
return platformdirs.user_data_dir(APPNAME, appauthor=False)
136+
114137
home = get_home_dir()
115138

116139
if sys.platform == "darwin":
@@ -145,17 +168,29 @@ def jupyter_runtime_dir():
145168
return pjoin(jupyter_data_dir(), "runtime")
146169

147170

148-
if os.name == "nt":
149-
programdata = os.environ.get("PROGRAMDATA", None)
150-
if programdata:
151-
SYSTEM_JUPYTER_PATH = [pjoin(programdata, "jupyter")]
152-
else: # PROGRAMDATA is not defined by default on XP.
153-
SYSTEM_JUPYTER_PATH = [os.path.join(sys.prefix, "share", "jupyter")]
171+
if use_platform_dirs():
172+
SYSTEM_JUPYTER_PATH = platformdirs.site_data_dir(
173+
APPNAME, appauthor=False, multipath=True
174+
).split(os.pathsep)
154175
else:
155-
SYSTEM_JUPYTER_PATH = [
156-
"/usr/local/share/jupyter",
157-
"/usr/share/jupyter",
158-
]
176+
deprecation(
177+
"Jupyter is migrating its paths to use standard platformdirs\n" # noqa
178+
+ "given by the platformdirs library. To remove this warning and\n"
179+
+ "see the appropriate new directories, set the environment variable\n"
180+
+ "`JUPYTER_PLATFORM_DIRS=1` and then run `jupyter --paths`.\n"
181+
+ "The use of platformdirs will be the default in `jupyter_core` v6"
182+
)
183+
if os.name == "nt":
184+
programdata = os.environ.get("PROGRAMDATA", None)
185+
if programdata:
186+
SYSTEM_JUPYTER_PATH = [pjoin(programdata, "jupyter")]
187+
else: # PROGRAMDATA is not defined by default on XP.
188+
SYSTEM_JUPYTER_PATH = [os.path.join(sys.prefix, "share", "jupyter")]
189+
else:
190+
SYSTEM_JUPYTER_PATH = [
191+
"/usr/local/share/jupyter",
192+
"/usr/share/jupyter",
193+
]
159194

160195
ENV_JUPYTER_PATH = [os.path.join(sys.prefix, "share", "jupyter")]
161196

@@ -222,18 +257,22 @@ def jupyter_path(*subdirs):
222257
return paths
223258

224259

225-
if os.name == "nt":
226-
programdata = os.environ.get("PROGRAMDATA", None)
227-
if programdata:
228-
SYSTEM_CONFIG_PATH = [os.path.join(programdata, "jupyter")]
229-
else: # PROGRAMDATA is not defined by default on XP.
230-
SYSTEM_CONFIG_PATH = []
260+
if use_platform_dirs():
261+
SYSTEM_CONFIG_PATH = platformdirs.site_config_dir(
262+
APPNAME, appauthor=False, multipath=True
263+
).split(os.pathsep)
231264
else:
232-
SYSTEM_CONFIG_PATH = [
233-
"/usr/local/etc/jupyter",
234-
"/etc/jupyter",
235-
]
236-
265+
if os.name == "nt":
266+
programdata = os.environ.get("PROGRAMDATA", None)
267+
if programdata:
268+
SYSTEM_CONFIG_PATH = [os.path.join(programdata, "jupyter")]
269+
else: # PROGRAMDATA is not defined by default on XP.
270+
SYSTEM_CONFIG_PATH = []
271+
else:
272+
SYSTEM_CONFIG_PATH = [
273+
"/usr/local/etc/jupyter",
274+
"/etc/jupyter",
275+
]
237276
ENV_CONFIG_PATH = [os.path.join(sys.prefix, "etc", "jupyter")]
238277

239278

0 commit comments

Comments
 (0)