Skip to content

Commit

Permalink
Merge pull request #6276 from wonsjb/ISSUE-6272
Browse files Browse the repository at this point in the history
Allow passing "-" as a file to jupyter workspaces import
  • Loading branch information
blink1073 committed Apr 29, 2019
2 parents 392dba9 + c6b7487 commit 0af6067
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions jupyterlab/labapp.py
Expand Up @@ -187,19 +187,12 @@ def start(self):
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):
Expand All @@ -218,6 +211,20 @@ def start(self):

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)

Expand Down

0 comments on commit 0af6067

Please sign in to comment.