Browse Source

Make the lab config directory configurable

Steven Silvester 8 years ago
parent
commit
6e4898e079
4 changed files with 48 additions and 32 deletions
  1. 25 24
      jupyterlab/commands.py
  2. 5 3
      jupyterlab/extension.py
  3. 4 0
      jupyterlab/labapp.py
  4. 14 5
      jupyterlab/labextensions.py

+ 25 - 24
jupyterlab/commands.py

@@ -34,7 +34,7 @@ def run(cmd, **kwargs):
     return check_output(cmd, **kwargs)
 
 
-def install_extension(extension):
+def install_extension(extension, config_dir=None):
     """Install an extension package into JupyterLab.
 
     Follows the semantics of https://docs.npmjs.com/cli/install.
@@ -44,16 +44,16 @@ def install_extension(extension):
     If link is true, the source directory is linked using `npm link`.
     """
     tar_name, pkg_name = validate_extension(extension)
-    config = _get_config()
+    config = _get_config(config_dir)
     path = pjoin(_get_cache_dir(config), tar_name)
     run(['npm', 'install', '--save', path], cwd=_get_root_dir(config))
     config['installed_extensions'][pkg_name] = path
     if pkg_name in config['linked_extensions']:
         del config['linked_extensions'][pkg_name]
-    _write_config(config)
+    _write_config(config, config_dir)
 
 
-def link_extension(extension):
+def link_extension(extension, config_dir=None):
     """Link an extension against the JupyterLab build.
     """
     path = _normalize_path(extension)
@@ -70,19 +70,19 @@ def link_extension(extension):
     _validate_package(data, path)
 
     # Update JupyterLab metadata.
-    config = _get_config()
+    config = _get_config(config_dir)
     name = data['name']
     config['linked_extensions'][name] = path
     if name in config['installed_extensions']:
         del config['installed_extensions'][name]
-    _write_config(config)
+    _write_config(config, config_dir)
 
 
-def unlink_extension(extension):
+def unlink_extension(extension, config_dir=None):
     """Unlink an extension from JupyterLab by path or name.
     """
     extension = _normalize_path(extension)
-    config = _get_config()
+    config = _get_config(config_dir)
 
     name = None
     for (key, value) in config['linked_extensions'].items():
@@ -92,40 +92,40 @@ def unlink_extension(extension):
 
     if name:
         del config['linked_extensions'][name]
-        _write_config(config)
+        _write_config(config, config_dir)
         return True
 
     print('No labextension matching "%s" is linked' % extension)
     return False
 
 
-def uninstall_extension(name):
+def uninstall_extension(name, config_dir=None):
     """Uninstall an extension by name.
     """
-    config = _get_config()
+    config = _get_config(config_dir)
 
     if name in config['installed_extensions']:
         del config['installed_extensions'][name]
-        _write_config(config)
+        _write_config(config, config_dir)
         return True
 
     print('No labextension named "%s" installed' % name)
     return False
 
 
-def list_extensions():
+def list_extensions(config_dir=None):
     """List installed extensions.
     """
-    config = _get_config()
+    config = _get_config(config_dir)
     installed = list(config['installed_extensions'])
     linked = list(config['linked_extensions'])
     return sorted(installed + linked)
 
 
-def validate_extension(extension):
+def validate_extension(extension, config_dir=None):
     """Verify that a JupyterLab extension is valid.
     """
-    config = _get_config()
+    config = _get_config(config_dir)
     extension = _normalize_path(extension)
     _ensure_package(config)
     cache_dir = _get_cache_dir(config)
@@ -140,19 +140,19 @@ def validate_extension(extension):
     return name, data['name']
 
 
-def clean():
+def clean(config_dir=None):
     """Clean the JupyterLab application directory."""
-    config = _get_config()
+    config = _get_config(config_dir)
     for name in ['node_modules', 'build']:
         target = pjoin(_get_root_dir(config), name)
         if osp.exists(target):
             shutil.rmtree(target)
 
 
-def build():
+def build(config_dir=None):
     """Build the JupyterLab application."""
     # Set up the build directory.
-    config = _get_config()
+    config = _get_config(config_dir)
     _ensure_package(config)
     root = _get_root_dir(config)
 
@@ -216,10 +216,10 @@ def _validate_package(data, extension):
         raise ValueError(msg)
 
 
-def _get_config():
+def _get_config(config_dir=None):
     """Get the JupyterLab config data.
     """
-    config_dir = _get_config_dir()
+    config_dir = config_dir or _get_config_dir()
     file = pjoin(config_dir, 'build_config.json')
     if not osp.exists(file):
         if not osp.exists(config_dir):
@@ -234,10 +234,11 @@ def _get_config():
     return data
 
 
-def _write_config(data):
+def _write_config(data, config_dir=None):
     """Write the JupyterLab config data.
     """
