9
9
from itertools import chain
10
10
from os .path import join as pjoin
11
11
12
+ import json5 # type:ignore
12
13
from jupyter_core .paths import SYSTEM_CONFIG_PATH , jupyter_config_dir , jupyter_path
13
14
from jupyter_server .services .config .manager import ConfigManager , recursive_update
14
15
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"):
78
79
return cm .get ("page_config" )
79
80
80
81
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
+
81
102
def get_page_config (labextensions_path , app_settings_dir = None , logger = None ): # noqa
82
103
"""Get the page config for the application handler"""
83
104
# Build up the full page config
@@ -87,17 +108,20 @@ def get_page_config(labextensions_path, app_settings_dir=None, logger=None): #
87
108
88
109
# Start with the app_settings_dir as lowest priority
89
110
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
101
125
102
126
# Get the traitlets config
103
127
static_page_config = get_static_page_config (logger = logger , level = "all" )
0 commit comments