# -*- coding: utf-8 -*-# Copyright (c) 2025-present tandemdude## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE."""Extension module adding support for using linkd-based dependency injection with `Quart <https://quart.palletsprojects.com/en/latest/>`_.See the examples directory for a full working application using this module... note:: Unlike the ``FastAPI`` and ``Starlette`` integrations, the default :meth:`~linkd.solver.inject` method will work for route handlers when using this extension.----"""from__future__importannotations__all__=["Contexts","DiContextMiddleware"]importtypingastfromlinkdimportcontextas_contextfromlinkd.extimport_commonift.TYPE_CHECKING:fromcollections.abcimportAwaitablefromcollections.abcimportCallablefromhypercorn.typingimportASGIReceiveCallablefromhypercorn.typingimportASGISendCallablefromhypercorn.typingimportScopefromlinkdimportsolverASGIApp=Callable[[Scope,ASGIReceiveCallable,ASGISendCallable],Awaitable[None]]
[docs]@t.finalclassContexts:"""Collection of the dependency injection context values linkd.ext.quart uses."""__slots__=()ROOT=_context.Contexts.ROOT"""The root DI context - ALL other contexts are built with this as the parent."""REQUEST=_common.REQUEST_CONTEXT"""DI context used during HTTP request handling."""
[docs]classDiContextMiddleware:""" Middleware class which handles setting up a DI context for each HTTP request. Args: app: The ASGI app this middleware will be applied to. manager: The dependency injection manager to use when entering the DI context. Example: .. code-block:: python import quart import linkd manager = linkd.DependencyInjectionManager() app = quart.Quart(__name__) app.asgi_app = linkd.ext.quart.DiContextMiddleware(app.asgi_app, manager) """__slots__=("app","manager")def__init__(self,app:ASGIApp,manager:solver.DependencyInjectionManager)->None:self.app:ASGIApp=appself.manager:solver.DependencyInjectionManager=managerasyncdef__call__(self,scope:Scope,receive:ASGIReceiveCallable,send:ASGISendCallable)->None:ifscope["type"]!="http":awaitself.app(scope,receive,send)returnasyncwithself.manager.enter_context(Contexts.ROOT),self.manager.enter_context(Contexts.REQUEST):awaitself.app(scope,receive,send)