Created
May 10, 2024 20:18
Build Adapter from DataSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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