Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose settings API to other handlers. #94

Merged
merged 1 commit into from
Jul 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 85 additions & 38 deletions jupyterlab_server/settings_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ def _get_settings(settings_dir, schema_name, schema):
Returns a tuple containing the raw user settings, the parsed user
settings, and a validation warning for a schema.
"""

path = _path(settings_dir, schema_name, False, SETTINGS_EXTENSION)
raw = '{}'
settings = dict()
settings = {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is faster to use than dict, so .. why not use it 🙃

warning = ''
validation_warning = 'Failed validating settings (%s): %s'
parse_error = 'Failed loading settings (%s): %s'
Expand Down Expand Up @@ -185,51 +184,99 @@ def _path(root_dir, schema_name, make_dirs=False, extension='.json'):
return path


class SettingsHandler(APIHandler):

def initialize(self, app_settings_dir, schemas_dir, settings_dir):
self.overrides = dict()
self.schemas_dir = schemas_dir
self.settings_dir = settings_dir
def _get_overrides(app_settings_dir):
"""Get overrides settings from `app_settings_dir`."""
overrides_path = os.path.join(app_settings_dir, 'overrides.json')
if os.path.exists(overrides_path):
with open(overrides_path) as fid:
try:
overrides = json.load(fid)
error = ""
except Exception as e:
overrides = {}
error = e

overrides_path = os.path.join(app_settings_dir, 'overrides.json')
overrides_warning = 'Failed loading overrides: %s'
return overrides, error

if os.path.exists(overrides_path):
with open(overrides_path) as fid:
try:
self.overrides = json.load(fid)
except Exception as e:
self.log.warn(overrides_warning % str(e))

@web.authenticated
def get(self, schema_name=''):
overrides = self.overrides
schemas_dir = self.schemas_dir
settings_dir = self.settings_dir
def get_settings(app_settings_dir, schemas_dir, settings_dir, schema_name="", overrides=None):
"""
Get setttings.

Parameters
----------
app_settings_dir:
Path to applications settings.
schemas_dir: str
Path to schemas.
settings_dir:
Path to settings.
schema_name str, optional
Schema name. Default is "".
overrides: dict, optional
Settings overrides. If not provided, the overrides will be loaded
from the `app_settings_dir`. Default is None.

Returns
-------
tuple
The first item is a dictionary with a list of setting if no `schema_name`
was provided, otherwise it is a dictionary with id, raw, scheme, settings
and version keys. The second item is a list of warnings.
"""
result = {}
warnings = []

if not schema_name:
settings_list, warnings = _list_settings(
schemas_dir, settings_dir, overrides)
if warnings:
self.log.warn('\n'.join(warnings))
return self.finish(json.dumps(dict(settings=settings_list)))
if overrides is None:
overrides, _error = _get_overrides(app_settings_dir)

if schema_name:
schema = _get_schema(schemas_dir, schema_name, overrides)
raw, settings, warning = _get_settings(
settings_dir, schema_name, schema)
raw, settings, _warning = _get_settings(settings_dir, schema_name, schema)
version = _get_version(schemas_dir, schema_name)
result = {
"id": schema_name,
"raw": raw,
"schema": schema,
"settings": settings,
"version": version,
}
warnings = [_warning]
else:
settings_list, warnings = _list_settings(schemas_dir, settings_dir, overrides)
result = {
"settings": settings_list,
}

return result, warnings

if warning:
self.log.warn(warning)

self.finish(json.dumps(dict(
id=schema_name,
raw=raw,
schema=schema,
settings=settings,
version=version
)))
class SettingsHandler(APIHandler):

def initialize(self, app_settings_dir, schemas_dir, settings_dir):
self.overrides, error = _get_overrides(app_settings_dir)
self.app_settings_dir = app_settings_dir
self.schemas_dir = schemas_dir
self.settings_dir = settings_dir

if error:
overrides_warning = 'Failed loading overrides: %s'
self.log.warn(overrides_warning % str(error))

@web.authenticated
def get(self, schema_name=""):
result, warnings = get_settings(
self.app_settings_dir,
self.schemas_dir,
self.settings_dir,
schema_name=schema_name,
overrides=self.overrides,
)

if warnings:
self.log.warn('\n'.join(warnings))

return self.finish(json.dumps(result))

@web.authenticated
def put(self, schema_name):
Expand Down