o
    RhG                     @   sv  d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z&m'Z' dd
l(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZAmBZBmCZCmDZDmEZEmFZFmGZG dZHereedge
d f  ZIeee9e;ge
d f  ZJee/ ZKeeee/eLf   ZMed ZNneZIeZJeZKee ZMeZNedZOedZPedeAdZQG dd deeeRe'e f ef ZSG dd deTZUer/eeeSge	d f  ZVneZVG d d! d!eVZWdS )"    N)partialupdate_wrapper)TYPE_CHECKINGAnyAsyncIterator	AwaitableCallableDictIterableIteratorListMappingMutableMappingOptionalSequenceTupleTypeTypeVarUnioncastoverload)Signal)
FrozenList   )hdrs)AbstractAccessLoggerAbstractMatchInfoAbstractRouterAbstractStreamWriter)DEBUGAppKey)RawRequestMessage)
web_logger)StreamReader)
Middleware)NotAppKeyWarning)AccessLogger)_fix_request_current_app)RequestHandler)Request)StreamResponse)AbstractRouteDef)Server)AbstractResourceAbstractRouteDomain
MaskDomainMatchedSubAppResourcePrefixedSubAppResourceUrlDispatcher)ApplicationCleanupErrorr4   _T_U	_Resource)boundc                       s  e Zd Zeg dZeddddddddejdee	 d	e
e d
eeeef  dedeej deddfddZded  ddfddZerPdededdf fddZdedefddZedee defddZededefddZdeeee f defddZdd d!Zedee d"eddfd#d$Z eded"eddfd%d$Z deeee f d"eddfd&d$Z deeee f ddfd'd(Z!defd)d*Z"de#eeee f  fd+d,Z$eddee d-ddee fd.d/Z%edee d-e&deee&f fd0d/Z%edded-edefd1d/Z%ddeeee f d-edefd2d/Z%e'dejfd3d4Z(deej ddfd5d6Z)e'defd7d8Z*dd9d:Z+e'defd;d<Z,dd=d>Z-e'defd?d@Z.ddBdCZ/dDedAd de0fdEdFZ1dGe2g e3f dAd de3fdHdIZ4dJedAd de5fdKdLZ6dMe
e7 de8e9 fdNdOZ:e'de;fdPdQZ<e'de=fdRdSZ>e'de=fdTdUZ?e'de=fdVdWZ@e'ddYdZZAe'de	fd[d\ZBe'deCfd]d^ZDdeEd_deej d`eeF daedeGfdbdcZHdeEd_deej d`eeF daedeGfdddeZIddfdgZJddhdiZKddjdkZLeMfdleNdmeOdnePdoeQdpdqdreeM deMfdsdtZRde#eSeef  fdudvZTdweMdeUfdxdyZVddzd{ZWdefd|d}ZXdefd~dZY  ZZS )r4   )logger_debug_router_loop_handler_args_middlewares_middlewares_handlers_run_middlewares_state_frozen_pre_frozen_subapps_on_response_prepare_on_startup_on_shutdown_on_cleanup_client_max_size_cleanup_ctxN i   .)r:   routermiddlewareshandler_argsclient_max_sizeloopdebugr:   rM   rN   rO   rP   rQ   rR   returnc                C   s  |d u rt  }ntjdtdd t|tsJ ||d ur%tjdtdd |dur1tjdtdd || _|| _|| _|| _	|| _
t|| _d | _d | _i | _d| _d| _g | _t| | _t| | _t| | _t| | _t | _| j| jj | j| jj || _d S )Nzrouter argument is deprecated   
stacklevelzloop argument is deprecated.zdebug argument is deprecatedF)r3   warningswarnDeprecationWarning
isinstancer   r;   r<   r=   r>   r:   r   r?   r@   rA   rB   rC   rD   rE   r   rF   rG   rH   rI   CleanupContextrK   appendrJ   )selfr:   rM   rN   rO   rP   rQ   rR   rL   rL   Q/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/aiohttp/web_app.py__init__j   sD   





