Skip to content

Commit

Permalink
Expose settings API to other handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
goanpeca committed Jul 4, 2020
1 parent 0d8fb81 commit d99ea9f
Showing 1 changed file with 83 additions and 39 deletions.
122 changes: 83 additions & 39 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 = {}
warning = ''
validation_warning = 'Failed validating settings (%s): %s'
parse_error = 'Failed loading settings (%s): %s'
Expand Down Expand Up @@ -185,51 +184,96 @@ 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

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)))
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.
"""
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

0 comments on commit d99ea9f

Please sign in to comment.