Skip to content

Commit

Permalink
Merge pull request #1080 from scitran/resolve-ids
Browse files Browse the repository at this point in the history
Support <id:xyz> syntax in resolver
  • Loading branch information
kofalt authored Feb 16, 2018
2 parents 5b975be + 559336c commit cb3525a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
68 changes: 48 additions & 20 deletions api/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from . import config
from .web.errors import APINotFoundException

class Node(object):

Expand Down Expand Up @@ -33,7 +34,7 @@ def get_children(parent):
raise NotImplementedError() # pragma: no cover

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
raise NotImplementedError() # pragma: no cover

def _get_files(table, match):
Expand Down Expand Up @@ -72,8 +73,8 @@ def get_children(parent):
return []

@staticmethod
def filter(children, criterion):
raise Exception("Files have no children")
def filter(children, criterion, _id=False):
raise APINotFoundException("Files have no children")

class AcquisitionNode(Node):
@staticmethod
Expand All @@ -83,11 +84,11 @@ def get_children(parent):
return files

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
for x in children:
if x['node_type'] == "file" and x.get('name') == criterion:
return x, FileNode
raise Exception('No ' + criterion + ' file found.')
raise APINotFoundException('No ' + criterion + ' file found.')

class SessionNode(Node):

Expand All @@ -99,13 +100,20 @@ def get_children(parent):
return list(acqs) + files

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
if _id:
selectAcq = '_id'
selectFil = '_id'
else:
selectAcq = 'label'
selectFil = 'name'

for x in children:
if x['node_type'] == "acquisition" and x.get('label') == criterion:
if x['node_type'] == "acquisition" and str(x.get(selectAcq)) == criterion:
return x, AcquisitionNode
if x['node_type'] == "file" and x.get('name') == criterion:
if x['node_type'] == "file" and str(x.get(selectFil)) == criterion:
return x, FileNode
raise Exception('No ' + criterion + ' acquisition or file found.')
raise APINotFoundException('No ' + criterion + ' acquisition or file found.')

class ProjectNode(Node):

Expand All @@ -117,13 +125,20 @@ def get_children(parent):
return list(sessions) + files

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
if _id:
selectSes = '_id'
selectFil = '_id'
else:
selectSes = 'label'
selectFil = 'name'

for x in children:
if x['node_type'] == "session" and x.get('label') == criterion:
if x['node_type'] == "session" and str(x.get(selectSes)) == criterion:
return x, SessionNode
if x['node_type'] == "file" and x.get('name') == criterion:
if x['node_type'] == "file" and str(x.get(selectFil)) == criterion:
return x, FileNode
raise Exception('No ' + criterion + ' session or file found.')
raise APINotFoundException('No ' + criterion + ' session or file found.')

class GroupNode(Node):

Expand All @@ -133,11 +148,16 @@ def get_children(parent):
return projects

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
if _id:
select = '_id'
else:
select = 'label'

for x in children:
if x.get('label') == criterion:
if str(x.get(select)) == criterion:
return x, ProjectNode
raise Exception('No ' + criterion + ' project found.')
raise APINotFoundException('No ' + criterion + ' project found.')

class RootNode(Node):

Expand All @@ -147,11 +167,11 @@ def get_children(parent):
return groups

@staticmethod
def filter(children, criterion):
def filter(children, criterion, _id=False):
for x in children:
if x.get('_id') == criterion:
return x, GroupNode
raise Exception('No ' + criterion + ' group found.')
raise APINotFoundException('No ' + criterion + ' group found.')


class Resolver(object):
Expand Down Expand Up @@ -188,9 +208,17 @@ def _resolve(path, node, parents=None):
if len(path) == 0:
return node, parents, last

current = path[0]
current = path[0]
current_id = False

# Check for <id:xyz> syntax
if current.startswith('<id:') and current.endswith('>'):
current = current[4:len(current)-1]
current_id = True
print current

children = node.get_children(last)
selected, next_ = node.filter(children, current)
selected, next_ = node.filter(children, current, current_id)

path = path[1:]
parents.append(selected)
Expand Down
22 changes: 16 additions & 6 deletions tests/integration_tests/python/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_resolver(data_builder, as_admin, as_user, as_public, file_form):

# try to resolve non-existent root/child
r = as_admin.post('/resolve', json={'path': ['child']})
assert r.status_code == 500
assert r.status_code == 404


# GROUP
Expand All @@ -59,7 +59,7 @@ def test_resolver(data_builder, as_admin, as_user, as_public, file_form):

# try to resolve non-existent root/group/child
r = as_admin.post('/resolve', json={'path': [group, 'child']})
assert r.status_code == 500
assert r.status_code == 404


# PROJECT
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_resolver(data_builder, as_admin, as_user, as_public, file_form):

# try to resolve non-existent root/group/project/child
r = as_admin.post('/resolve', json={'path': [group, project_label, 'child']})
assert r.status_code == 500
assert r.status_code == 404


# SESSION
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_resolver(data_builder, as_admin, as_user, as_public, file_form):

# try to resolve non-existent root/group/project/session/child
r = as_admin.post('/resolve', json={'path': [group, project_label, session_label, 'child']})
assert r.status_code == 500
assert r.status_code == 404


# ACQUISITION
Expand Down Expand Up @@ -170,12 +170,22 @@ def test_resolver(data_builder, as_admin, as_user, as_public, file_form):
assert path_in_result([group, project, session, acquisition, acquisition_file], result)
assert result['children'] == []

def idz(s):
return '<id:' + s + '>'

# resolve root/group/project/session/acquisition/file with id
r = as_admin.post('/resolve', json={'path': [idz(group), idz(project), idz(session), idz(acquisition), acquisition_file]})
result = r.json()
assert r.ok
assert path_in_result([group, project, session, acquisition, acquisition_file], result)
assert result['children'] == []

# try to resolve non-existent root/group/project/session/acquisition/child
r = as_admin.post('/resolve', json={'path': [group, project_label, session_label, acquisition_label, 'child']})
assert r.status_code == 500
assert r.status_code == 404


# FILE
# try to resolve non-existent (also invalid) root/group/project/session/acquisition/file/child
r = as_admin.post('/resolve', json={'path': [group, project_label, session_label, acquisition_label, acquisition_file, 'child']})
assert r.status_code == 500
assert r.status_code == 404

0 comments on commit cb3525a

Please sign in to comment.