zApplication.__init__clsc                 C   s   t jd| jtdd d S )Nz8Inheritance class {} from web.Application is discouraged   rU   )rW   rX   format__name__rY   )r`   rL   rL   r^   __init_subclass__   s   
zApplication.__init_subclass__namevalc                    s2   || j vrtjd|tdd t || d S )Nz:Setting custom web.Application.{} attribute is discouragedrT   rU   )ATTRSrW   rX   rb   rY   super__setattr__)r]   re   rf   	__class__rL   r^   ri      s   
zApplication.__setattr__otherc                 C   s   | |u S NrL   )r]   rl   rL   rL   r^   __eq__   s   zApplication.__eq__keyc                 C      d S rm   rL   r]   ro   rL   rL   r^   __getitem__      zApplication.__getitem__c                 C   rp   rm   rL   rq   rL   rL   r^   rr      rs   c                 C   s
   | j | S rm   )rB   rq   rL   rL   r^   rr         
c                 C   s   | j rtjdtdd d S d S )Nz=Changing state of started or joined application is deprecatedra   rU   )rC   rW   rX   rY   r]   rL   rL   r^   _check_frozen   s   
zApplication._check_frozenvaluec                 C   rp   rm   rL   r]   ro   rw   rL   rL   r^   __setitem__   rs   zApplication.__setitem__c                 C   rp   rm   rL   rx   rL   rL   r^   ry      rs   c                 C   s0   |    t|tstjdtdd || j|< d S )NzIt is recommended to use web.AppKey instances for keys.
https://docs.aiohttp.org/en/stable/web_advanced.html#application-s-configrT   )categoryrV   )rv   rZ   r    rW   rX   r%   rB   rx   rL   rL   r^   ry      s   
c                 C   s   |    | j|= d S rm   )rv   rB   rq   rL   rL   r^   __delitem__   s   zApplication.__delitem__c                 C   
   t | jS rm   )lenrB   ru   rL   rL   r^   __len__   rt   zApplication.__len__c                 C   r|   rm   )iterrB   ru   rL   rL   r^   __iter__   rt   zApplication.__iter__defaultc                 C   rp   rm   rL   r]   ro   r   rL   rL   r^   get   rs   zApplication.getc                 C   rp   rm   rL   r   rL   rL   r^   r      rs   c                 C   rp   rm   rL   r   rL   rL   r^   r      rs   c                 C   s   | j ||S rm   )rB   r   r   rL   rL   r^   r      s   c                 C   s   t jdtdd ttj| jS )Nzloop property is deprecatedrT   rU   )rW   rX   rY   r   asyncioAbstractEventLoopr=   ru   rL   rL   r^   rQ      s   zApplication.loopc                 C   s`   |d u rt  }| jd ur| j|urtd|| _| jdu r#| | _| jD ]}|| q&d S )Nz8web.Application instance initialized with different loop.)r   get_event_loopr=   RuntimeErrorr;   	get_debugrE   	_set_loop)r]   rQ   subapprL   rL   r^   r      s   


zApplication._set_loopc                 C      | j S rm   )rD   ru   rL   rL   r^   
pre_frozen     zApplication.pre_frozenc                 C   s   | j rd S d| _ | j  | j  | j  | j  | j  | j  | j  t	| 
 | _| jr7dnd| _| jD ]}|  | jpH|j| _q=d S )NTF)rD   r?   freezer<   rF   rK   rG   rH   rI   tuple_prepare_middlewarer@   rN   rA   rE   
pre_freezer]   r   rL   rL   r^   r     s    







zApplication.pre_freezec                 C   r   rm   )rC   ru   rL   rL   r^   frozen+  r   zApplication.frozenc                 C   s0   | j rd S |   d| _ | jD ]}|  qd S NT)rC   r   rE   r   r   rL   rL   r^   r   /  s   

zApplication.freezec                 C   s   t jdtdd | jS )Nzdebug property is deprecatedrT   rU   )rW   rX   rY   r;   ru   rL   rL   r^   rR   8  s   zApplication.debugr   c                    s4   dt dd f fdd}|d |d |d d S )NsignamerS   c                    s2   t |  d fdd}t | }|| d S )Nappr4   rS   c                    s     I d H  d S rm   )send)r   )r   subsigrL   r^   handlerA  s   zEApplication._reg_subapp_signals.<locals>.reg_handler.<locals>.handler)r   r4   rS   N)getattrr\   )r   r   appsigr   )r   r^   reg_handler>  s   

