Skip to content

Instantly share code, notes, and snippets.

@danielballan
Created May 10, 2024 20:18
Show Gist options
  • Save danielballan/c51dcdd4b72dd8e4e9693bcbd6e39a83 to your computer and use it in GitHub Desktop.
Save danielballan/c51dcdd4b72dd8e4e9693bcbd6e39a83 to your computer and use it in GitHub Desktop.
Build Adapter from DataSource
import collections
from tiled.mimetypes import DEFAULT_ADAPTERS_BY_MIMETYPE
def build_adapter(data_source, adapters_by_mimetype=None):
# We use a dictionary mapping a mimetype to a callable that returns an Adapter instance.
# This might be a class, classmethod constructor, factory function...
# it does not matter here; it is just a callable.
# User-provided adapters take precedence over defaults.
merged_adapters_by_mimetype = collections.ChainMap(
(adapters_by_mimetype or {}),
DEFAULT_ADAPTERS_BY_MIMETYPE,
)
adapter_factory = merged_adapters_by_mimetype[data_source.mimetype]
# Construct kwargs to pass to Adapter.
parameters = collections.defaultdict(list)
for asset in data_source.assets:
if asset.parameter is None:
# This asset is not directly opened by the Adapter.
# It is used indirectly, such as the case of HDF5 virtual dataset
# 'data' files are referenced from 'master' files.
continue
if asset.num is None:
# This parameters takes the URI as a scalar value.
parameters[asset.parameter] = asset.data_uri
else:
# This parameters takes a list of URIs.
parameters[asset.parameter].append(asset.data_uri)
adapter_kwargs = dict(parameters)
adapter_kwargs.update(data_source.parameters)
adapter = adapter_factory(**adapter_kwargs)
return adapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment