瀏覽代碼

Handle user settings dir

Steven Silvester 7 年之前
父節點
當前提交
26e4830538
共有 3 個文件被更改,包括 44 次插入15 次删除
  1. 24 3
      jupyterlab/commands.py
  2. 10 7
      jupyterlab/extension.py
  3. 10 5
      jupyterlab/labapp.py

+ 24 - 3
jupyterlab/commands.py

@@ -17,7 +17,7 @@ from subprocess import check_output, CalledProcessError, STDOUT
 import shutil
 import sys
 import tarfile
-from jupyter_core.paths import ENV_JUPYTER_PATH
+from jupyter_core.paths import ENV_JUPYTER_PATH, jupyter_config_path
 from notebook.nbextensions import (
     GREEN_ENABLED, GREEN_OK, RED_DISABLED, RED_X
 )
@@ -45,6 +45,16 @@ def get_app_dir(app_dir=None):
     return os.path.realpath(app_dir)
 
 
+def get_user_settings_dir():
+    """Get the configured JupyterLab app directory.
+    """
+    settings_dir = os.environ.get('JUPYTERLAB_SETTINGS_DIR')
+    settings_dir = settings_dir or pjoin(
+        jupyter_config_path()[0], 'lab', 'userSettings'
+    )
+    return os.path.realpath(settings_dir)
+
+
 def run(cmd, **kwargs):
     """Run a command in the given working directory.
     """
@@ -659,11 +669,13 @@ def _ensure_package(app_dir, name=None, version=None, logger=None):
 
     # Look for mismatched version.
     pkg_path = pjoin(staging, 'package.json')
+    version_updated = False
     if os.path.exists(pkg_path):
         with open(pkg_path) as fid:
             data = json.load(fid)
         if data['jupyterlab'].get('version', '') != __version__:
             shutil.rmtree(staging)
+            version_updated = True
 
     if not os.path.exists(staging):
         os.makedirs(staging)
@@ -707,12 +719,21 @@ def _ensure_package(app_dir, name=None, version=None, logger=None):
     if version:
         data['jupyterlab']['version'] = version
 
-    data['scripts']['build'] = 'webpack'
-
     pkg_path = pjoin(staging, 'package.json')
     with open(pkg_path, 'w') as fid:
         json.dump(data, fid, indent=4)
 
+    # Copy any missing or outdated schema files.
+    schema_local = pjoin(here, 'schemas')
+    schema_app = pjoin(app_dir, 'schemas')
+    if not os.path.exists(schema_app):
+        os.makedirs(schema_app)
+
+    for schema in os.listdir(schema_local):
+        dest = pjoin(schema_app, schema)
+        if version_updated or not os.path.exists(dest):
+            shutil.copy(pjoin(schema_local, schema), dest)
+
 
 def _is_extension(data):
     """Detect if a package is an extension using its metadata.

+ 10 - 7
jupyterlab/extension.py

@@ -7,7 +7,9 @@ import os
 
 from jupyterlab_launcher import add_handlers, LabConfig
 
-from .commands import get_app_dir, list_extensions, should_build
+from .commands import (
+    get_app_dir, list_extensions, should_build, get_user_settings_dir
+)
 from .settings_handler import settings_path, SettingsHandler
 from ._version import __version__
 
@@ -91,15 +93,16 @@ def load_jupyter_server_extension(nbapp):
 
     add_handlers(web_app, config)
 
+    user_settings_dir = get_user_settings_dir()
+
     if core_mode:
-        schemas_path = os.path.join(here, 'schemas')
-        user_settings_path = ''
+        schemas_dir = os.path.join(here, 'schemas')
+        user_settings_dir = ''
     else:
-        schemas_path = os.path.join(app_dir, 'schemas')
-        user_settings_path = os.path.join(app_dir, 'userSettings')
+        schemas_dir = os.path.join(app_dir, 'schemas')
 
     settings_handler = (settings_path, SettingsHandler, {
-        'schemas_path': schemas_path,
-        'settings_path': user_settings_path
+        'schemas_dir': schemas_dir,
+        'user_settings_dir': user_settings_dir
     })
     web_app.add_handlers(".*$", [settings_handler])

+ 10 - 5
jupyterlab/labapp.py

@@ -11,7 +11,7 @@ from traitlets import Bool, Unicode
 
 from ._version import __version__
 from .extension import load_jupyter_server_extension
-from .commands import build, clean, get_app_dir
+from .commands import build, clean, get_app_dir, get_user_settings_dir
 
 
 build_aliases = dict(base_aliases)
@@ -68,13 +68,17 @@ class LabCleanApp(JupyterApp):
 class LabPathApp(JupyterApp):
     version = __version__
     description = """
-    Print the configured path to the JupyterLab application
+    Print the configured paths for the JupyterLab application
 
-    The path can be configured using the JUPYTERLAB_DIR environment variable.
+    The application path can be configured using the JUPYTERLAB_DIR environment variable.
+    The user settings path can be configured using the JUPYTERLAB_SETTINGS_DIR
+        environment variable or it will fall back to
+        `/lab/userSettings` in the default Jupyter configuration directory.
     """
 
     def start(self):
-        print(get_app_dir())
+        print('Application directory:   %s' % get_app_dir())
+        print('User Settings directory: %s' % get_user_settings_dir())
 
 
 lab_aliases = dict(aliases)
@@ -129,7 +133,8 @@ class LabApp(NotebookApp):
     subcommands = dict(
         build=(LabBuildApp, LabBuildApp.description.splitlines()[0]),
         clean=(LabCleanApp, LabCleanApp.description.splitlines()[0]),
-        path=(LabPathApp, LabPathApp.description.splitlines()[0])
+        path=(LabPathApp, LabPathApp.description.splitlines()[0]),
+        paths=(LabPathApp, LabPathApp.description.splitlines()[0])
     )
 
     default_url = Unicode('/lab', config=True,