-    with open(pjoin(_get_config_dir(), 'build_config.json'), 'w') as fid:
+    config_dir = config_dir or _get_config_dir()
+    with open(pjoin(config_dir, 'build_config.json'), 'w') as fid:
         json.dump(data, fid, indent=4)
 
 

+ 5 - 3
jupyterlab/extension.py

@@ -10,7 +10,7 @@ from tornado import web
 from notebook.base.handlers import IPythonHandler, FileFindHandler
 from jinja2 import FileSystemLoader
 from notebook.utils import url_path_join as ujoin
-from .commands import _get_build_dir, _get_config_dir
+from .commands import _get_build_dir, _get_config
 
 
 #-----------------------------------------------------------------------------
@@ -88,13 +88,15 @@ def add_handlers(app):
     prefix = ujoin(base_url, PREFIX)
 
     # Handle page config data.
+    config_dir = app.lab_config_dir
+    config = _get_config(config_dir)
     page_config_data = web_app.settings.get('page_config_data', {})
-    page_config_file = os.path.join(_get_config_dir(), 'page_config_data.json')
+    page_config_file = os.path.join(config_dir, 'page_config_data.json')
     if os.path.exists(page_config_file):
         with open(page_config_file) as fid:
             page_config_data.update(json.load(fid))
 
-    built_files = _get_build_dir()
+    built_files = _get_build_dir(config)
 
     # Check for dev mode.
     dev_mode = False

+ 4 - 0
jupyterlab/labapp.py

@@ -6,6 +6,7 @@
 
 from notebook.notebookapp import NotebookApp, flags
 from jupyter_core.application import JupyterApp
+from jupyter_core.paths import ENV_CONFIG_PATH
 
 from traitlets import Bool, Unicode
 
@@ -74,6 +75,9 @@ class LabApp(NotebookApp):
     dev_mode = Bool(False, config=True,
         help="Whether to start the app in dev mode")
 
+    lab_config_dir = Bool(ENV_CONFIG_PATH[0], config=True,
+        help="The lab configuration directory")
+
     def init_server_extensions(self):
         """Load any extensions specified by config.
 

+ 14 - 5
jupyterlab/labextensions.py

@@ -7,6 +7,7 @@ import os
 import sys
 
 from jupyter_core.application import JupyterApp, base_flags
+from jupyter_core.paths import ENV_CONFIG_PATH
 from traitlets import Bool
 
 from ._version import __version__
@@ -27,6 +28,9 @@ class BaseExtensionApp(JupyterApp):
     version = __version__
     flags = flags
 
+    lab_config_dir = Bool(ENV_CONFIG_PATH[0], config=True,
+        help="The lab configuration directory")
+
     should_build = Bool(True, config=True,
         help="Whether to build the app after the action")
 
@@ -36,7 +40,7 @@ class InstallLabExtensionApp(BaseExtensionApp):
 
     def start(self):
         self.extra_args = self.extra_args or [os.getcwd()]
-        [install_extension(arg) for arg in self.extra_args]
+        [install_extension(arg, self.config_dir) for arg in self.extra_args]
         if self.should_build:
             build()
 
@@ -46,7 +50,7 @@ class LinkLabExtensionApp(BaseExtensionApp):
 
     def start(self):
         self.extra_args = self.extra_args or [os.getcwd()]
-        [link_extension(arg) for arg in self.extra_args]
+        [link_extension(arg, self.config_dir) for arg in self.extra_args]
         if self.should_build:
             build()
 
@@ -56,7 +60,8 @@ class UnlinkLabExtensionApp(BaseExtensionApp):
 
     def start(self):
         self.extra_args = self.extra_args or [os.getcwd()]
-        ans = any([unlink_extension(arg) for arg in self.extra_args])
+        ans = any([unlink_extension(arg, self.config_dir)
+                   for arg in self.extra_args])
         if ans and self.should_build:
             build()
 
@@ -66,7 +71,8 @@ class UninstallLabExtensionApp(BaseExtensionApp):
 
     def start(self):
         self.extra_args = self.extra_args or [os.getcwd()]
-        ans = any([uninstall_extension(arg) for arg in self.extra_args])
+        ans = any([uninstall_extension(arg, self.config_dir)
+                   for arg in self.extra_args])
         if ans and self.should_build:
             build()
 
@@ -75,8 +81,11 @@ class ListLabExtensionsApp(JupyterApp):
     version = __version__
     description = "Install a labextension"
 
+    lab_config_dir = Bool(ENV_CONFIG_PATH[0], config=True,
+        help="The lab configuration directory")
+
     def start(self):
-        [print(ext) for ext in list_extensions()]
+        [print(ext) for ext in list_extensions(self.config_dir)]
 
 
 _examples = """