Browse Source

Clean up theme and setting handling

Steven Silvester 7 years ago
parent
commit
ab35a2f9d3

+ 46 - 29
jupyterlab/commands.py

@@ -156,6 +156,19 @@ def install_extension_async(extension, app_dir=None, logger=None, abort_callback
         )
         raise ValueError(msg)
 
+    # Handle any schemas.
+    schemaDir = data['jupyterlab'].get('schemaDir', None)
+    if schemaDir:
+        dest = pjoin(app_dir, 'schemas')
+        _copy_tar_files(fname, schemaDir, dest)
+
+    # Handle a theme.
+    themeDir = data['jupyterlab'].get('themeDir', None)
+    if themeDir:
+        normedName = data['name'].replace('@', '').replace('/', '')
+        dest = pjoin(app_dir, 'themes', normedName)
+        _copy_tar_files(fname, themeDir, dest)
+
     # Remove an existing extension tarball.
     ext_path = pjoin(app_dir, 'extensions', fname)
     if os.path.exists(ext_path):
@@ -170,13 +183,6 @@ def install_extension_async(extension, app_dir=None, logger=None, abort_callback
     if os.path.exists(target):
         shutil.rmtree(target)
 
-    # Handle any schemas.
-    schema_data = data['jupyterlab'].get('schema_data', dict())
-    for (key, value) in schema_data.items():
-        path = pjoin(app_dir, 'schemas', key + '.json')
-        with open(path, 'w') as fid:
-            fid.write(value)
-
 
 def link_package(path, app_dir=None, logger=None):
     """Link a package against the JupyterLab build."""
@@ -796,20 +802,25 @@ def _ensure_package(app_dir, logger=None, name=None, version=None):
     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')
-    if not os.path.exists(schema_local):
-        os.makedirs(schema_local)
+    # Copy any missing or outdated schema or theme items.
+    for item in ['schemas', 'themes']:
+        local = pjoin(here, item)
+        if not os.path.exists(local):
+            os.makedirs(local)
 
-    for schema in os.listdir(schema_local):
-        dest = pjoin(app_dir, 'schemas', schema)
-        if version_updated or not os.path.exists(dest):
-            shutil.copy(pjoin(schema_local, schema), dest)
+        for item_path in os.listdir(local):
+            src = pjoin(local, item_path)
+            dest = pjoin(app_dir, item, item_path)
+            if version_updated or not os.path.exists(dest):
+                if os.path.isdir(src):
+                    shutil.copytree(src, dest)
+                else:
+                    shutil.copy(src, dest)
 
 
 def _ensure_app_dirs(app_dir, logger):
     """Ensure that the application directories exist"""
-    dirs = ['extensions', 'settings', 'schemas', 'staging']
+    dirs = ['extensions', 'settings', 'schemas', 'themes', 'staging']
     for dname in dirs:
         path = pjoin(app_dir, dname)
         if not osp.exists(path):
@@ -970,22 +981,25 @@ def _read_package(target):
     tar = tarfile.open(target, "r:gz")
     f = tar.extractfile('package/package.json')
     data = json.loads(f.read().decode('utf8'))
-    jlab = data.get('jupyterlab', None)
-    if not jlab:
-        return data
-    schemas = jlab.get('schemas', None)
-    if not schemas:
-        return data
-    schema_data = dict()
-    for schema in schemas:
-        f = tar.extractfile('package/' + schema)
-        key = schema.split('/')[-1]
-        key = key.replace('.json', '')
-        schema_data[key] = f.read().decode('utf8')
-    data['jupyterlab']['schema_data'] = schema_data
+    tar.close()
     return data
 
 
+def _copy_tar_files(fname, source, dest):
+    """Copy the files from a target path to the destination.
+    """
+    tar = tarfile.open(fname, "r:gz")
+    subdir_and_files = [
+        tarinfo for tarinfo in tar.getmembers()
+        if tarinfo.name.startswith('package/' + source)
+    ]
+    offset = len('package/' + source + '/')
+    for member in subdir_and_files:
+        member.path = member.path[offset:]
+    tar.extractall(path=dest, members=subdir_and_files)
+    tar.close()
+
+
 def _normalize_path(extension):
     """Normalize a given extension if it is a path.
     """
@@ -993,3 +1007,6 @@ def _normalize_path(extension):
     if osp.exists(extension):
         extension = osp.abspath(extension)
     return extension
+
+if __name__ == '__main__':
+    _copy_tar_files('jupyterlab/jupyterlab-theme-dark-extension-0.9.0.tgz', 'style', '')

+ 14 - 11
jupyterlab/update-core.js

@@ -53,20 +53,23 @@ packages.forEach(function(packagePath) {
   });
 
   // Handle schemas.
-  var schemas = jlab['schemas'] || [];
-  schemas.forEach(function(schemaPath) {
-    var file = path.basename(schemaPath);
-    var from = path.join(packagePath, schemaPath)
-    var to = path.join(basePath, 'jupyterlab', 'schemas', file);
-    fs.copySync(from, to);
-  });
+  var schemaDir = jlab['schemaDir'];
+  if (schemaDir) {
+    schemaDir = path.join(packagePath, schemaDir);
+    var schemas = glob.sync(path.join(schemaDir, '*'));
+    schemas.forEach(function(schemaPath) {
+      var file = path.basename(schemaPath);
+      var to = path.join(basePath, 'jupyterlab', 'schemas', file);
+      fs.copySync(schemaPath, to);
+    });
+  }
 
-  // Handle theme assets.
-  var themes = jlab['themeAssets'];
-  if (themes) {
+  // Handle themes.
+  var themeDir = jlab['themeDir'];
+  if (themeDir) {
     var name = data['name'].replace('@', '');
     name = name.replace('/', '-');
-    var from = path.join(packagePath, themes);
+    var from = path.join(packagePath, themeDir);
     var to = path.join(basePath, 'jupyterlab', 'themes', name);
     fs.copySync(from, to);
   }

+ 1 - 3
packages/apputils-extension/package.json

@@ -31,9 +31,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "schemas": [
-      "schema/jupyter.services.theme-manager.json"
-    ]
+    "schemaDir": "schema"
   },
   "repository": {
     "type": "git",

+ 1 - 3
packages/codemirror-extension/package.json

@@ -33,9 +33,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "schemas": [
-      "schema/jupyter.services.codemirror-commands.json"
-    ]
+    "schemaDir": "schema"
   },
   "repository": {
     "type": "git",

+ 1 - 3
packages/fileeditor-extension/package.json

@@ -31,9 +31,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "schemas": [
-      "schema/jupyter.services.editor-tracker.json"
-    ]
+    "schemaDir": "schema"
   },
   "repository": {
     "type": "git",

+ 1 - 3
packages/shortcuts-extension/package.json

@@ -30,9 +30,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "schemas": [
-      "schema/jupyter.extensions.shortcuts.json"
-    ]
+    "schemaDir": "schema"
   },
   "repository": {
     "type": "git",

+ 1 - 1
packages/theme-dark-extension/package.json

@@ -25,7 +25,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "themeAssets": "style"
+    "themeDir": "style"
   },
   "author": "Project Jupyter",
   "license": "BSD-3-Clause",

+ 1 - 1
packages/theme-light-extension/package.json

@@ -25,7 +25,7 @@
   },
   "jupyterlab": {
     "extension": true,
-    "themeAssets": "style"
+    "themeDir": "style"
   },
   "author": "Project Jupyter",
   "license": "BSD-3-Clause",