z4Application._reg_subapp_signals.<locals>.reg_handler
on_startupon_shutdown
on_cleanup)str)r]   r   r   rL   r   r^   _reg_subapp_signals=  s   	zApplication._reg_subapp_signalsprefixc                 C   s@   t |ts	td|d}|stdtt||}| ||S )NzPrefix must be str/zPrefix cannot be empty)rZ   r   	TypeErrorrstrip
ValueErrorr   r2   _add_subapp)r]   r   r   factoryrL   rL   r^   
add_subappK  s   

zApplication.add_subappresource_factoryc                 C   sf   | j rtd|j rtd| }| j| | | | j| |  | jd ur1|	| j |S )Nz0Cannot add sub application to frozen applicationzCannot add frozen application)
r   r   rM   register_resourcer   rE   r\   r   r=   r   )r]   r   r   resourcerL   rL   r^   r   T  s   

zApplication._add_subappdomainc                 C   sD   t |ts	tdd|v rt|}nt|}tt||}| ||S )NzDomain must be str*)rZ   r   r   r0   r/   r   r1   r   )r]   r   r   ruler   rL   rL   r^   
add_domaind  s   

zApplication.add_domainroutesc                 C   s   | j |S rm   )rM   
add_routes)r]   r   rL   rL   r^   r   n  s   zApplication.add_routesc                 C   r   rm   )rF   ru   rL   rL   r^   on_response_prepareq  r   zApplication.on_response_preparec                 C   r   rm   )rG   ru   rL   rL   r^   r   u  r   zApplication.on_startupc                 C   r   rm   )rH   ru   rL   rL   r^   r   y  r   zApplication.on_shutdownc                 C   r   rm   )rI   ru   rL   rL   r^   r   }  r   zApplication.on_cleanupr[   c                 C   r   rm   )rK   ru   rL   rL   r^   cleanup_ctx  r   zApplication.cleanup_ctxc                 C   r   rm   )r<   ru   rL   rL   r^   rM     r   zApplication.routerc                 C   r   rm   )r?   ru   rL   rL   r^   rN     r   zApplication.middlewaresrQ   access_log_classr   kwargsc                K   sz   t |tstd|| | |   | j|d< ||d< | jr/| j D ]\}}|||< q&t	| j
f| j| jd|S )NzMaccess_log_class must be subclass of aiohttp.abc.AbstractAccessLogger, got {}rR   r   )request_factoryrQ   )
issubclassr   r   rb   r   r   r;   r>   itemsr,   _handle_make_requestr=   )r]   rQ   r   r   kvrL   rL   r^   _make_handler  s(   



zApplication._make_handlerc                K   s&   t jdtdd | jd||d|S )NzFApplication.make_handler(...) is deprecated, use AppRunner API insteadrT   rU   r   rL   )rW   rX   rY   r   )r]   rQ   r   r   rL   rL   r^   make_handler  s   zApplication.make_handlerc                       | j | I dH  dS )zmCauses on_startup signal

        Should be called in the event loop along with the request handler.
        N)r   r   ru   rL   rL   r^   startup     zApplication.startupc                    r   )zMCauses on_shutdown signal

        Should be called before cleanup()
        N)r   r   ru   rL   rL   r^   shutdown  r   zApplication.shutdownc                    s6   | j jr| j | I dH  dS | j| I dH  dS )zLCauses on_cleanup signal

        Should be called after shutdown()
        N)r   r   r   rK   rI   ru   rL   rL   r^   cleanup  s   zApplication.cleanupmessagepayloadprotocolwritertaskzasyncio.Task[None]_clsc              	   C   s   ||||||| j | jdS )N)rP   )r=   rJ   )r]   r   r   r   r   r   r   rL   rL   r^   r     s   	zApplication._make_requestc                 c   s`    t | jD ] }t|dd dkr|dfV  qtjd|tdd |dfV  qt| dfV  d S )N__middleware_version__r   Tz1old-style middleware "{!r}" deprecated, see #2252rT   rU   F)reversedr?   r   rW   rX   rb   rY   r'   )r]   mrL   rL   r^   r     s   zApplication._prepare_middlewarerequestc                    s  t  }| }| j|I d H }|r t|ts td||	|  |
  d }||_|jtj}|rG||I d H }|j I d H  |d u r|j}| jrx|jd d d D ]}|jD ]\}	}
|
rntt|	|d|}q^|	||I d H }q^qY||I d H }|S )Nz9match_info should be AbstractMatchInfo instance, not {!r})r   )r   r   r   r<   resolverZ   r   r   rb   add_appr   _match_infoheadersr   r   EXPECTexpect_handlerr   drainr   rA   appsr@   r   r   )r]   r   rQ   rR   
match_inforespexpectr   r   r   	new_stylerL   rL   r^   r     s>   

zApplication._handlec                 C   s   | S )zgunicorn compatibilityrL   ru   rL   rL   r^   __call__  s   zApplication.__call__c                 C   s   dt | ddS )Nz<Application 0xx>)idru   rL   rL   r^   __repr__!  s   zApplication.__repr__c                 C   s   dS r   rL   ru   rL   rL   r^   __bool__$  rs   zApplication.__bool__rS   N).rm   )r   r4   rS   N)rS   r[   )rS   r4   )[rc   
__module____qualname__	frozensetrg   r"   loggingLoggerr   r3   r
   r$   r   r   r   intr   r   r_   r   rd   r   ri   objectboolrn   r   r    r6   rr   r   rv   ry   r{   r~   r   r   r   r7   propertyrQ   r   r   r   r   r   rR   r   r2   r   r   r8   r   r1   r   r+   r   r.   r   _RespPrepareSignalr   
_AppSignalr   r   r   r   rM   _MiddlewaresrN   r&   r   r,   r   r   r   r   r   r)   r!   r#   r(   r   r   r   r   r*   r   r   r   r   __classcell__rL   rL   rj   r^   r4   R   s   	

7
""$$

	
	



!





&c                   @   s"   e Zd Zedee fddZdS )r5   rS   c                 C   s   t tt | jd S )Nr   )r   r   BaseExceptionargsru   rL   rL   r^   
exceptions)  s   zCleanupError.exceptionsN)rc   r   r   r   r   r   r   rL   rL   rL   r^   r5   (  s    r5   c                       sB   e Zd Zd
 fddZdeddfddZdeddfdd	Z  ZS )r[   rS   Nc                    s   t    g | _d S rm   )rh   r_   _exitsru   rj   rL   r^   r_   5  s   

zCleanupContext.__init__r   c                    s6   | D ]}||  }| I d H  | j| qd S rm   )	__aiter__	__anext__r   r\   )r]   r   cbitrL   rL   r^   rG   9  s   zCleanupContext._on_startupc                    s   g }t | jD ]5}z	| I d H  W n ty   Y q ty2 } z|| W Y d }~qd }~ww |t|d q|rOt|dkrJ|d td|d S )Nz has more than one 'yield'r   r   z Multiple errors on cleanup stage)	r   r   r   StopAsyncIteration	Exceptionr\   r   r}   r5   )r]   r   errorsr   excrL   rL   r^   rI   ?  s"   
zCleanupContext._on_cleanupr   )rc   r   r   r_   r4   rG   rI   r   rL   rL   rj   r^   r[   4  s    r[   )Xr   r   rW   	functoolsr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   	aiosignalr   
frozenlistr    r   abcr   r   r   r   helpersr   r    http_parserr!   logr"   streamsr#   typedefsr$   web_exceptionsr%   web_logr&   web_middlewaresr'   web_protocolr(   web_requestr)   web_responser*   web_routedefr+   
web_serverr,   web_urldispatcherr-   r.   r/   r0   r1   r2   r3   __all__r   r   r   r   _MiddlewaresHandlers_Subappsr6   r7   r8   r   r4   r   r5   _CleanupContextBaser[   rL   rL   rL   r^   <module>   s^    T$

$   Y