Debian Codegraph @

pyvista/core/utilities/_registry_helpers.py

raw
1 """Internal helpers shared by PyVista's plugin registries.
2
3 The registries (accessor, reader, writer, interactor style, jupyter
4 backend) all need to record where each registration came from so a user
5 inspecting :func:`~pyvista.registered_readers` and friends can tell
6 explicit code apart from entry-point discovery. The helpers here keep
7 that source-attribution logic in one place.
8 """
9
10 from __future__ import annotations
11
12
13 def handler_source(handler: object) -> str:
14 """Return ``module.qualname`` for *handler* when available.
15
16 Used by every plugin registry to attach a human-readable origin
17 string to each explicit registration so that :func:`registered_*`
18 introspection can show where a handler came from.
19
20 Parameters
21 ----------
22 handler : object
23 Any callable or class; typically a function, method, or type
24 object whose ``__module__`` and ``__qualname__`` attributes are
25 meaningful.
26
27 Returns
28 -------
29 str
30 ``f'{handler.__module__}.{handler.__qualname__}'`` when both
31 attributes are present, falling back to ``'<unknown>'`` for the
32 module piece and ``repr(handler)`` for the qualname piece when
33 not.
34
35 """
36 module = getattr(handler, '__module__', None) or '<unknown>'
37 qualname = getattr(handler, '__qualname__', None) or repr(handler)
38 return f'{module}.{qualname}'