Explorar el Código

Merge pull request #5695 from Zsailer/workspace-flag

add 'name' flag to workspaces import CLI
Ian Rose hace 6 años
padre
commit
44c0a90316
Se han modificado 1 ficheros con 31 adiciones y 12 borrados
  1. 31 12
      jupyterlab/labapp.py

+ 31 - 12
jupyterlab/labapp.py

@@ -129,7 +129,6 @@ class LabWorkspaceExportApp(JupyterApp):
     If a workspace name is passed in, this command will export that workspace.
     If no workspace is found, this command will export an empty workspace.
     """
-
     def start(self):
         app = LabApp(config=self.config)
         base_url = app.base_url
@@ -164,6 +163,20 @@ class LabWorkspaceImportApp(JupyterApp):
     This command will import a workspace from a JSON file. The format of the
         file must be the same as what the export functionality emits.
     """
+    workspace_name = Unicode(
+        None,
+        config=True,
+        allow_none=True,
+        help="""
+        Workspace name. If given, the workspace ID in the imported
+        file will be replaced with a new ID pointing to this
+        workspace name.
+        """
+    )
+
+    aliases = {
+        'name': 'LabWorkspaceImportApp.workspace_name'
+    }
 
     def start(self):
         app = LabApp(config=self.config)
@@ -214,16 +227,23 @@ class LabWorkspaceImportApp(JupyterApp):
         if 'data' not in workspace:
             raise Exception('The `data` field is missing.')
 
-        if 'metadata' not in workspace:
-            raise Exception('The `metadata` field is missing.')
-
-        if 'id' not in workspace['metadata']:
-            raise Exception('The `id` field is missing in `metadata`.')
-
-        id = workspace['metadata']['id']
-        if id != ujoin(base_url, page_url) and not id.startswith(ujoin(base_url, workspaces_url)):
-            error = '%s does not match page_url or start with workspaces_url.'
-            raise Exception(error % id)
+        # If workspace_name is set in config, inject the
+        # name into the workspace metadata.
+        if self.workspace_name is not None:
+            if self.workspace_name == "":
+                workspace_id = ujoin(base_url, page_url)
+            else:
+                workspace_id = ujoin(base_url, workspaces_url, self.workspace_name)
+            workspace['metadata'] = {'id': workspace_id}
+        # else check that the workspace_id is valid.
+        else:
+            if 'id' not in workspace['metadata']:
+                raise Exception('The `id` field is missing in `metadata`.')
+            else:
+                id = workspace['metadata']['id']
+                if id != ujoin(base_url, page_url) and not id.startswith(ujoin(base_url, workspaces_url)):
+                    error = '%s does not match page_url or start with workspaces_url.'
+                    raise Exception(error % id)
 
         return workspace
 
@@ -236,7 +256,6 @@ class LabWorkspaceApp(JupyterApp):
     There are two sub-commands for export or import of workspaces. This app
         should not otherwise do any work.
     """
-
     subcommands = dict()
     subcommands['export'] = (
         LabWorkspaceExportApp,