
    h                         d dl Z d dlmZ ddl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	 Z G d
 d      Z G d de      Zy)    N)async_to_sync   )DEFAULT_CHANNEL_LAYER)aclose_old_connectionsdatabase_sync_to_async)StopConsumer)get_channel_layer)await_many_dispatchc                     d| vrt        d      | d   j                  dd      }|j                  d      rt        d      |S )zl
    Looks at a message, checks it has a sensible type, and returns the
    handler name for that type.
    typez(Incoming message has no 'type' attribute._z.Malformed type in message (leading underscore))
ValueErrorreplace
startswith)messagehandler_names     M/var/www/Befach/backend/env/lib/python3.12/site-packages/channels/consumer.pyget_handler_namer      sN     WCDD6?**34Ls#IJJ    c                   :    e Zd ZdZdZeZd Zd Zd Z	e
d        Zy)AsyncConsumerz
    Base consumer class. Implements the ASGI application spec, and adds on
    channel layer management and routing of events to named methods based
    on their type.
    Fc                 F  K   || _         t        | j                        | _        | j                  `| j                  j	                          d{   | _        t        j                  | j                  j                  | j
                        | _	        | j                  rt        |      | _        n|| _        	 | j                  +t        || j                  g| j                         d{    yt        |g| j                         d{    y7 7 '7 	# t        $ r Y yw xY ww)U
        Dispatches incoming messages to type-based handlers asynchronously.
        N)scoper	   channel_layer_aliaschannel_layernew_channelchannel_name	functoolspartialreceivechannel_receive_syncr   	base_sendr
   dispatchr   )selfr   r"   sends       r   __call__zAsyncConsumer.__call__%   s     
 /t/G/GH)&*&8&8&D&D&F FD#,#4#4""**D,=,=$D  ::*40DN!DN		!!-)d223T]]   *7)T]]CCC! !G D 		sg   AD!DA&D!51D &D'D +D!,D DD D!D D 	DD!DD!c                    K   t        | t        |      d      }|r#t                d{     ||       d{    yt        d|d   z        7 &7 w)z6
        Works out what to do with a message.
        NNo handler for message type %sr   )getattrr   r   r   r'   r   handlers      r   r&   zAsyncConsumer.dispatchC   sW      $ 0 94@(***'"""=OPP +"s   &AAAAAAc                 B   K   | j                  |       d{    y7 wzB
        Overrideable/callable-by-subclasses send method.
        Nr%   r'   r   s     r   r(   zAsyncConsumer.sendN   s      nnW%%%s   c                 b      fd} |_         |_        t        j                  | d       |S )z
        Return an ASGI v3 single callable that instantiates a consumer instance
        per scope. Similar in purpose to Django's as_view().

        initkwargs will be used to instantiate the consumer instance.
        c                 D   K    di } || ||       d {   S 7 w)N r5   )r   r"   r(   consumercls
initkwargss       r   appz"AsyncConsumer.as_asgi.<locals>.app]   s)     (Z(H!%$7777s     r5   )updated)consumer_classconsumer_initkwargsr    update_wrapper)r7   r8   r9   s   `` r   as_asgizAsyncConsumer.as_asgiT   s3    	8 !", 	  c26
r   N)__name__
__module____qualname____doc__r$   r   r   r)   r&   r(   classmethodr>   r5   r   r   r   r      s8     E/<	Q&  r   r   c                   *    e Zd ZdZdZed        Zd Zy)SyncConsumera  
    Synchronous version of the consumer, which is what we write most of the
    generic consumers against (for now). Calls handlers in a threadpool and
    uses CallBouncer to get the send method out to the main event loop.

    It would have been possible to have "mixed" consumers and auto-detect
    if a handler was awaitable or not, but that would have made the API
    for user-called methods very confusing as there'd be two types of each.
    Tc                 f    t        | t        |      d      }|r	 ||       yt        d|d   z        )r   Nr+   r   )r,   r   r   r-   s      r   r&   zSyncConsumer.dispatchv   s8     $ 0 94@G=OPPr   c                 &    | j                  |       yr0   r1   r2   s     r   r(   zSyncConsumer.send   s     	wr   N)r?   r@   rA   rB   r$   r   r&   r(   r5   r   r   rE   rE   i   s'     E	Q 	Q r   rE   )r    asgiref.syncr    r   dbr   r   
exceptionsr   layersr	   utilsr
   r   r   rE   r5   r   r   <module>rN      s8     & # > $ % &K K\ =  r   