Exemplo n.º 1
0
    async def _asgi_lifespan(self, receive: Receive, send: Send) -> None:
        # search through the provided classes for something with a handler
        # method and register that
        for name, provided in self._env.items():
            if isimplementation(type(provided), EventHandler):
                if self._asgi_handler is not None:
                    # TODO log a better warning
                    print("there already is an _asgi_handler. not replacing it.")
                    continue

                self._asgi_handler = provided

        if self._asgi_handler is None:
            raise Exception  # TODO Write an actual exception

        while True:
            msg = await receive()

            if msg.get("type") == "lifespan.startup":
                self.build()
                interrupt = True
                interrupt = interrupt and await self._on_start()

                status = "lifespan.startup.failed" if interrupt else "lifespan.startup.complete"
                await send({"type": status})

                return

            if msg.get("type") == "lifespan.shutdown":
                await self._on_stop()
                await send({"type": "lifespan.shutdown.complete"})
                self._loop.close()
                return
Exemplo n.º 2
0
    def _search_protocol(self, dep: Any) -> Optional[str]:
        """
        `search_protocol` attempts to match a Protocol definition to an object
        provided to the Harness. If the required Protocol is that of `jab.Logger`
        and no suitable Logger has been provided, the `DefaultJabLogger` stored in
        `_logger` will be provided.

        Parameters
        ----------
        dep : Any
            The protocol that some object must implement.

        Returns
        -------
        Optional[str]
            If an object can be found that implements the provided Protocol, its key-value
            is returned, otherwise None is returned.
        """
        for name, obj in self._provided.items():
            if isfunction(obj):
                obj = get_type_hints(obj)["return"]

            if isimplementation(obj, dep):
                return name

        if dep is Logger:
            self._env[DEFAULT_LOGGER] = self._logger
            return DEFAULT_LOGGER

        return None
Exemplo n.º 3
0
def test_impl_with_proto_return(protocol, impl_with_proto):
    assert isimplementation(impl_with_proto, protocol)
Exemplo n.º 4
0
def test_protocol_returning_protocol(protocol, impl):
    assert isimplementation(impl, protocol)
Exemplo n.º 5
0
def test_bad_overloaded(bad_overloaded):
    Overloaded, Names = bad_overloaded

    with pytest.raises(ReturnedUnionType):
        isimplementation(Overloaded, Names)