o
    >h                     @   s~   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	d
 ZG dd dZG dd deZdS )    N)async_to_sync   )DEFAULT_CHANNEL_LAYER)aclose_old_connectionsdatabase_sync_to_async)StopConsumer)get_channel_layer)await_many_dispatchc                 C   s6   d| vrt d| d dd}|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_name r   q/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/channels/consumer.pyget_handler_name   s   
r   c                   @   s<   e Zd ZdZdZeZdd Zdd Zdd Z	e
d	d
 ZdS )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                    s   || _ t| j| _| jdur"| j I dH | _t| jj| j| _	| j
r+t|| _n|| _z!| jdurCt|| j	g| jI dH  W dS t|g| jI dH  W dS  tyY   Y dS 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   sendr   r   r   __call__%   s(   


zAsyncConsumer.__call__c                    sD   t | t|d}|rt I dH  ||I dH  dS td|d  )z6
        Works out what to do with a message.
        NNo handler for message type %sr
   )getattrr   r   r   r#   r   handlerr   r   r   r"   C   s   zAsyncConsumer.dispatchc                    s   |  |I dH  dS zB
        Overrideable/callable-by-subclasses send method.
        Nr!   r#   r   r   r   r   r$   N   s   zAsyncConsumer.sendc                    s.    fdd} |_ |_tj| d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                    s"    di }|| ||I d H S )Nr   r   )r   r   r$   consumercls
initkwargsr   r   app]   s   z"AsyncConsumer.as_asgi.<locals>.appr   )updated)consumer_classconsumer_initkwargsr   update_wrapper)r/   r0   r1   r   r.   r   as_asgiT   s
   	zAsyncConsumer.as_asgiN)__name__
__module____qualname____doc__r    r   r   r%   r"   r$   classmethodr6   r   r   r   r   r      s    r   c                   @   s(   e Zd ZdZdZedd Zdd ZdS )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                 C   s0   t | t|d}|r|| dS td|d  )r   Nr&   r
   )r'   r   r   r(   r   r   r   r"   v   s   zSyncConsumer.dispatchc                 C   s   |  | dS r*   r+   r,   r   r   r   r$      s   zSyncConsumer.sendN)r7   r8   r9   r:   r    r   r"   r$   r   r   r   r   r<   i   s    

r<   )r   asgiref.syncr    r   dbr   r   
exceptionsr   layersr   utilsr	   r   r   r<   r   r   r   r   <module>   s    N