o
    iF                     @   s:  d Z ddlmZ ddlZeeZddlmZ ddl	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 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd deZG dd deZG dd deZG dd de
Z G dd de!Z"dd Z#G dd de!Z$G dd de%Z&G dd  d eZ'dS )!a  The ``spyne.server.twisted`` module contains a server transport compatible
with the Twisted event loop. It uses the TwistedWebResource object as transport.

Also see the twisted examples in the examples directory of the source
distribution.

If you want to have a hard-coded URL in the wsdl document, this is how to do
it: ::

    resource = TwistedWebResource(...)
    resource.http_transport.doc.wsdl11.build_interface_document("http://example.com")

This is not strictly necessary -- if you don't do this, Spyne will get the
URL from the first request, build the wsdl on-the-fly and cache it as a
string in memory for later requests. However, if you want to make sure
you only have this url on the WSDL, this is how to do it. Note that if
your client takes the information in wsdl seriously, all requests will go
to the designated url above which can make testing a bit difficult. Use
in moderation.

This module is EXPERIMENTAL. Your mileage may vary. Patches are welcome.
    )absolute_importN)Deferred)Factory)WebSocketsProtocol)WebSocketsResource)CONTROLS)MethodContextTransportContextAddress)process_contexts)PushBaseComplexModel)Fault)
ServerBasec                   @   s   e Zd Zdd Zdd ZdS )WebSocketTransportContextc                 C   s"   t | ||| || _	 || _d S N)r	   __init__client_handleparent)selfr   	transporttyper    r   ^/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/server/twisted/websocket.pyr   G   s
   z"WebSocketTransportContext.__init__c                 C   s$   | j d ur| j j }t|S d S r   )r   r   getPeerr
   from_twisted_address)r   peerr   r   r   get_peerP   s   

z"WebSocketTransportContext.get_peerN)__name__
__module____qualname__r   r   r   r   r   r   r   F   s    	r   c                   @      e Zd Zdd ZdS )WebSocketMethodContextc                 C   s$   t | |t j t| |d|| _d S )Nws)r   r   SERVERr   r   )r   r   r   r   r   r   r   W   s   
zWebSocketMethodContext.__init__Nr   r    r!   r   r   r   r   r   r#   V       r#   c                   @   sL   e Zd ZdZdddZedd Zejdd Zd	d
 Zdd Z	dd Z
dS )TwistedWebSocketProtocolzDA protocol that parses and generates messages in a WebSocket stream.FNc                 C   s2   || _ || _t| | _|r| j| _| j| _d S d S r   )_spyne_transport_clientsid!_TwistedWebSocketProtocol__app_id_connectionMadeconnectionMade_connectionLostconnectionLost)r   r   bookkeepr*   r   r   r   r   a   s   
z!TwistedWebSocketProtocol.__init__c                 C   s   | j S r   )r,   r   r   r   r   app_idi   s   zTwistedWebSocketProtocol.app_idc                 C   s2   | j | jd }|r| j | j= || j |< || _d S r   )r*   getr,   )r   whatentryr   r   r   r3   m   s
   


c                 C   s   t |  | | j| j< d S r   )r   r.   r*   r3   r2   r   r   r   r-   w   s   
z(TwistedWebSocketProtocol._connectionMadec                 C   s   | j t| = d S r   )r*   r+   )r   reasonr   r   r   r/   |   s   z(TwistedWebSocketProtocol._connectionLostc           	         s  j td}|g|_|}|d |dd  jr%j_n jr2j_n jr>j_d fdd	} fdd}j}t	|t
tfrb|d }t	|trs|| || d S t	|tr{t |jd	d
 d S )N)r   r      Tc                    s^   |rt jjjdkr| g_n| _ dj  	  t
 d S )Nr8    )len
descriptorout_message
_type_info
out_objectget_out_string	sendFramejoin
out_stringcloser   )retvalcbfinopcodeothersp_ctxr   tptr   r   _cb_deferred   s   

