浏览代码

Use theme and schema handlers in launcher

Steven Silvester 7 年之前
父节点
当前提交
1d2da20a40
共有 2 个文件被更改,包括 7 次插入117 次删除
  1. 7 18
      jupyterlab/extension.py
  2. 0 99
      jupyterlab/settings_handler.py

+ 7 - 18
jupyterlab/extension.py

@@ -14,7 +14,7 @@ from notebook.base.handlers import FileFindHandler
 from .commands import (
     get_app_dir, list_extensions, should_build, get_user_settings_dir
 )
-from .settings_handler import settings_path, SettingsHandler
+
 from .build_handler import build_path, Builder, BuildHandler
 from ._version import __version__
 
@@ -73,12 +73,8 @@ def load_jupyter_server_extension(nbapp):
     installed = list_extensions(app_dir)
     fallback = not installed and not os.path.exists(config.assets_dir)
 
-    base_url = web_app.settings['base_url']
-
-    theme_url = ujoin(base_url, 'lab/api/themes')
     web_app.settings.setdefault('page_config_data', dict())
     web_app.settings['page_config_data']['token'] = nbapp.token
-    web_app.settings['page_config_data']['themePath'] = theme_url
 
     if core_mode or fallback:
         config.assets_dir = os.path.join(here, 'build')
@@ -95,27 +91,20 @@ def load_jupyter_server_extension(nbapp):
     elif core_mode or fallback:
         nbapp.log.info(CORE_NOTE.strip())
 
-    add_handlers(web_app, config)
-
-    user_settings_dir = get_user_settings_dir()
-
     if core_mode or fallback:
         schemas_dir = os.path.join(here, 'schemas')
     else:
         schemas_dir = os.path.join(app_dir, 'schemas')
 
-    settings_url = ujoin(base_url, settings_path)
-    settings_handler = (settings_url, SettingsHandler, {
-        'schemas_dir': schemas_dir,
-        'settings_dir': user_settings_dir
-    })
+    config.schemas_dir = schemas_dir
+    config.user_settings_dir = get_user_settings_dir()
+    config.themes_dir = os.path.join(here, 'themes')
 
-    theme_handler = (ujoin(theme_url, "(.*)"), FileFindHandler, {
-        'path': os.path.join(here, 'themes')
-    })
+    add_handlers(web_app, config)
 
+    base_url = web_app.settings['base_url']
     build_url = ujoin(base_url, build_path)
     builder = Builder(nbapp.log, core_mode, app_dir)
     build_handler = (build_url, BuildHandler, {'builder': builder})
 
-    web_app.add_handlers(".*$", [settings_handler, build_handler, theme_handler])
+    web_app.add_handlers(".*$", [build_handler])

+ 0 - 99
jupyterlab/settings_handler.py

@@ -1,99 +0,0 @@
-"""Tornado handlers for frontend config storage."""
-
-# Copyright (c) Jupyter Development Team.
-# Distributed under the terms of the Modified BSD License.
-import json
-import os
-from tornado import web
-
-from notebook.base.handlers import APIHandler, json_errors
-
-try:
-    from jsonschema import ValidationError
-    from jsonschema import Draft4Validator as Validator
-except ImportError:
-    Validator = None
-
-
-class SettingsHandler(APIHandler):
-
-    def initialize(self, schemas_dir, settings_dir):
-        self.schemas_dir = schemas_dir
-        self.settings_dir = settings_dir
-
-    @json_errors
-    @web.authenticated
-    def get(self, section_name):
-        self.set_header('Content-Type', "application/json")
-        path = os.path.join(self.schemas_dir, section_name + ".json")
-
-        if not os.path.exists(path):
-            raise web.HTTPError(404, "Schema not found: %r" % section_name)
-        with open(path) as fid:
-            # Attempt to load the schema file.
-            try:
-                schema = json.load(fid)
-            except Exception as e:
-                name = section_name
-                message = "Failed parsing schema ({}): {}".format(name, str(e))
-                raise web.HTTPError(500, message)
-
-        path = os.path.join(self.settings_dir, section_name + '.json')
-        settings = dict()
-        if os.path.exists(path):
-            with open(path) as fid:
-                # Attempt to load the settings file.
-                try:
-                    settings = json.load(fid)
-                except Exception as e:
-                    self.log.warn(str(e))
-
-        # Validate the data against the schema.
-        if Validator is not None and len(settings):
-            validator = Validator(schema)
-            try:
-                validator.validate(settings)
-            except ValidationError as e:
-                self.log.warn(str(e))
-                settings = dict()
-
-        resp = dict(id=section_name, data=dict(user=settings), schema=schema)
-        self.finish(json.dumps(resp))
-
-    @json_errors
-    @web.authenticated
-    def patch(self, section_name):
-        if not self.settings_dir:
-            raise web.HTTPError(404, "No current settings directory")
-
-        path = os.path.join(self.schemas_dir, section_name + '.json')
-
-        if not os.path.exists(path):
-            raise web.HTTPError(404, "Schema not found for: %r" % section_name)
-
-        data = self.get_json_body()  # Will raise 400 if content is not valid JSON
-
-        # Validate the data against the schema.
-        if Validator is not None:
-            with open(path) as fid:
-                schema = json.load(fid)
-            validator = Validator(schema)
-            try:
-                validator.validate(data)
-            except ValidationError as e:
-                raise web.HTTPError(400, str(e))
-
-        # Create the settings dir as needed.
-        if not os.path.exists(self.settings_dir):
-            os.makedirs(self.settings_dir)
-
-        path = os.path.join(self.settings_dir, section_name + '.json')
-
-        with open(path, 'w') as fid:
-            json.dump(data, fid)
-
-        self.set_status(204)
-
-
-# The path for a lab settings section.
-settings_path = r"/lab/api/settings/(?P<section_name>[\w.-]+)"