From 6758119032842031e3d5ec8b96fa8416dc3c0e41 Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Sat, 20 Apr 2024 01:23:38 +0530 Subject: [PATCH 1/7] Add util to get consolidated page_config from higher levels --- jupyterlab_server/config.py | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index e1d2cd2..50d046c 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -13,7 +13,7 @@ from typing import Any import json5 -from jupyter_core.paths import SYSTEM_CONFIG_PATH, jupyter_config_dir, jupyter_path +from jupyter_core.paths import ENV_CONFIG_PATH, 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 from traitlets import Bool, HasTraits, List, Unicode, default @@ -77,6 +77,7 @@ def get_static_page_config( app_settings_dir: str | None = None, # noqa: ARG001 logger: Logger | None = None, # noqa: ARG001 level: str = "all", + include_higher_levels: bool = False ) -> dict[str, Any]: """Get the static page config for JupyterLab @@ -87,7 +88,7 @@ def get_static_page_config( level: string, optional ['all'] The level at which to get config: can be 'all', 'user', 'sys_prefix', or 'system' """ - cm = _get_config_manager(level) + cm = _get_config_manager(level, include_higher_levels) return cm.get("page_config") # type:ignore[no-untyped-call] @@ -358,30 +359,40 @@ def _default_translations_api_url(self) -> str: return ujoin(self.app_url, "api", "translations/") -def _get_config_manager(level: str) -> ConfigManager: +def get_allowed_levels() -> list[str]: + return ["all", "user", "sys_prefix", "system", "app", "extension"] + +def _get_config_manager(level: str, include_higher_levels: bool=False) -> ConfigManager: """Get the location of config files for the current context Returns the string to the environment """ - allowed = ["all", "user", "sys_prefix", "system", "app", "extension"] + allowed = get_allowed_levels() if level not in allowed: msg = f"Page config level must be one of: {allowed}" raise ValueError(msg) config_name = "labconfig" - + if level == "all": return ConfigManager(config_dir_name=config_name) - if level == "user": - config_dir = jupyter_config_dir() - elif level == "sys_prefix": - # Delayed import since this gets monkey-patched in tests - from jupyter_core.paths import ENV_CONFIG_PATH + paths = {"app": [], + "system": SYSTEM_CONFIG_PATH, + "sys_prefix": [ENV_CONFIG_PATH[0]], + "user": [jupyter_config_dir()], + "extension": []} - config_dir = ENV_CONFIG_PATH[0] + if include_higher_levels: + levels = allowed[allowed.index(level):] else: - config_dir = SYSTEM_CONFIG_PATH[0] + levels = [level] + + read_config_paths, write_config_dir = [], None - full_config_path = osp.join(config_dir, config_name) + for _level in levels: + for p in paths[_level]: + read_config_paths.append(osp.join(p, config_name)) + if write_config_dir is None and paths[_level]: + write_config_dir = osp.join(paths[_level][0], config_name) - return ConfigManager(read_config_path=[full_config_path], write_config_dir=full_config_path) + return ConfigManager(read_config_path=read_config_paths, write_config_dir=write_config_dir) From ff5d158989fb85ce10509358bd8ea5dc0c4f3ea6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Apr 2024 19:56:49 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyterlab_server/config.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index 50d046c..a81c376 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -77,7 +77,7 @@ def get_static_page_config( app_settings_dir: str | None = None, # noqa: ARG001 logger: Logger | None = None, # noqa: ARG001 level: str = "all", - include_higher_levels: bool = False + include_higher_levels: bool = False, ) -> dict[str, Any]: """Get the static page config for JupyterLab @@ -362,7 +362,8 @@ def _default_translations_api_url(self) -> str: def get_allowed_levels() -> list[str]: return ["all", "user", "sys_prefix", "system", "app", "extension"] -def _get_config_manager(level: str, include_higher_levels: bool=False) -> ConfigManager: + +def _get_config_manager(level: str, include_higher_levels: bool = False) -> ConfigManager: """Get the location of config files for the current context Returns the string to the environment """ @@ -372,18 +373,20 @@ def _get_config_manager(level: str, include_higher_levels: bool=False) -> Config raise ValueError(msg) config_name = "labconfig" - + if level == "all": return ConfigManager(config_dir_name=config_name) - paths = {"app": [], - "system": SYSTEM_CONFIG_PATH, - "sys_prefix": [ENV_CONFIG_PATH[0]], - "user": [jupyter_config_dir()], - "extension": []} + paths = { + "app": [], + "system": SYSTEM_CONFIG_PATH, + "sys_prefix": [ENV_CONFIG_PATH[0]], + "user": [jupyter_config_dir()], + "extension": [], + } if include_higher_levels: - levels = allowed[allowed.index(level):] + levels = allowed[allowed.index(level) :] else: levels = [level] From fd4bdf34eb7f1bfb3be2e6446899af65cd314ecb Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Sat, 20 Apr 2024 19:33:50 +0530 Subject: [PATCH 3/7] Fix lint --- jupyterlab_server/config.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index a81c376..92c3028 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -377,13 +377,11 @@ def _get_config_manager(level: str, include_higher_levels: bool = False) -> Conf if level == "all": return ConfigManager(config_dir_name=config_name) - paths = { - "app": [], - "system": SYSTEM_CONFIG_PATH, - "sys_prefix": [ENV_CONFIG_PATH[0]], - "user": [jupyter_config_dir()], - "extension": [], - } + paths: dict[str, list] = {"app": [], + "system": SYSTEM_CONFIG_PATH, + "sys_prefix": [ENV_CONFIG_PATH[0]], + "user": [jupyter_config_dir()], + "extension": []} if include_higher_levels: levels = allowed[allowed.index(level) :] @@ -395,7 +393,7 @@ def _get_config_manager(level: str, include_higher_levels: bool = False) -> Conf for _level in levels: for p in paths[_level]: read_config_paths.append(osp.join(p, config_name)) - if write_config_dir is None and paths[_level]: + if write_config_dir is None and paths[_level]: # type: ignore[redundant-expr] write_config_dir = osp.join(paths[_level][0], config_name) return ConfigManager(read_config_path=read_config_paths, write_config_dir=write_config_dir) From 9d2b7153f7005f99c083e9d38aa1a98d3652aea0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Apr 2024 14:08:50 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyterlab_server/config.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index 92c3028..b88e49a 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -377,11 +377,13 @@ def _get_config_manager(level: str, include_higher_levels: bool = False) -> Conf if level == "all": return ConfigManager(config_dir_name=config_name) - paths: dict[str, list] = {"app": [], - "system": SYSTEM_CONFIG_PATH, - "sys_prefix": [ENV_CONFIG_PATH[0]], - "user": [jupyter_config_dir()], - "extension": []} + paths: dict[str, list] = { + "app": [], + "system": SYSTEM_CONFIG_PATH, + "sys_prefix": [ENV_CONFIG_PATH[0]], + "user": [jupyter_config_dir()], + "extension": [], + } if include_higher_levels: levels = allowed[allowed.index(level) :] From 533554df51e306d6e1bd32cf12c148e58b901d85 Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Mon, 22 Apr 2024 14:11:41 +0530 Subject: [PATCH 5/7] Fix lint --- jupyterlab_server/config.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index b88e49a..caec06c 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -385,10 +385,7 @@ def _get_config_manager(level: str, include_higher_levels: bool = False) -> Conf "extension": [], } - if include_higher_levels: - levels = allowed[allowed.index(level) :] - else: - levels = [level] + levels = allowed[allowed.index(level):] if include_higher_levels else [level] read_config_paths, write_config_dir = [], None From b92c5dd6c7caee80fe06e8a0baee36f6d658cdd9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 08:42:13 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyterlab_server/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index caec06c..69ad5d7 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -385,7 +385,7 @@ def _get_config_manager(level: str, include_higher_levels: bool = False) -> Conf "extension": [], } - levels = allowed[allowed.index(level):] if include_higher_levels else [level] + levels = allowed[allowed.index(level) :] if include_higher_levels else [level] read_config_paths, write_config_dir = [], None From d925f6cf34d099c8f00ef1debdf273514d2e5ccd Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Mon, 22 Apr 2024 14:32:58 +0530 Subject: [PATCH 7/7] Add missing docstring --- jupyterlab_server/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jupyterlab_server/config.py b/jupyterlab_server/config.py index 69ad5d7..faf57cd 100644 --- a/jupyterlab_server/config.py +++ b/jupyterlab_server/config.py @@ -360,6 +360,9 @@ def _default_translations_api_url(self) -> str: def get_allowed_levels() -> list[str]: + """ + Returns the levels where configs can be stored. + """ return ["all", "user", "sys_prefix", "system", "app", "extension"]