Skip to content

Commit

Permalink
Merge overrides settings if values are dicts (#188)
Browse files Browse the repository at this point in the history
* Merge overrides settings if dicts

* test

* Pin sphinx version <4 and fix changelog username change

* Add tests
  • Loading branch information
goanpeca authored Jul 8, 2021
1 parent 6ecc630 commit 52f4cf9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
- conda-forge
dependencies:
- python=3.8
- sphinx>=1.8
- sphinx<4.0
- sphinx-copybutton
- pip
- myst-parser
Expand Down
15 changes: 11 additions & 4 deletions jupyterlab_server/settings_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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 <jupyter_config>/labconfig dirs
# to allow layering of defaults
cm = ConfigManager(config_dir_name="labconfig")
Expand Down
5 changes: 4 additions & 1 deletion jupyterlab_server/tests/app-settings/overrides.json
Original file line number Diff line number Diff line change
@@ -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: 中文(台灣)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions jupyterlab_server/tests/test_settings_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 52f4cf9

Please sign in to comment.