Skip to content

registry

ElementInfo dataclass

ElementInfo(name, module, kind, element=None, description=None)

Useful information associated with an element.

Parameters:

Name Type Description Default
name str

The element's name (class).

required
extra str

The extra package this element can be found in. The empty string represents no extra, or a base element.

required
module str

The module the element can be found in, represented as a string.

required
kind ElementType

The type of element, i.e. source element.

required
element Element | None

If the element is not broken, a reference to this element's class.

None
description str | None

If the element is not broken, the documentation associated with this element.

None

broken property

broken

Whether this element is broken.

plugin cached property

plugin

The plugin associated with this element.

This is tied to the library, where base sgn elements by default are associated with the base plugin.

short_description cached property

short_description

A one line description associated with this element, if not broken.

from_entrypoint classmethod

from_entrypoint(entrypoint)

Create an ElementInfo from a package entry point.

Parameters:

Name Type Description Default
entrypoint EntryPoint

The package entry point associated with an element.

required

Returns:

Type Description
ElementInfo

The element information.

Source code in src/sgn_inspect/registry.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
@classmethod
def from_entrypoint(cls, entrypoint: EntryPoint) -> Self:
    """Create an ElementInfo from a package entry point.

    Parameters
    ----------
    entrypoint : EntryPoint
        The package entry point associated with an element.

    Returns
    -------
    ElementInfo
        The element information.

    """
    module = entrypoint.module
    try:
        element = entrypoint.load()
        kind = ElementType.from_element(element)
    except Exception:
        element = None
        kind = ElementType.INVALID
        description = None
    else:
        description = element.__doc__
    return cls(entrypoint.name, module, kind, element, description)

ElementType

Bases: IntEnum

Used to inform a particular element's type, i.e. source.

INVALID class-attribute instance-attribute

INVALID = auto()

not an element

SINK class-attribute instance-attribute

SINK = auto()

a sink element

SOURCE class-attribute instance-attribute

SOURCE = auto()

a source element

TRANSFORM class-attribute instance-attribute

TRANSFORM = auto()

a transform element

from_element classmethod

from_element(element)

Determine an element's type from an element class.

Parameters:

Name Type Description Default
element type[Element] | Callable[..., Element]

The element class or class method constructor associated with an element class.

required

Returns:

Type Description
ElementType

The element's type.

Source code in src/sgn_inspect/registry.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@typing.no_type_check
@classmethod
def from_element(
    cls, element: type[Element] | Callable[..., Element]
) -> ElementType:
    """Determine an element's type from an element class.

    Parameters
    ----------
    element : type[Element] | Callable[..., Element]
        The element class or class method constructor associated with an
        element class.

    Returns
    -------
    ElementType
        The element's type.

    """
    if inspect.ismethod(element):
        element = element.__self__
    if issubclass(element, SourceElement):
        return ElementType.SOURCE
    if issubclass(element, TransformElement):
        return ElementType.TRANSFORM
    if issubclass(element, SinkElement):
        return ElementType.SINK
    msg = f"{element} is not an element"
    raise TypeError(msg)

discover_elements

discover_elements()

Discover all elements registered via entry points.

Note that non-base elements are only discoverable after installing the corresponding extra package associated with a plugin.

Returns:

Type Description
dict[str, ElementInfo]

A mapping between element names and their information.

Source code in src/sgn_inspect/registry.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def discover_elements() -> dict[str, ElementInfo]:
    """Discover all elements registered via entry points.

    Note that non-base elements are only discoverable after installing the
    corresponding extra package associated with a plugin.

    Returns
    -------
    dict[str, ElementInfo]
        A mapping between element names and their information.

    """
    elements: dict[str, ElementInfo] = {}
    entrypoints = entry_points(group="sgn_elements")
    for name in entrypoints.names:
        elements[name] = ElementInfo.from_entrypoint(entrypoints[name])
    return elements