Skip to content

Commit 951b54c

Browse files
divyansshhhDivyansh Choudharypre-commit-ci[bot]
authored
Add util to get consolidated page_config from higher levels (#448)
* Add util to get consolidated page_config from higher levels * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add missing docstring --------- Co-authored-by: Divyansh Choudhary <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cca5a50 commit 951b54c

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

jupyterlab_server/config.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Any
1414

1515
import json5
16-
from jupyter_core.paths import SYSTEM_CONFIG_PATH, jupyter_config_dir, jupyter_path
16+
from jupyter_core.paths import ENV_CONFIG_PATH, SYSTEM_CONFIG_PATH, jupyter_config_dir, jupyter_path
1717
from jupyter_server.services.config.manager import ConfigManager, recursive_update
1818
from jupyter_server.utils import url_path_join as ujoin
1919
from traitlets import Bool, HasTraits, List, Unicode, default
@@ -77,6 +77,7 @@ def get_static_page_config(
7777
app_settings_dir: str | None = None, # noqa: ARG001
7878
logger: Logger | None = None, # noqa: ARG001
7979
level: str = "all",
80+
include_higher_levels: bool = False,
8081
) -> dict[str, Any]:
8182
"""Get the static page config for JupyterLab
8283
@@ -87,7 +88,7 @@ def get_static_page_config(
8788
level: string, optional ['all']
8889
The level at which to get config: can be 'all', 'user', 'sys_prefix', or 'system'
8990
"""
90-
cm = _get_config_manager(level)
91+
cm = _get_config_manager(level, include_higher_levels)
9192
return cm.get("page_config") # type:ignore[no-untyped-call]
9293

9394

@@ -358,11 +359,18 @@ def _default_translations_api_url(self) -> str:
358359
return ujoin(self.app_url, "api", "translations/")
359360

360361

361-
def _get_config_manager(level: str) -> ConfigManager:
362+
def get_allowed_levels() -> list[str]:
363+
"""
364+
Returns the levels where configs can be stored.
365+
"""
366+
return ["all", "user", "sys_prefix", "system", "app", "extension"]
367+
368+
369+
def _get_config_manager(level: str, include_higher_levels: bool = False) -> ConfigManager:
362370
"""Get the location of config files for the current context
363371
Returns the string to the environment
364372
"""
365-
allowed = ["all", "user", "sys_prefix", "system", "app", "extension"]
373+
allowed = get_allowed_levels()
366374
if level not in allowed:
367375
msg = f"Page config level must be one of: {allowed}"
368376
raise ValueError(msg)
@@ -372,16 +380,22 @@ def _get_config_manager(level: str) -> ConfigManager:
372380
if level == "all":
373381
return ConfigManager(config_dir_name=config_name)
374382

375-
if level == "user":
376-
config_dir = jupyter_config_dir()
377-
elif level == "sys_prefix":
378-
# Delayed import since this gets monkey-patched in tests
379-
from jupyter_core.paths import ENV_CONFIG_PATH
383+
paths: dict[str, list] = {
384+
"app": [],
385+
"system": SYSTEM_CONFIG_PATH,
386+
"sys_prefix": [ENV_CONFIG_PATH[0]],
387+
"user": [jupyter_config_dir()],
388+
"extension": [],
389+
}
380390

381-
config_dir = ENV_CONFIG_PATH[0]
382-
else:
383-
config_dir = SYSTEM_CONFIG_PATH[0]
391+
levels = allowed[allowed.index(level) :] if include_higher_levels else [level]
392+
393+
read_config_paths, write_config_dir = [], None
384394

385-
full_config_path = osp.join(config_dir, config_name)
395+
for _level in levels:
396+
for p in paths[_level]:
397+
read_config_paths.append(osp.join(p, config_name))
398+
if write_config_dir is None and paths[_level]: # type: ignore[redundant-expr]
399+
write_config_dir = osp.join(paths[_level][0], config_name)
386400

387-
return ConfigManager(read_config_path=[full_config_path], write_config_dir=full_config_path)
401+
return ConfigManager(read_config_path=read_config_paths, write_config_dir=write_config_dir)

0 commit comments

Comments
 (0)