z<TwistedWebSocketProtocol.frameReceived.<locals>._cb_deferredc                    sN   | j _t| jtst|    	d
j    d S Nr9   )value	out_error
issubclassr   r   loggererrorgetTracebackr?   r@   rA   rB   rC   )err)rG   rH   rJ   r   rK   r   r   _eb_deferred   s   
z<TwistedWebSocketProtocol.frameReceived.<locals>._eb_deferredF)rE   )T)r)   r#   	in_stringgenerate_contextsin_errorr>   get_in_objectget_out_objectrO   
isinstancelisttupler   addCallback
addErrbackr   NotImplementedError)	r   rH   datarG   initial_ctxcontextsrL   rU   retr   rF   r   frameReceived   s0   




	


z&TwistedWebSocketProtocol.frameReceivedFN)r   r    r!   __doc__r   propertyr3   setterr-   r/   re   r   r   r   r   r(   ^   s    


	r(   c                   @   s   e Zd ZdddZdd ZdS )TwistedWebSocketFactoryFNc                 C   s2   || _ t|| _|| _|| _|d u ri | _d S d S r   )appr   r   r1   r*   )r   rk   r1   r*   r   r   r   r      s   

z TwistedWebSocketFactory.__init__c                 C   s   t | j| j| jS r   )r(   r   r1   r*   )r   addrr   r   r   buildProtocol   s   
z%TwistedWebSocketFactory.buildProtocolrf   )r   r    r!   r   rm   r   r   r   r   rj      s    
rj   c                   @      e Zd ZdS )_FakeNr   r    r!   r   r   r   r   ro          ro   c                    s   G  fdddt }|S )Nc                       s   e Zd Zd iZdS )z_FakeWrap.<locals>._Retzugh N)r   r    r!   r=   r   clsr   r   _Ret   r'   rt   r   )rs   rt   r   rr   r   	_FakeWrap   s   ru   c                   @   r"   )_FakeCtxc                 C   s    || _ d | _t | _|| j_d S r   )r>   rO   ro   r;   r<   )r   objrs   r   r   r   r      s   z_FakeCtx.__init__Nr&   r   r   r   r   rv      r'   rv   c                   @   rn   )InvalidRequestErrorNrp   r   r   r   r   rx      rq   rx   c                   @   s2   e Zd ZdddZdd ZdddZdd	d
ZdS )TwistedWebSocketResourceFNc                 C   s@   || _ || _|d u ri | _|r| j| _t| t||| j d S r   )rk   clientsdo_propagate	propagater   r   rj   )r   rk   r1   rz   r   r   r   r      s   

z!TwistedWebSocketResource.__init__c                 C   s   t d)Nz=You must enable bookkeeping to have message propagation work.)rx   r2   r   r   r   r|      s   z"TwistedWebSocketResource.propagatec                 C   sD   |d u r|j }| jj}t||}|||j || d|jS rM   )		__class__rk   out_protocolrv   	serializeRESPONSEcreate_out_stringrA   rB   )r   rw   rs   opctxr   r   r   get_doc   s   

z TwistedWebSocketResource.get_docc                 C   s:   |  ||}| j D ]}td| |tj|d qd S )Nz
sending toT)r   rz   
itervaluesprintr@   r   TEXT)r   rw   rs   doccr   r   r   r{      s
   
z%TwistedWebSocketResource.do_propagaterf   r   )r   r    r!   r   r|   r   r{   r   r   r   r   ry      s
    

ry   )(rg   
__future__r   logging	getLoggerr   rQ   twisted.internet.deferr   twisted.internet.protocolr   spyne.util._twisted_wsr   r   r   spyner   r	   r
   spyne.auxprocr   spyne.modelr   spyne.model.complexr   spyne.model.faultr   spyne.serverr   r   r#   r(   rj   objectro   ru   rv   	Exceptionrx   ry   r   r   r   r   <module>   s0   
\