Skip to content

Instantly share code, notes, and snippets.

@Zsailer
Last active July 17, 2020 03:39
Show Gist options
  • Save Zsailer/5ce485e8159843b012ef5ce439d66293 to your computer and use it in GitHub Desktop.
Save Zsailer/5ce485e8159843b012ef5ce439d66293 to your computer and use it in GitHub Desktop.
import pathlib
from jupyter_core.paths import jupyter_config_path
from traitlets.config.loader import (
JSONFileConfigLoader
)
def configd_path(
config_dir=None,
configd_prefix="jupyter_server",
):
# Build directory name for `config.d` directory.
configd = "_".join([configd_prefix, "config.d"])
if not config_dir:
config_dir = jupyter_config_path()
# Leverage pathlib for path management.
return [pathlib.Path(path).joinpath(configd) for path in config_dir]
def configd_files(
config_dir=None,
configd_prefix="jupyter_server",
):
"""Lists (only) JSON files found in a Jupyter config.d folder.
"""
paths = configd_path(
config_dir=config_dir,
configd_prefix=configd_prefix
)
files = []
for path in paths:
json_files = path.glob("*.json")
files.extend(json_files)
return files
def enabled(name, server_config):
"""Given a server config object, return True if the extension
is explicitly enabled in the config.
"""
enabled = (
server_config
.get("ServerApp", {})
.get("jpserver_extensions", {})
.get(name, False)
)
return enabled
def find_extension_in_configd(
name,
config_dir=None,
configd_prefix="jupyter_server",
):
"""Search through all config.d files and return the
JSON Path for this named extension. If the extension
is not found, return None
"""
files = configd_files(
config_dir=config_dir,
configd_prefix=configd_prefix,
)
for f in files:
if name == f.stem:
return f
def configd_enabled(
name,
config_dir=None,
configd_prefix="jupyter_server",
):
"""Check if the named extension is enabled somewhere in
a config.d folder.
"""
config_file = find_extension_in_configd(
name,
config_dir=config_dir,
configd_prefix=configd_prefix,
)
if config_file:
c = JSONFileConfigLoader(
filename=str(config_file.name),
path=str(config_file.parent)
)
config = c.load_config()
return enabled(name, config)
else:
return False
def list_extensions_in_configd(
configd_prefix="jupyter_server",
config_paths=None
):
"""Get a dictionary of all jpserver_extensions found in the
config directories list.
Parameters
----------
config_paths : list
List of config directories to search for the
`jupyter_server_config.d` directory.
"""
# Build directory name for `config.d` directory.
configd = "_".join([configd_prefix, "config.d"])
if not config_paths:
config_paths = jupyter_config_path()
# Leverage pathlib for path management.
config_paths = [pathlib.Path(p) for p in config_paths]
extensions = []
for path in config_paths:
json_files = path.joinpath(configd).glob("*.json")
for file in json_files:
# The extension name is the file name (minus file suffix)
extension_name = file.stem
extensions.append(extension_name)
return extensions
import pytest
from jupyter_server.extension.utils import (
list_extensions_in_configd,
configd_enabled,
validate_extension
)
# Use ServerApps environment because it monkeypatches
# jupyter_core.paths and provides a config directory
# that's not cross contaminating the user config directory.
pytestmark = pytest.mark.usefixtures("environ")
@pytest.fixture
def configd(env_config_path):
"""A pathlib.Path object that acts like a jupyter_server_config.d folder."""
configd = env_config_path.joinpath('jupyter_server_config.d')
configd.mkdir()
return configd
ext1_json_config = """\
{
"ServerApp": {
"jpserver_extensions": {
"ext1_config": true
}
}
}
"""
@pytest.fixture
def ext1_config(configd):
config = configd.joinpath("ext1_config.json")
config.write_text(ext1_json_config)
ext2_json_config = """\
{
"ServerApp": {
"jpserver_extensions": {
"ext2_config": false
}
}
}
"""
@pytest.fixture
def ext2_config(configd):
config = configd.joinpath("ext2_config.json")
config.write_text(ext2_json_config)
def test_list_extension_from_configd(ext1_config, ext2_config):
extensions = list_extensions_in_configd()
assert "ext2_config" in extensions
assert "ext1_config" in extensions
def test_config_enabled(ext1_config):
assert configd_enabled("ext1_config")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment