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
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from collections.abc import Iterable
from enaml.core.include import Include
def _mappedview_helper(model, typemap, kwargs, modelkey):
""" A helper function for the MappedView component.
This helper instantiates the view for the given configuration of
a MappedView component.
"""
for t in type(model).mro():
if t in typemap:
if modelkey:
kwargs = dict(kwargs)
kwargs[modelkey] = model
r = typemap[t](**kwargs)
return list(r) if isinstance(r, Iterable) else [r]
raise TypeError('Unhandled model type `%s`' % type(model).__name__)
enamldef MappedView(Include):
""" A custom Include subtype which will automatically create a
view based on the type of a given model object.
Parameters
----------
model : object
The object acting as the model for this view. The mro of the
type of this object is traversed to find a match in the given
type map. If a match exists, the corresponding view is created.
typemap : dict
A dictionary which maps object type to a callable which returns
a view or iterable of views when invoked.
kwargs : dict, optional
Additional keyword arguments to pass to the matching callable.
The default is an empty dictionary.
modelkey : str, optional
If non-empty, this key will be added to the dict of keyword
arguments passed to a matching callable in the typemap. The
value of the key will be the model instance associated with
this AutoView. The default is 'model'.
"""
attr model
attr typemap: dict
attr kwargs: dict = {}
attr modelkey: str = 'model'
objects << _mappedview_helper(model, typemap, kwargs, modelkey)
|