redsun.injection¶
Sharing and asking¶
provides
¶
Share a method's return value under the type it is annotated with.
Anything asking for MotorReadings in its setup receives the result
of calling this method on the built component. The return annotation is
the key, so it must be distinct across the application.
DevicesOf
module-attribute
¶
Every device of the session that satisfies P, by name.
class MotorProtocol(Protocol):
async def set(self, value: float) -> None: ...
class MotorPresenter:
def __init__(self, name: str, *, motors: DevicesOf[MotorProtocol]) -> None:
self._motors = motors
Each device of the session, and each device below it, is checked on its
own, so a root device and the devices it holds can all match. Devices are
built before any presenter or view, so the mapping arrives
complete and may be read while the component is built. Ask for
redsun.DeviceMapping instead to receive every device,
unfiltered.
Asking the session¶
satisfies
¶
Whether candidate satisfies protocol.
An instance that does is narrowed to protocol for a type checker; a class is not, since it is not an instance of the protocol it satisfies.
satisfying
¶
Return the components of components that satisfy protocol, by name.
rejected
¶
Return why each component that nearly matched protocol was left out.
Only components carrying some of the protocol's members appear, so a component missing all of them does not drown out a near miss.
The registry¶
Registry
¶
What one session provides, one key to one value.
A registry belongs to one session. A key holds one value, and every lookup
matches its key exactly: a subclass is not answered by a value registered
under its base class, and a union is not answered by a value registered
under one of its members, apart from X | None, which X answers. ask
answers a key nobody registered, each time a provider for it is called.
register
¶
Register factory under key, called on every request.
Raises:
| Type | Description |
|---|---|
RegistrationError
|
If key is registered already, metadata |
register_resource
¶
register_resource(
key: object,
provider: Callable[[], Generator[Any, None, None]],
*,
owner: str,
internal: bool = False,
) -> None
Register the generator provider under key as a resource.
The resource is made on its first request, shared by every request
after it, and torn down by close,
the last made first. An internal key is refused to
call.
Raises:
| Type | Description |
|---|---|
RegistrationError
|
If key is registered already, metadata |
close
¶
Tear down the resources made, the last made first.
A closed registry refuses a request for a resource. Closing twice does nothing.
providers
¶
Yield a provider of key, or none when nothing answers it.
call
¶
Call fn, filling each parameter not in passed from the keys held.
A parameter with a default keeps it when nothing answers. One written
X | None, or a union with None among its members, receives None
then. A None from a held key does not answer a required parameter.
Raises:
| Type | Description |
|---|---|
TypeError
|
If a required parameter has no value, naming each, or a parameter asks for an internal key. |
RuntimeError
|
If the registry is closed and a parameter asks for a resource. |