Skip to content

Commit 48be03a

Browse files
Add json5 support for page_config.json (#388)
Co-authored-by: Steven Silvester <[email protected]>
1 parent 1dcca61 commit 48be03a

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

jupyterlab_server/config.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from itertools import chain
1010
from os.path import join as pjoin
1111

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

8081

82+
def load_config(path):
83+
"""Load either a json5 or a json config file.
84+
85+
Parameters
86+
----------
87+
path : str
88+
Path to the file to be loaded
89+
90+
Returns
91+
-------
92+
Dict[Any, Any]
93+
Dictionary of json or json5 data
94+
"""
95+
with open(path, encoding="utf-8") as fid:
96+
if path.endswith('.json5'):
97+
return json5.load(fid)
98+
else:
99+
return json.load(fid)
100+
101+
81102
def get_page_config(labextensions_path, app_settings_dir=None, logger=None): # noqa
82103
"""Get the page config for the application handler"""
83104
# Build up the full page config
@@ -87,17 +108,20 @@ def get_page_config(labextensions_path, app_settings_dir=None, logger=None): #
87108

88109
# Start with the app_settings_dir as lowest priority
89110
if app_settings_dir:
90-
app_page_config = pjoin(app_settings_dir, "page_config.json")
91-
if osp.exists(app_page_config):
92-
with open(app_page_config, encoding="utf-8") as fid:
93-
data = json.load(fid)
94-
95-
# Convert lists to dicts
96-
for key in [disabled_key, "deferredExtensions"]:
97-
if key in data:
98-
data[key] = {key: True for key in data[key]}
99-
100-
recursive_update(page_config, data)
111+
config_paths = [
112+
pjoin(app_settings_dir, "page_config.json5"),
113+
pjoin(app_settings_dir, "page_config.json"),
114+
]
115+
for path in config_paths:
116+
if osp.exists(path):
117+
data = load_config(path)
118+
# Convert lists to dicts
119+
for key in [disabled_key, "deferredExtensions"]:
120+
if key in data:
121+
data[key] = {key: True for key in data[key]}
122+
123+
recursive_update(page_config, data)
124+
break
101125

102126
# Get the traitlets config
103127
static_page_config = get_static_page_config(logger=logger, level="all")

tests/test_config.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@
44
import json
55
import os
66

7+
import json5 # type:ignore
8+
import pytest
9+
710
from jupyterlab_server.config import get_page_config
811

912

10-
def test_get_page_config(tmp_path):
13+
@pytest.mark.parametrize(
14+
"lib,extension",
15+
(
16+
(json, "json"),
17+
(json5, "json5"),
18+
),
19+
)
20+
def test_get_page_config(tmp_path, lib, extension):
1121
labext_path = [os.path.join(tmp_path, "ext")]
1222
settings_path = os.path.join(tmp_path, "settings")
1323
os.mkdir(settings_path)
1424

15-
with open(os.path.join(settings_path, "page_config.json"), "w") as fid:
25+
with open(os.path.join(settings_path, f"page_config.{extension}"), "w") as fid:
1626
data = dict(deferredExtensions=["foo"])
17-
json.dump(data, fid)
27+
lib.dump(data, fid)
1828

1929
static_dir = os.path.join(tmp_path, "static")
2030
os.mkdir(static_dir)

0 commit comments

Comments
 (0)