Browse Source

Merge pull request #5695 from Zsailer/workspace-flag

add 'name' flag to workspaces import CLI
Ian Rose 6 years ago
parent
commit
44c0a90316
1 changed files with 31 additions and 12 deletions
  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 a workspace name is passed in, this command will export that workspace.
     If no workspace is found, this command will export an empty workspace.
     If no workspace is found, this command will export an empty workspace.
     """
     """
-
     def start(self):
     def start(self):
         app = LabApp(config=self.config)
         app = LabApp(config=self.config)
         base_url = app.base_url
         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
     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.
         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):
     def start(self):
         app = LabApp(config=self.config)
         app = LabApp(config=self.config)
@@ -214,16 +227,23 @@ class LabWorkspaceImportApp(JupyterApp):
         if 'data' not in workspace:
         if 'data' not in workspace:
             raise Exception('The `data` field is missing.')
             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
         return workspace
 
 
@@ -236,7 +256,6 @@ class LabWorkspaceApp(JupyterApp):
     There are two sub-commands for export or import of workspaces. This app
     There are two sub-commands for export or import of workspaces. This app
         should not otherwise do any work.
         should not otherwise do any work.
     """
     """
-
     subcommands = dict()
     subcommands = dict()
     subcommands['export'] = (
     subcommands['export'] = (
         LabWorkspaceExportApp,
         LabWorkspaceExportApp,