Browse Source

Merge pull request #6276 from wonsjb/ISSUE-6272

Allow passing "-" as a file to jupyter workspaces import
Steven Silvester 6 years ago
parent
commit
0af6067a79
1 changed files with 16 additions and 9 deletions
  1. 16 9
      jupyterlab/labapp.py

+ 16 - 9
jupyterlab/labapp.py

@@ -187,19 +187,12 @@ class LabWorkspaceImportApp(JupyterApp):
             print('One argument is required for workspace import.')
             sys.exit(1)
 
-        file_name = self.extra_args[0]
-        file_path = os.path.abspath(file_name)
-
-        if not os.path.exists(file_path):
-            print('%s does not exist.' % file_name)
-            sys.exit(1)
-
         workspace = dict()
-        with open(file_path) as fid:
+        with self._smart_open() as fid:
             try:  # to load, parse, and validate the workspace file.
                 workspace = self._validate(fid, base_url, page_url, workspaces_url)
             except Exception as e:
-                print('%s is not a valid workspace:\n%s' % (file_name, e))
+                print('%s is not a valid workspace:\n%s' % (fid.name, e))
                 sys.exit(1)
 
         if not os.path.exists(directory):
@@ -218,6 +211,20 @@ class LabWorkspaceImportApp(JupyterApp):
 
         print('Saved workspace: %s' % workspace_path)
 
+    def _smart_open(self):
+        file_name = self.extra_args[0]
+
+        if file_name == '-':
+            return sys.stdin
+        else:
+            file_path = os.path.abspath(file_name)
+
+            if not os.path.exists(file_path):
+                print('%s does not exist.' % file_name)
+                sys.exit(1)
+            
+            return open(file_path)
+
     def _validate(self, data, base_url, page_url, workspaces_url):
         workspace = json.load(data)