in lucid/misc/io/loading.py [0:0]
def load(url_or_handle, allow_unsafe_formats=False, cache=None, **kwargs):
"""Load a file.
File format is inferred from url. File retrieval strategy is inferred from
URL. Returned object type is inferred from url extension.
Args:
url_or_handle: a (reachable) URL, or an already open file handle
allow_unsafe_formats: set to True to allow saving unsafe formats (eg. pickles)
cache: whether to attempt caching the resource. Defaults to True only if
the given URL specifies a remote resource.
Raises:
RuntimeError: If file extension or URL is not supported.
"""
# handle lists of URLs in a performant manner
if isinstance(url_or_handle, (list, tuple)):
return _load_urls(url_or_handle, cache=cache, **kwargs)
ext, decompressor_ext = _get_extension(url_or_handle)
try:
ext = ext.lower()
if ext in loaders:
loader = loaders[ext]
elif ext in unsafe_loaders:
if not allow_unsafe_formats:
raise ValueError(f"{ext} is considered unsafe, you must explicitly allow its use by passing allow_unsafe_formats=True")
loader = unsafe_loaders[ext]
else:
raise KeyError(f'no loader found for {ext}')
decompressor = decompressors[decompressor_ext] if decompressor_ext is not None else nullcontext
message = "Using inferred loader '%s' due to passed file extension '%s'."
log.debug(message, loader.__name__[6:], ext)
return load_using_loader(url_or_handle, decompressor, loader, cache, **kwargs)
except KeyError:
log.warning("Unknown extension '%s', attempting to load as image.", ext)
try:
with read_handle(url_or_handle, cache=cache) as handle:
result = _load_img(handle)
except Exception as e:
message = "Could not load resource %s as image. Supported extensions: %s"
log.error(message, url_or_handle, list(loaders))
raise RuntimeError(message.format(url_or_handle, list(loaders)))
else:
log.info("Unknown extension '%s' successfully loaded as image.", ext)
return result