Skip to content

Add json5 support for page_config.json #388

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

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions jupyterlab_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from itertools import chain
from os.path import join as pjoin

import json5 # type:ignore
from jupyter_core.paths import SYSTEM_CONFIG_PATH, jupyter_config_dir, jupyter_path
from jupyter_server.services.config.manager import ConfigManager, recursive_update
from jupyter_server.utils import url_path_join as ujoin
Expand Down Expand Up @@ -78,6 +79,26 @@ def get_static_page_config(app_settings_dir=None, logger=None, level="all"):
return cm.get("page_config")


def load_config(path):
"""Load either a json5 or a json config file.

Parameters
----------
path : str
Path to the file to be loaded

Returns
-------
Dict[Any, Any]
Dictionary of json or json5 data
"""
with open(path, encoding="utf-8") as fid:
if path.endswith('.json5'):
return json5.load(fid)
else:
return json.load(fid)


def get_page_config(labextensions_path, app_settings_dir=None, logger=None): # noqa
"""Get the page config for the application handler"""
# Build up the full page config
Expand All @@ -87,17 +108,20 @@ def get_page_config(labextensions_path, app_settings_dir=None, logger=None): #

# Start with the app_settings_dir as lowest priority
if app_settings_dir:
app_page_config = pjoin(app_settings_dir, "page_config.json")
if osp.exists(app_page_config):
with open(app_page_config, encoding="utf-8") as fid:
data = json.load(fid)

# Convert lists to dicts
for key in [disabled_key, "deferredExtensions"]:
if key in data:
data[key] = {key: True for key in data[key]}

recursive_update(page_config, data)
config_paths = [
pjoin(app_settings_dir, "page_config.json5"),
pjoin(app_settings_dir, "page_config.json"),
]
for path in config_paths:
if osp.exists(path):
data = load_config(path)
# Convert lists to dicts
for key in [disabled_key, "deferredExtensions"]:
if key in data:
data[key] = {key: True for key in data[key]}

recursive_update(page_config, data)
break

# Get the traitlets config
static_page_config = get_static_page_config(logger=logger, level="all")
Expand Down
16 changes: 13 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
import json
import os

import json5 # type:ignore
import pytest

from jupyterlab_server.config import get_page_config


def test_get_page_config(tmp_path):
@pytest.mark.parametrize(
"lib,extension",
(
(json, "json"),
(json5, "json5"),
),
)
def test_get_page_config(tmp_path, lib, extension):
labext_path = [os.path.join(tmp_path, "ext")]
settings_path = os.path.join(tmp_path, "settings")
os.mkdir(settings_path)

with open(os.path.join(settings_path, "page_config.json"), "w") as fid:
with open(os.path.join(settings_path, f"page_config.{extension}"), "w") as fid:
data = dict(deferredExtensions=["foo"])
json.dump(data, fid)
lib.dump(data, fid)

static_dir = os.path.join(tmp_path, "static")
os.mkdir(static_dir)
Expand Down