Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platformdirs for path locations #292

Merged
merged 36 commits into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9ae146a
Use appdirs for path locations
blink1073 Sep 26, 2022
49c99dc
use user env and fix lint
blink1073 Sep 26, 2022
b73cd9d
try setting home
blink1073 Sep 26, 2022
07936a6
fix linux tests
blink1073 Sep 26, 2022
c10eacd
use platformdirs
blink1073 Sep 26, 2022
7a03788
add deprecation cycle
blink1073 Sep 27, 2022
ccb2f78
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2022
2f5d854
Use envset to check the boolean JUPYTER_PLATFORM_DIRS environment var…
jasongrout Sep 27, 2022
5338567
Add note to --debug text about JUPYTER_PLATFORM_DIRS
jasongrout Sep 27, 2022
e8b6cda
Fix system config directory
jasongrout Sep 27, 2022
28275a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2022
236dd7c
Make data directory not end in "data" for compatibility with legacy d…
jasongrout Sep 27, 2022
e43a162
Set multipath to true for site directories.
jasongrout Sep 27, 2022
b556baa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2022
2104e47
Experiment: only capitalize Jupyter in paths for windows and macos
jasongrout Sep 27, 2022
f494b8e
Fix xdg tests by making xdg directories absolute paths.
jasongrout Sep 27, 2022
a560f7a
fix tests
blink1073 Sep 27, 2022
6aa5cd7
fix test
blink1073 Sep 27, 2022
063b330
fix test
blink1073 Sep 27, 2022
0fc4b69
lint
blink1073 Sep 27, 2022
685000a
remove todo
blink1073 Sep 27, 2022
c01b1be
Change platform test skips to convenience decorators
jasongrout Sep 27, 2022
b3ea511
Change test platformdirs env patch to convenience context manager
jasongrout Sep 27, 2022
814580a
Consolidate config tests
jasongrout Sep 27, 2022
ae5f2b9
Simplify data dir tests
jasongrout Sep 27, 2022
be2d066
Simplify runtime path testing
jasongrout Sep 27, 2022
a982b99
Clean up test formatting and names
jasongrout Sep 27, 2022
2164bd0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2022
47d174f
Consolidate config dir testing
jasongrout Sep 27, 2022
8092283
Test config directories
jasongrout Sep 27, 2022
025999f
Better window test expectations
jasongrout Sep 27, 2022
98d6fb7
Fix some tests
jasongrout Sep 27, 2022
69e053d
Fix test: Windows uses local appdata under platformdirs
jasongrout Sep 27, 2022
0bf2040
Fix windows appdata tests
jasongrout Sep 27, 2022
280da04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2022
a427fc4
Update jupyter_core/utils/__init__.py
jasongrout Sep 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -60,7 +60,7 @@ repos:
- id: mypy
args: ["--config-file", "pyproject.toml"]
stages: [manual]
additional_dependencies: [pytest]
additional_dependencies: [pytest, platformdirs]
exclude: |
exclude: |
(?x)^(
Expand Down
9 changes: 9 additions & 0 deletions jupyter_core/command.py
Expand Up @@ -231,6 +231,15 @@ def main():
if args.debug:
env = os.environ

if paths.use_platform_dirs():
print(
"JUPYTER_PLATFORM_DIRS is set to a true value, so we use platformdirs to find platform-specific directories"
)
else:
print(
"JUPYTER_PLATFORM_DIRS is set to a false value, or is not set, so we use hardcoded legacy paths for platform-specific directories"
)

if paths.prefer_environment_over_user():
print(
"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"
Expand Down
83 changes: 61 additions & 22 deletions jupyter_core/paths.py
Expand Up @@ -19,8 +19,15 @@
from pathlib import Path
from typing import Optional

import platformdirs

from jupyter_core.utils import deprecation

pjoin = os.path.join

# Capitalize Jupyter in paths only on Windows and MacOS
APPNAME = "Jupyter" if sys.platform in ("win32", "darwin") else "jupyter"

# UF_HIDDEN is a stat flag not defined in the stat module.
# It is used by BSD to indicate hidden files.
UF_HIDDEN = getattr(stat, "UF_HIDDEN", 32768)
Expand All @@ -40,6 +47,15 @@ def envset(name, default=False):
return os.environ[name].lower() not in ["no", "n", "false", "off", "0", "0.0"]


def use_platform_dirs():
"""Determine if platformdirs should be used for system-specific paths.

We plan for this to default to False in jupyter_core version 5 and to True
in jupyter_core version 6.
"""
return envset("JUPYTER_PLATFORM_DIRS", False)


def get_home_dir():
"""Get the real path of the home directory"""
homedir = os.path.expanduser("~")
Expand Down Expand Up @@ -85,7 +101,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 JUPYTER_CONFIG_DIR if defined, otherwise the appropriate
directory for the platform.
"""

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

if use_platform_dirs():
return platformdirs.user_config_dir(APPNAME, appauthor=False)

home_dir = get_home_dir()
return pjoin(home_dir, ".jupyter")

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

if use_platform_dirs():
return platformdirs.user_data_dir(APPNAME, appauthor=False)

home = get_home_dir()

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


if os.name == "nt":
programdata = os.environ.get("PROGRAMDATA", None)
if programdata:
SYSTEM_JUPYTER_PATH = [pjoin(programdata, "jupyter")]
else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_JUPYTER_PATH = [os.path.join(sys.prefix, "share", "jupyter")]
if use_platform_dirs():
SYSTEM_JUPYTER_PATH = platformdirs.site_data_dir(
APPNAME, appauthor=False, multipath=True
).split(os.pathsep)
else:
SYSTEM_JUPYTER_PATH = [
"/usr/local/share/jupyter",
"/usr/share/jupyter",
]
deprecation(
"Jupyter is migrating its paths to use standard platformdirs\n" # noqa
+ "given by the platformdirs library. To remove this warning and\n"
+ "see the appropriate new directories, set the environment variable\n"
+ "`JUPYTER_PLATFORM_DIRS=1` and then run `jupyter --paths`.\n"
+ "The use of platformdirs will be the default in `jupyter_core` v6"
)
if os.name == "nt":
programdata = os.environ.get("PROGRAMDATA", None)
if programdata:
SYSTEM_JUPYTER_PATH = [pjoin(programdata, "jupyter")]
else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_JUPYTER_PATH = [os.path.join(sys.prefix, "share", "jupyter")]
else:
SYSTEM_JUPYTER_PATH = [
"/usr/local/share/jupyter",
"/usr/share/jupyter",
]

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

Expand Down Expand Up @@ -222,18 +257,22 @@ def jupyter_path(*subdirs):
return paths


if os.name == "nt":
programdata = os.environ.get("PROGRAMDATA", None)
if programdata:
SYSTEM_CONFIG_PATH = [os.path.join(programdata, "jupyter")]
else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_CONFIG_PATH = []
if use_platform_dirs():
SYSTEM_CONFIG_PATH = platformdirs.site_config_dir(
APPNAME, appauthor=False, multipath=True
).split(os.pathsep)
else:
SYSTEM_CONFIG_PATH = [
"/usr/local/etc/jupyter",
"/etc/jupyter",
]

if os.name == "nt":
programdata = os.environ.get("PROGRAMDATA", None)
if programdata:
SYSTEM_CONFIG_PATH = [os.path.join(programdata, "jupyter")]
else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_CONFIG_PATH = []
else:
SYSTEM_CONFIG_PATH = [
"/usr/local/etc/jupyter",
"/etc/jupyter",
]
ENV_CONFIG_PATH = [os.path.join(sys.prefix, "etc", "jupyter")]


Expand Down