def _check_iterable(arg, msg, **kwargs): """Ensure we have an iterable which is not a string or an iterator""" if isinstance(arg, compat.string_types): raise exceptions.ValidationError(msg.format(arg=arg, **kwargs)) elif not isinstance(arg, collections.Iterable): raise exceptions.ValidationError(msg.format(arg=arg, **kwargs)) elif iter(arg) is iter(arg): raise exceptions.ValidationError(msg.format(arg=arg, **kwargs))
def check_integer(arg, min=None, max=None): if not isinstance(arg, compat.integer_types): raise exceptions.ValidationError('Expected an integer, not %r' % arg) elif min is not None and arg < min: raise exceptions.ValidationError( 'Expected number larger or equal to %d, not %r' % (min, arg)) elif max is not None and arg > max: raise exceptions.ValidationError( 'Expected number smaller or equal to %d, not %r' % (max, arg))
def check_integer(arg, min=None, max=None): if not isinstance(arg, int): raise exceptions.ValidationError(f"Expected an integer, not {arg!r}") elif min is not None and arg < min: raise exceptions.ValidationError( f"Expected number larger or equal to {min}, not {arg!r}") elif max is not None and arg > max: raise exceptions.ValidationError( f"Expected number smaller or equal to {max}, not {arg!r}")
def check_query(arg, fields=None, list_values=True): if fields is None: fields = SEARCH_FIELDS # TODO: normalize name -> track_name # TODO: normalize value -> [value] # TODO: normalize blank -> [] or just remove field? # TODO: remove list_values? if not isinstance(arg, Mapping): raise exceptions.ValidationError( f"Expected a query dictionary, not {arg!r}") for key, value in arg.items(): check_choice( key, fields, msg="Expected query field to be one of " "{choices}, not {arg!r}", ) if list_values: msg = 'Expected "{key}" to be list of strings, not {arg!r}' _check_iterable(value, msg, key=key) [_check_query_value(key, v, msg) for v in value] else: _check_query_value(key, value, 'Expected "{key}" to be a string, not {arg!r}')
def get_images(self, uris): """Lookup the images for the given URIs Backends can use this to return image URIs for any URI they know about be it tracks, albums, playlists. The lookup result is a dictionary mapping the provided URIs to lists of images. Unknown URIs or URIs the corresponding backend couldn't find anything for will simply return an empty list for that URI. :param uris: list of URIs to find images for :type uris: list of string :rtype: {uri: tuple of :class:`mopidy.models.Image`} .. versionadded:: 1.0 """ validation.check_uris(uris) futures = { backend: backend.library.get_images(backend_uris) for (backend, backend_uris) in self._get_backends_to_uris(uris).items() if backend_uris } results = {uri: tuple() for uri in uris} for backend, future in futures.items(): with _backend_error_handling(backend): if future.get() is None: continue validation.check_instance(future.get(), Mapping) for uri, images in future.get().items(): if uri not in uris: raise exceptions.ValidationError( f"Got unknown image URI: {uri}") validation.check_instances(images, models.Image) results[uri] += tuple(images) return results
def _check_query_value(key, arg, msg): if not isinstance(arg, compat.string_types) or not arg.strip(): raise exceptions.ValidationError(msg.format(arg=arg, key=key))
def check_instances(arg, cls, msg='Expected a list of {name}, not {arg!r}'): _check_iterable(arg, msg, name=cls.__name__) if not all(isinstance(instance, cls) for instance in arg): raise exceptions.ValidationError(msg.format(arg=arg, name=cls.__name__))
def check_instance(arg, cls, msg='Expected a {name} instance, not {arg!r}'): if not isinstance(arg, cls): raise exceptions.ValidationError(msg.format(arg=arg, name=cls.__name__))
def check_choice(arg, choices, msg='Expected one of {choices}, not {arg!r}'): if arg not in choices: raise exceptions.ValidationError( msg.format(arg=arg, choices=tuple(choices)))
def check_uri(arg, msg='Expected a valid URI, not {arg!r}'): if not isinstance(arg, compat.string_types): raise exceptions.ValidationError(msg.format(arg=arg)) elif urllib.parse.urlparse(arg).scheme == '': raise exceptions.ValidationError(msg.format(arg=arg))
def check_uri(arg, msg="Expected a valid URI, not {arg!r}"): if not isinstance(arg, str): raise exceptions.ValidationError(msg.format(arg=arg)) elif urllib.parse.urlparse(arg).scheme == "": raise exceptions.ValidationError(msg.format(arg=arg))