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):
return self.get(uri).json()['numRows']
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.
All LIGO-Virgo users can create test superevents, but special
......@@ -1201,28 +1201,40 @@ class GraceDb(GsiRest):
if labels:
if isinstance(labels, six.string_types):
labels = [labels]
elif isinstance(labels, list):
pass
elif isinstance(labels, (list, tuple)):
# 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:
raise TypeError("labels arg is {0}, should be str or list"
.format(type(labels)))
# Check labels against those in database
for l in labels:
if l not in self.allowed_labels:
raise NameError(("Label '{0}' does not exist in the "
"database").format(l))
raise ValueError(("Label '{0}' does not exist in the "
"database").format(l))
if events:
if isinstance(events, six.string_types):
events = [events]
elif isinstance(events, list):
pass
elif isinstance(events, (list, tuple)):
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:
raise TypeError("events arg is {0}, should be str or list"
.format(type(events)))
# validate category, convert to short form if necessary
category = self._getCode(category.lower(),
self.superevent_categories)
# Validate category, convert to short form if necessary
if not isinstance(category, six.string_types):
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:
raise ValueError("category must be one of: {0}".format(
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