
    Mf                         d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 d Z
d Zd	 Z G d
 de          Z G d de          ZdS )a  
Define pipe consumer and also http endpoint for direct injection of pipe messages

Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md

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.
    N)WebsocketConsumer)AsyncHttpConsumer)get_channel_layer)async_to_syncc                     d| z  S )Nzdpd_pipe_%s )channel_names    [/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django_plotly_dash/consumers.py_form_pipe_channel_namer       s    <''    c                 F     t          t                    | ||           dS )z-Send message through pipe to client componentr	   labelvalueN)r   async_send_to_pipe_channelr   s      r
   send_to_pipe_channelr   #   s8     .M,--<4949; ; ; ; ; ;r   c                    K   t          |           }t                      }|                    |d||d           d{V  dS )z<Send message asynchronously through pipe to client componentz
pipe.value)typer   r   N)r   r   
group_send)r	   r   r   pcnchannel_layers        r
   r   r   +   su       ",
/
/C%''M

"
"3+7,1,1$3 $34 4 4 4 4 4 4 4 4 4 4r   c                   D     e Zd ZdZ fdZd Z fdZd Zd Zd Z	 xZ
S )MessageConsumerz,Websocket handler for pipe to dash componentc                 H     t                      j        |i | i | _        d S N)super__init__channel_maps)selfargskwargs	__class__s      r
   r   zMessageConsumer.__init__9   s-    $)&)))r   c                 .    |                                   d S r   )accept)r   s    r
   connectzMessageConsumer.connect>   s    r   c                     | j                                         D ]-\  }} t          | j        j                  || j                   .t                                          |          S r   )r   itemsr   r   group_discardr	   r   
disconnect)r   reason_pipe_group_namer"   s       r
   r)   zMessageConsumer.disconnectA   sf    "&"3"9"9";"; 	` 	`A;M$,:;;OTM^____ww!!&)))r   c                 X    t          j        |          }|                     |           dS )z!Send a new value into the ws pipeN)jsondumpssend)r   messagejmsgs      r
   
pipe_valuezMessageConsumer.pipe_valueH   s$    z'""		$r   c                 4   t          |          }| j        r| j                            |d          }||k    r`|r( t	          | j        j                  || j                   || j        |<    t	          | j        j                  || j                   dS dS dS )zf
        Update this consumer to listen on channel_name for the js widget associated with uid
        N)r   r   r   getr   r(   r	   	group_add)r   uidr	   r   r,   currents         r
   update_pipe_channelz#MessageConsumer.update_pipe_channelM   s     2,?? 	`'++C66G/)) `CM$"4"BCCGTM^___)8!#&;d0:;;OTM^_____	` 	`))r   c                    t          j        |          }|                    dd          }|dk    r:	 |d         }|d         }|d         }|                     |||           d S #  Y d S xY w|                    dd          }|                    dd           }|                    dd	          }t	          |||
           d S )Nr   unknown_typeconnection_tripletr	   r7   r   UNNAMED_CHANNELr   DEFAULT$LABELr   )r.   loadsr5   r9   r   )r   	text_datar1   message_typer	   r7   r   r   s           r
   receivezMessageConsumer.receive\   s    *Y''{{6>::///
&~6en(((lEBBBBB  #;;~7HIILKK..EKK99E l',',. . . . . .s   /A# #A()__name__
__module____qualname____doc__r   r%   r)   r3   r9   rB   __classcell__)r"   s   @r
   r   r   7   s        22    
  * * * * *  
` ` `. . . . . . .r   r   c                       e Zd ZdZd ZdS )PokePipeConsumerz5Async handling of http request inserting pipe messagec                   K   | j                             dd           }|                    d          }	 t          j        |          }|                    dd          }|                    dd           }|                    dd          }t          |||           d {V  d|d	|d
}d}	n#  d|z  }d}	Y nxY w|                     |	|                    d                     d {V  d S )Nuserzutf-8r	   r=   r   r   r>   z%PokePipeConsumer consumed message of z for 
   z2Unable to understand and forward on message of %s
i  )scoper5   decoder.   r?   r   send_responseencode)
r   bodyrK   as_utfincoming_messager	   r   r   responseresponse_codes
             r
   handlezPokePipeConsumer.handle~   sS     z~~fd++W%%	 #z&11 ,//@QRRL$(($77E$((/BBE -\-2-24 4 4 4 4 4 4 4 4 4
 HMM	 HMMM  !)!9!9; ; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   A8B- -	B8N)rC   rD   rE   rF   rW   r   r   r
   rI   rI   {   s)        ;;; ; ; ; ;r   rI   )rF   r.   channels.generic.websocketr   channels.generic.httpr   channels.layersr   asgiref.syncr   r   r   r   r   rI   r   r   r
   <module>r\      s    0  8 8 8 8 8 8 3 3 3 3 3 3 - - - - - - & & & & & &( ( (; ; ;
4 
4 
4B. B. B. B. B.' B. B. B.H; ; ; ; ;( ; ; ; ; ;r   