Skip to content

erdantic.plugins

ModelFieldExtractor

Bases: Protocol[_ModelType_contra]

Protocol class for a field extractor function for a plugin.

Source code in erdantic/plugins/__init__.py
43
44
45
46
class ModelFieldExtractor(Protocol[_ModelType_contra]):
    """Protocol class for a field extractor function for a plugin."""

    def __call__(self, model: _ModelType_contra) -> Sequence["FieldInfo"]: ...

ModelPredicate

Bases: Protocol[_ModelType_co]

Protocol class for a predicate function for a plugin.

Source code in erdantic/plugins/__init__.py
37
38
39
40
class ModelPredicate(Protocol[_ModelType_co]):
    """Protocol class for a predicate function for a plugin."""

    def __call__(self, obj: Any) -> TypeGuard[_ModelType_co]: ...

get_field_extractor_fn

get_field_extractor_fn(key: str) -> ModelFieldExtractor

Get the field extractor function for a plugin by its key.

Source code in erdantic/plugins/__init__.py
85
86
87
88
89
90
def get_field_extractor_fn(key: str) -> ModelFieldExtractor:
    """Get the field extractor function for a plugin by its key."""
    try:
        return _dict[key][1]
    except KeyError:
        raise PluginNotFoundError(key=key)

get_predicate_fn

get_predicate_fn(key: str) -> ModelPredicate

Get the predicate function for a plugin by its key.

Source code in erdantic/plugins/__init__.py
77
78
79
80
81
82
def get_predicate_fn(key: str) -> ModelPredicate:
    """Get the predicate function for a plugin by its key."""
    try:
        return _dict[key][0]
    except KeyError:
        raise PluginNotFoundError(key=key)

identify_field_extractor_fn

identify_field_extractor_fn(
    tp: type,
) -> Optional[ModelFieldExtractor]

Identify the field extractor function for a model type.

Parameters:

Name Type Description Default
tp type

A type annotation.

required

Returns:

Type Description
ModelFieldExtractor | None

The field extractor function for a known model type, or None if the model type is not recognized by any registered plugins.

Source code in erdantic/plugins/__init__.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def identify_field_extractor_fn(tp: type) -> Optional[ModelFieldExtractor]:
    """Identify the field extractor function for a model type.

    Args:
        tp (type): A type annotation.

    Returns:
        ModelFieldExtractor | None: The field extractor function for a known model type, or None if
            the model type is not recognized by any registered plugins.
    """
    for key, (predicate_fn, get_fields_fn) in _dict.items():
        if predicate_fn(tp):
            logger.debug("Identified '%s' as a '%s' model.", typenames(tp), key)
            return get_fields_fn
    logger.debug("'%s' is not a known model type.", typenames(tp))
    return None

list_plugins

list_plugins() -> List[str]

List the keys of all registered plugins.

Source code in erdantic/plugins/__init__.py
72
73
74
def list_plugins() -> List[str]:
    """List the keys of all registered plugins."""
    return list(_dict.keys())

register_plugin

register_plugin(
    key: str,
    predicate_fn: ModelPredicate[_ModelType],
    get_fields_fn: ModelFieldExtractor[_ModelType],
)

Register a plugin for a specific model class type.

Parameters:

Name Type Description Default
key str

An identifier for this plugin.

required
predicate_fn ModelPredicate

A predicate function to determine if an object is a class of the model that is supported by this plugin.

required
get_fields_fn ModelFieldExtractor

A function to extract fields from a model class that is supported by this plugin.

required
Source code in erdantic/plugins/__init__.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def register_plugin(
    key: str,
    predicate_fn: ModelPredicate[_ModelType],
    get_fields_fn: ModelFieldExtractor[_ModelType],
):
    """Register a plugin for a specific model class type.

    Args:
        key (str): An identifier for this plugin.
        predicate_fn (ModelPredicate): A predicate function to determine if an object is a class
            of the model that is supported by this plugin.
        get_fields_fn (ModelFieldExtractor): A function to extract fields from a model class that
            is supported by this plugin.
    """
    logger.debug("Registering plugin '%s'", key)
    if key in _dict:
        logger.warning("Overwriting existing implementation for key '%s'", key)
    _dict[key] = (predicate_fn, get_fields_fn)