Commit 00032476 authored by Tanner Prestegard's avatar Tanner Prestegard

rest: improve arg validation in gracedb.createSuperevent

parent 4fc8ddb6
...@@ -1157,7 +1157,7 @@ class GraceDb(GsiRest): ...@@ -1157,7 +1157,7 @@ class GraceDb(GsiRest):
return self.get(uri).json()['numRows'] return self.get(uri).json()['numRows']
def createSuperevent(self, t_start, t_0, t_end, preferred_event, def createSuperevent(self, t_start, t_0, t_end, preferred_event,
category='production', events=[], labels=None): category='production', events=None, labels=None):
"""Create a superevent. """Create a superevent.
All LIGO-Virgo users can create test superevents, but special All LIGO-Virgo users can create test superevents, but special
...@@ -1201,28 +1201,40 @@ class GraceDb(GsiRest): ...@@ -1201,28 +1201,40 @@ class GraceDb(GsiRest):
if labels: if labels:
if isinstance(labels, six.string_types): if isinstance(labels, six.string_types):
labels = [labels] labels = [labels]
elif isinstance(labels, list): elif isinstance(labels, (list, tuple)):
pass # Validate each entry
if any([not isinstance(l, six.string_types) for l in labels]):
err_msg = "One of the provided labels is not a string"
raise TypeError(err_msg)
else: else:
raise TypeError("labels arg is {0}, should be str or list" raise TypeError("labels arg is {0}, should be str or list"
.format(type(labels))) .format(type(labels)))
# Check labels against those in database # Check labels against those in database
for l in labels: for l in labels:
if l not in self.allowed_labels: if l not in self.allowed_labels:
raise NameError(("Label '{0}' does not exist in the " raise ValueError(("Label '{0}' does not exist in the "
"database").format(l)) "database").format(l))
if events: if events:
if isinstance(events, six.string_types): if isinstance(events, six.string_types):
events = [events] events = [events]
elif isinstance(events, list): elif isinstance(events, (list, tuple)):
pass if any([not isinstance(e, six.string_types) for e in events]):
err_msg = \
"One of the provided event graceids is not a string"
raise TypeError(err_msg)
else: else:
raise TypeError("events arg is {0}, should be str or list" raise TypeError("events arg is {0}, should be str or list"
.format(type(events))) .format(type(events)))
# validate category, convert to short form if necessary # Validate category, convert to short form if necessary
category = self._getCode(category.lower(), if not isinstance(category, six.string_types):
self.superevent_categories) err_msg = "category arg is {0}, should be a string".format(
type(category))
raise TypeError(err_msg)
category = self._getCode(category, self.superevent_categories)
# Note: category can be None here as a result of processing
# by _getCode, which indicates failure to match the provided
# superevent category
if not category: if not category:
raise ValueError("category must be one of: {0}".format( raise ValueError("category must be one of: {0}".format(
list(six.itervalues(self.superevent_categories)))) list(six.itervalues(self.superevent_categories))))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment