From 52f4cf92adb8b6dc3b350ce324619a231a9003ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Pe=C3=B1a-Castellanos?= Date: Thu, 8 Jul 2021 09:38:18 -0500 Subject: [PATCH] Merge overrides settings if values are dicts (#188) * Merge overrides settings if dicts * test * Pin sphinx version <4 and fix changelog username change * Add tests --- CHANGELOG.md | 2 +- docs/environment.yml | 2 +- jupyterlab_server/settings_handler.py | 15 +++++++++---- .../tests/app-settings/overrides.json | 5 ++++- .../apputils-extension/themes.json | 22 +++++++++++++++++++ jupyterlab_server/tests/test_settings_api.py | 15 +++++++++++++ 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71752f3c..0d8920f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,7 +90,7 @@ github_url: 'https://github.com/jupyterlab/jupyterlab_server/blob/master/CHANGEL ## 2.1.5 -* Fix/cp949 encoding error [#158](https://github.com/jupyterlab/jupyterlab_server/pull/158) ([@takavfx](https://github.com/takavfx)) +* Fix/cp949 encoding error [#158](https://github.com/jupyterlab/jupyterlab_server/pull/158) ([@k-takanori](https://github.com/k-takanori)) ## 2.1.4 diff --git a/docs/environment.yml b/docs/environment.yml index 6034b271..a9befbda 100644 --- a/docs/environment.yml +++ b/docs/environment.yml @@ -4,7 +4,7 @@ channels: - conda-forge dependencies: - python=3.8 -- sphinx>=1.8 +- sphinx<4.0 - sphinx-copybutton - pip - myst-parser diff --git a/jupyterlab_server/settings_handler.py b/jupyterlab_server/settings_handler.py index 70bd45d3..5706a792 100755 --- a/jupyterlab_server/settings_handler.py +++ b/jupyterlab_server/settings_handler.py @@ -21,8 +21,6 @@ def _get_schema(schemas_dir, schema_name, overrides, labextensions_path): """Returns a dict containing a parsed and validated JSON schema.""" - - notfound_error = 'Schema not found: %s' parse_error = 'Failed parsing schema (%s): %s' validation_error = 'Failed validating schema (%s): %s' @@ -215,12 +213,18 @@ def _list_settings(schemas_dir, settings_dir, overrides, extension='.json', labe def _override(schema_name, schema, overrides): """Override default values in the schema if necessary.""" - if schema_name in overrides: defaults = overrides[schema_name] for key in defaults: if key in schema['properties']: - schema['properties'][key]['default'] = defaults[key] + new_defaults = schema['properties'][key]['default'] + # If values for defaults are dicts do a recursive update + if isinstance(new_defaults, dict): + recursive_update(new_defaults.copy(), defaults[key]) + else: + new_defaults = defaults[key] + + schema['properties'][key]['default'] = new_defaults else: schema['properties'][key] = dict(default=defaults[key]) @@ -259,14 +263,17 @@ def _get_overrides(app_settings_dir): """Get overrides settings from `app_settings_dir`.""" overrides, error = {}, "" overrides_path = os.path.join(app_settings_dir, 'overrides.json') + if not os.path.exists(overrides_path): overrides_path = os.path.join(app_settings_dir, 'overrides.json5') + if os.path.exists(overrides_path): with open(overrides_path, encoding='utf-8') as fid: try: overrides = json5.load(fid) except Exception as e: error = e + # Allow `default_settings_overrides.json` files in /labconfig dirs # to allow layering of defaults cm = ConfigManager(config_dir_name="labconfig") diff --git a/jupyterlab_server/tests/app-settings/overrides.json b/jupyterlab_server/tests/app-settings/overrides.json index d77e638f..65da15a4 100644 --- a/jupyterlab_server/tests/app-settings/overrides.json +++ b/jupyterlab_server/tests/app-settings/overrides.json @@ -1,6 +1,9 @@ { "@jupyterlab/apputils-extension:themes": { - "theme": "JupyterLab Dark" + "theme": "JupyterLab Dark", + "codeCellConfig": { + "lineNumbers": false + } }, "@jupyterlab/unicode-extension:plugin": { "comment": "Here are some languages with unicode in their names: id: Bahasa Indonesia, ms: Bahasa Melayu, bs: Bosanski, ca: Català, cs: Čeština, da: Dansk, de: Deutsch, et: Eesti, en: English, es: Español, fil: Filipino, fr: Français, it: Italiano, hu: Magyar, nl: Nederlands, no: Norsk, pl: Polski, pt-br: Português (Brasil), pt: Português (Portugal), ro: Română, fi: Suomi, sv: Svenska, vi: Tiếng Việt, tr: Türkçe, el: Ελληνικά, ru: Русский, sr: Српски, uk: Українська, he: עברית, ar: العربية, th: ไทย, ko: 한국어, ja: 日本語, zh: 中文(中国), zh-tw: 中文(台灣)" diff --git a/jupyterlab_server/tests/schemas/@jupyterlab/apputils-extension/themes.json b/jupyterlab_server/tests/schemas/@jupyterlab/apputils-extension/themes.json index 5dd56756..ec8cd729 100644 --- a/jupyterlab_server/tests/schemas/@jupyterlab/apputils-extension/themes.json +++ b/jupyterlab_server/tests/schemas/@jupyterlab/apputils-extension/themes.json @@ -4,6 +4,28 @@ "properties": { "theme": { "type": "string", "title": "Selected Theme", "default": "JupyterLab Light" + }, + "codeCellConfig": { + "title": "Code Cell Configuration", + "description": "The configuration for all code cells.", + "$ref": "#/definitions/editorConfig", + "default": { + "autoClosingBrackets": true, + "cursorBlinkRate": 530, + "fontFamily": null, + "fontSize": null, + "lineHeight": null, + "lineNumbers": false, + "lineWrap": "off", + "matchBrackets": true, + "readOnly": false, + "insertSpaces": true, + "tabSize": 4, + "wordWrapColumn": 80, + "rulers": [], + "codeFolding": false, + "lineWiseCopyCut": true + } } }, "type": "object" diff --git a/jupyterlab_server/tests/test_settings_api.py b/jupyterlab_server/tests/test_settings_api.py index 28f334e2..c19e546e 100755 --- a/jupyterlab_server/tests/test_settings_api.py +++ b/jupyterlab_server/tests/test_settings_api.py @@ -13,6 +13,21 @@ from .utils import validate_request +async def test_get_settings_overrides_dicts(jp_fetch, labserverapp): + # Check that values that are dictionaries in overrides.json are + # merged with the schema. + id = '@jupyterlab/apputils-extension:themes' + r = await jp_fetch('lab', 'api', 'settings', id) + validate_request(r) + res = r.body.decode() + data = json.loads(res) + assert data['id'] == id + schema = data['schema'] + # Check that overrides.json file is respected. + assert schema['properties']['codeCellConfig']['default']["lineNumbers"] is False + assert len(schema['properties']['codeCellConfig']['default']) == 15 + + async def test_get_settings(jp_fetch, labserverapp): id = '@jupyterlab/apputils-extension:themes' r = await jp_fetch('lab', 'api', 'settings', id)