Skip to content

Commit cd829a8

Browse files
Optional argument to get only the schema names (#385)
* Adds an optional argument to get only the schemas names * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Adds the federated extensions too, and avoid duplicating the file search by using the same function as previously * Adds a test on fetching the schema IDs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Change the argument 'names_only' to 'ids_only' for consistency * Updating doc string --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 55a5056 commit cd829a8

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

jupyterlab_server/settings_handler.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,25 @@ def initialize(
2626

2727
@web.authenticated
2828
def get(self, schema_name=""):
29-
"""Get setting(s)"""
29+
"""
30+
Get setting(s)
31+
32+
Parameters
33+
----------
34+
schema_name: str
35+
The id of a unique schema to send, added to the URL
36+
37+
## NOTES:
38+
An optional argument `ids_only=true` can be provided in the URL to get only the
39+
ids of the schemas instead of the content.
40+
"""
3041
# Need to be update here as translator locale is not change when a new locale is put
3142
# from frontend
3243
locale = self.get_current_locale()
3344
translator.set_locale(locale)
3445

46+
ids_only = self.get_argument('ids_only', '') == 'true'
47+
3548
result, warnings = get_settings(
3649
self.app_settings_dir,
3750
self.schemas_dir,
@@ -40,6 +53,7 @@ def get(self, schema_name=""):
4053
schema_name=schema_name,
4154
overrides=self.overrides,
4255
translator=translator.translate_schema,
56+
ids_only=ids_only,
4357
)
4458

4559
# Print all warnings.

jupyterlab_server/settings_utils.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ def _list_settings(
129129
extension=".json",
130130
labextensions_path=None,
131131
translator=None,
132+
ids_only=False,
132133
):
133134
"""
134135
Returns a tuple containing:
135136
- the list of plugins, schemas, and their settings,
136-
respecting any defaults that may have been overridden.
137+
respecting any defaults that may have been overridden if `ids_only=False`,
138+
otherwise a list of dict containing only the ids of plugins.
137139
- the list of warnings that were generated when
138140
validating the user overrides against the schemas.
139141
"""
@@ -159,16 +161,20 @@ def _list_settings(
159161
).replace(
160162
"\\", "/"
161163
) # Normalize slashes.
162-
schema, version = _get_schema(schemas_dir, schema_name, overrides, None)
163-
if translator is not None:
164-
schema = translator(schema)
165-
user_settings = _get_user_settings(settings_dir, schema_name, schema)
166164

167-
if user_settings["warning"]:
168-
warnings.append(user_settings.pop("warning"))
165+
if ids_only:
166+
settings[_id] = dict(id=_id)
167+
else:
168+
schema, version = _get_schema(schemas_dir, schema_name, overrides, None)
169+
if translator is not None:
170+
schema = translator(schema)
171+
user_settings = _get_user_settings(settings_dir, schema_name, schema)
172+
173+
if user_settings["warning"]:
174+
warnings.append(user_settings.pop("warning"))
169175

170-
# Add the plugin to the list of settings.
171-
settings[_id] = dict(id=_id, schema=schema, version=version, **user_settings)
176+
# Add the plugin to the list of settings.
177+
settings[_id] = dict(id=_id, schema=schema, version=version, **user_settings)
172178

173179
if labextensions_path is not None:
174180
schema_paths = []
@@ -195,16 +201,21 @@ def _list_settings(
195201
if _id in federated_settings:
196202
continue
197203

198-
schema, version = _get_schema(
199-
schemas_dir, schema_name, overrides, labextensions_path=labextensions_path
200-
)
201-
user_settings = _get_user_settings(settings_dir, schema_name, schema)
204+
if ids_only:
205+
federated_settings[_id] = dict(id=_id)
206+
else:
207+
schema, version = _get_schema(
208+
schemas_dir, schema_name, overrides, labextensions_path=labextensions_path
209+
)
210+
user_settings = _get_user_settings(settings_dir, schema_name, schema)
202211

203-
if user_settings["warning"]:
204-
warnings.append(user_settings.pop("warning"))
212+
if user_settings["warning"]:
213+
warnings.append(user_settings.pop("warning"))
205214

206-
# Add the plugin to the list of settings.
207-
federated_settings[_id] = dict(id=_id, schema=schema, version=version, **user_settings)
215+
# Add the plugin to the list of settings.
216+
federated_settings[_id] = dict(
217+
id=_id, schema=schema, version=version, **user_settings
218+
)
208219

209220
settings.update(federated_settings)
210221
settings_list = [settings[key] for key in sorted(settings.keys(), reverse=True)]
@@ -317,6 +328,7 @@ def get_settings(
317328
overrides=None,
318329
labextensions_path=None,
319330
translator=None,
331+
ids_only=False,
320332
):
321333
"""
322334
Get settings.
@@ -343,9 +355,10 @@ def get_settings(
343355
-------
344356
tuple
345357
The first item is a dictionary with a list of setting if no `schema_name`
346-
was provided, otherwise it is a dictionary with id, raw, scheme, settings
347-
and version keys. The second item is a list of warnings. Warnings will
348-
either be a list of i) strings with the warning messages or ii) `None`.
358+
was provided (only the ids if `ids_only=True`), otherwise it is a dictionary
359+
with id, raw, scheme, settings and version keys.
360+
The second item is a list of warnings. Warnings will either be a list of
361+
i) strings with the warning messages or ii) `None`.
349362
"""
350363
result = {}
351364
warnings = []
@@ -367,6 +380,7 @@ def get_settings(
367380
overrides,
368381
labextensions_path=labextensions_path,
369382
translator=translator,
383+
ids_only=ids_only,
370384
)
371385
result = {
372386
"settings": settings_list,

tests/test_settings_api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ async def test_listing(jp_fetch, labserverapp):
104104
assert {None} == set(last_modifieds + createds)
105105

106106

107+
async def test_listing_ids(jp_fetch, labserverapp):
108+
ids = [
109+
"@jupyterlab/apputils-extension:themes",
110+
"@jupyterlab/apputils-extension-federated:themes",
111+
"@jupyterlab/codemirror-extension:commands",
112+
"@jupyterlab/codemirror-extension-federated:commands",
113+
"@jupyterlab/shortcuts-extension:plugin",
114+
"@jupyterlab/translation-extension:plugin",
115+
"@jupyterlab/unicode-extension:plugin",
116+
]
117+
r = await jp_fetch("lab", "api", "settings/", params={"ids_only": "true"})
118+
validate_request(r)
119+
res = r.body.decode()
120+
response = json.loads(res)
121+
response_ids = [item["id"] for item in response["settings"]]
122+
# Checks the IDs list is correct
123+
assert set(response_ids) == set(ids)
124+
125+
# Checks there is only the 'id' key in each item
126+
assert all(
127+
[(len(item.keys()) == 1 and list(item.keys())[0] == 'id') for item in response["settings"]]
128+
)
129+
130+
107131
async def test_patch(jp_fetch, labserverapp):
108132
id = "@jupyterlab/shortcuts-extension:plugin"
109133

0 commit comments

Comments
 (0)