Skip to content

Commit

Permalink
Merge pull request #61 from afshin/workspaces-list
Browse files Browse the repository at this point in the history
Update workspaces listing to include values.
  • Loading branch information
afshin committed Feb 14, 2019
2 parents 2252f95 + c2a6872 commit 346c7d0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
3 changes: 2 additions & 1 deletion jupyterlab_server/tests/test_workspaces_api.py
Expand Up @@ -53,8 +53,9 @@ def test_get(self):
def test_listing(self):
# ID fields are from workspaces/*.jupyterlab-workspace
listing = set(['foo', 'f/o/o/'])
output = set(self.workspaces_api.get().json()['workspaces']['ids'])

assert set(self.workspaces_api.get().json()['workspaces']) == listing
assert output == listing

def test_put(self):
id = 'foo'
Expand Down
14 changes: 4 additions & 10 deletions jupyterlab_server/workspaces_handler.py
Expand Up @@ -15,16 +15,12 @@
# The JupyterLab workspace file extension.
WORKSPACE_EXTENSION = '.jupyterlab-workspace'

# A cache of workspace names and their slug file name counterparts.
_cache = dict()


def _list_workspaces(directory, prefix):
"""
Return the list of workspaces in a given directory beginning with the
given prefix.
"""
workspaces = []
workspaces = { 'ids': [], 'values': [] }
if not os.path.exists(directory):
return workspaces

Expand All @@ -35,15 +31,13 @@ def _list_workspaces(directory, prefix):
items.sort()

for slug in items:
if slug in _cache:
workspaces.append(_cache[slug])
continue
workspace_path = os.path.join(directory, slug)
if os.path.exists(workspace_path):
with open(workspace_path) as fid:
try: # to load and parse the workspace file.
_cache[slug] = json.load(fid)['metadata']['id']
workspaces.append(_cache[slug])
workspace = json.load(fid)
workspaces.get('ids').append(workspace['metadata']['id'])
workspaces.get('values').append(workspace)
except Exception as e:
raise web.HTTPError(500, str(e))
return workspaces
Expand Down

0 comments on commit 346c7d0

Please sign in to comment.