Skip to content

Commit

Permalink
Merge pull request #50 from MaxTuecke/servable_description
Browse files Browse the repository at this point in the history
Fixing describe_servable and describe_methods for consistency
  • Loading branch information
WardLT committed Mar 5, 2019
2 parents 441298d + 16ca6e8 commit c6fe7fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
21 changes: 11 additions & 10 deletions dlhub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,39 +128,40 @@ def get_task_status(self, task_id):
r = self.get("{task_id}/status".format(task_id=task_id))
return r.json()

def describe_servable(self, owner, name):
def describe_servable(self, name):
"""Get the description for a certain servable
Args:
owner (string): Username of the owner of the servable
name (string): Name of the servable
name (string): DLHub name of the servable of the form <user>/<servable_name>
Returns:
dict: Summary of the servable
"""
split_name = name.split('/')
if len(split_name) < 2:
raise AttributeError('Please enter name in the form <user>/<servable_name>')

# Create a query for a single servable
query = self.query.match_servable(name)\
.match_owner(owner).add_sort("dlhub.publication_date", False)\
query = self.query.match_servable('/'.join(split_name[1:]))\
.match_owner(split_name[0]).add_sort("dlhub.publication_date", False)\
.search(limit=1)

# Raise error if servable is not found
if len(query) == 0:
raise AttributeError('No such servable: {}/{}'.format(owner, name))
raise AttributeError('No such servable: {}'.format(name))
return query[0]

def describe_methods(self, owner, name, method=None):
def describe_methods(self, name, method=None):
"""Get the description for the method(s) of a certain servable
Args:
owner (string): Username of the owner of the servable
name (string): Name of the servable
name (string): DLHub name of the servable of the form <user>/<servable_name>
method (string): Optional: Name of the method
Returns:
dict: Description of a certain method if ``method`` provided, all methods
if the method name was not provided.
"""

metadata = self.describe_servable(owner, name)
metadata = self.describe_servable(name)
return get_method_details(metadata, method)

def run(self, name, inputs, input_type='python'):
Expand Down
10 changes: 5 additions & 5 deletions dlhub_sdk/tests/test_dlhub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ def test_submit(self):

def test_describe_model(self):
# Find the 1d_norm function from the test user (should be there)
description = self.dl.describe_servable('dlhub.test_gmail', '1d_norm')
description = self.dl.describe_servable('dlhub.test_gmail/1d_norm')
self.assertEqual('dlhub.test_gmail', description['dlhub']['owner'])
self.assertEqual('1d_norm', description['dlhub']['name'])

# Give it a bogus name, check the error
with self.assertRaises(AttributeError) as exc:
self.dl.describe_servable('dlhub.test_gmail', 'nonexistant')
self.dl.describe_servable('dlhub.test_gmail/nonexistant')
self.assertIn('No such servable', str(exc.exception))

# Get only the method details
expected = dict(description['servable']['methods'])
del expected['run']['method_details']
methods = self.dl.describe_methods('dlhub.test_gmail', '1d_norm')
methods = self.dl.describe_methods('dlhub.test_gmail/1d_norm')
self.assertEqual(expected, methods)

method = self.dl.describe_methods('dlhub.test_gmail', '1d_norm', 'run')
method = self.dl.describe_methods('dlhub.test_gmail/1d_norm', 'run')
self.assertEqual(expected['run'], method)

with self.assertRaises(ValueError) as exc:
self.dl.describe_methods('dlhub.test_gmail', '1d_norm', 'notamethod')
self.dl.describe_methods('dlhub.test_gmail/1d_norm', 'notamethod')
self.assertIn('No such method', str(exc.exception))

def test_search_by_servable(self):
Expand Down

0 comments on commit c6fe7fa

Please sign in to comment.