1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
"""
Common validation functions
"""
from pika.compat import basestring
def require_string(value, value_name):
"""Require that value is a string
:raises: TypeError
"""
if not isinstance(value, basestring):
raise TypeError('%s must be a str or unicode str, but got %r' % (
value_name,
value,
))
def require_callback(callback, callback_name='callback'):
"""Require that callback is callable and is not None
:raises: TypeError
"""
if not callable(callback):
raise TypeError('callback %s must be callable, but got %r' % (
callback_name,
callback,
))
def rpc_completion_callback(callback):
"""Verify callback is callable if not None
:returns: boolean indicating nowait
:rtype: bool
:raises: TypeError
"""
if callback is None:
# No callback means we will not expect a response
# i.e. nowait=True
return True
if callable(callback):
# nowait=False
return False
else:
raise TypeError('completion callback must be callable if not None')
def zero_or_greater(name, value):
"""Verify that value is zero or greater. If not, 'name'
will be used in error message
:raises: ValueError
"""
if int(value) < 0:
errmsg = '{} must be >= 0, but got {}'.format(name, value)
raise ValueError(errmsg)
|