o
    i"                     @   s   d dl Z e eZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZ G dd	 d	eZd
d ZdS )    N)isgenerator)EventManagerIgnoredprocess_contexts)FaultPushBase)ProtocolBase)Break	coroutinec                   @   s   e Zd ZdZdZ	 dd Zedd Zejdd Zdd	 Z	d$d
dZ
dd Zdd Zdd Zdd Zdd ZeZedd Zdd Zdd Zdd Zdd Zd d! Zed"d# ZdS )%
ServerBaseaI  This class is the abstract base class for all server transport
    implementations. Unlike the client transports, this class does not define
    a pure-virtual method that needs to be implemented by all base classes.

    If there needs to be a call to start the main loop, it's called
    ``serve_forever()`` by convention.
    Nc                 C   s&   || _ | j| j _|   t| | _d S N)app	transportappinitr   event_manager)selfr    r   R/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/server/_base.py__init__-   s   
zServerBase.__init__c                 C   s
   | j jjS r   )r   	interfacedocsr   r   r   r   doc4   s   
zServerBase.docc                 C   s   t | jj_d S r   )r   r   r   r   )r   valuer   r   r   r   8   s   c                 C   s   | j |  d S r   )r   reinitializer   r   r   r   r   <      zServerBase.appinitc              
   C   s   z| j j|| | j j|tj | j j|}W |S  ty? } zd|_||_	||_
|f}|d W Y d}~|S d}~ww )zCalls create_in_document and decompose_incoming_envelope to get
        method_request string in order to generate contexts.
        Nmethod_exception_object)r   in_protocolcreate_in_documentdecompose_incoming_enveloper	   REQUESTgenerate_method_contextsr   	in_objectin_error	out_error
fire_event)r   ctxin_string_charsetretvaler   r   r   generate_contexts?   s    
zServerBase.generate_contextsc              
   C   sz   z| j jj|| j jjd W dS  ty< } z!t| td|j d|_	||_
||_|d W Y d}~dS d}~ww )znUses the ``ctx.in_string`` to set ``ctx.in_body_doc``, which in turn
        is used to set ``ctx.in_object``.messagezFailed document is: %sNr   )r   r   deserializer!   r   logger	exceptiondebugin_documentr#   r$   r%   r&   )r   r'   r*   r   r   r   get_in_object[   s   

zServerBase.get_in_objectc                 C   sp   |j du r| j| n|j t|jttfr+t|jdkr+t|jd tr+d|_dS t|jtr6d|_dS dS )zfCalls the matched user function by passing it the ``ctx.in_object``
        to set ``ctx.out_object``.Nr   r   r   )	r$   r   process_request
isinstance
out_objectlisttuplelenr   r   r'   r   r   r   get_out_objectn   s   


zServerBase.get_out_objectc                 C   sz   |j \}|d u r|t  n t|trnt|jdkr)|jd }t|ts(J ntd| | |||}| 	|||S )Nr   z%r is not a PushBase instance)
r6   throwr
   r5   r   r9   pusher_stack
ValueErrorinit_interim_pushpusher_try_close)r   r'   genoobjr)   r   r   r   convert_pull_to_push   s   

zServerBase.convert_pull_to_pushc                 C   sr   |j durdS |jdu r2|jj|tjd}t|r2|jdur2t|jdkr2t|j	dkr2| 
||S | | dS )z]Uses the ``ctx.out_object`` to set ``ctx.out_document`` and later
        ``ctx.out_string``.Nr,      r   )
out_stringout_documentout_protocol	serializer	   RESPONSEr   r6   r9   r>   rD   finalize_context)r   r'   retr   r   r   get_out_string_pull   s   

zServerBase.get_out_string_pullc                 C   sd   |j d u r|d n|d |j| |j d u r!|d n|d |jd u r0d|_d S d S )Nmethod_return_documentmethod_exception_documentmethod_return_stringmethod_exception_string)    )r%   r&   rH   create_out_stringrF   r:   r   r   r   rK      s   





zServerBase.finalize_contextc                 c   sv    |j j|tjd}t|r4z
	 dV }|| q ty3   z|t  W n	 ty0   Y nw Y nw | 	| dS )z?Uses the ``ctx.out_object`` to directly set ``ctx.out_string``.r,   TN)
rH   rI   r	   rJ   r   sendr
   r=   StopIterationrK   )r   r'   rL   yr   r   r   get_out_string_push   s"   
zServerBase.get_out_string_pushc                 C   s   t  )z*Implement your event loop here, if needed.)NotImplementedErrorr   r   r   r   serve_forever   s   zServerBase.serve_foreverc                    sH   t |tsJ  jd usJ  d  fdd}j |||ddS )Nmethod_return_pushc                      s   t d  d S )Nr   r   r   p_ctxr   r   r   _cb_push_finish   r   z5ServerBase.init_interim_push.<locals>._cb_push_finishTinterim)r5   r   
out_streamr&   pusher_init)r   rL   r\   rB   r]   r   r[   r   r@      s
   
zServerBase.init_interim_pushc                 C   s   | |||d |S r   )init)r   r\   rB   r]   pusherr_   r   r   r   ra      s   zServerBase.pusher_initc                 C   s.   t d| |  |j }||u sJ d S )NzClosing pusher with ret=%r)r/   r1   closer>   pop)r   r'   rc   _poppedr   r   r   rA      s   
zServerBase.pusher_try_closec                    s   t |tsJ |jv rtd d S j| d }t|s+J d fdd}j	|||dd}
|| |S )NzPushBase reinit avoided.rZ   z7It looks like this protocol is not async-compliant yet.c                      s   t   d S r   r   r   othersr\   r   r   r   r]      r   z2ServerBase.init_root_push.<locals>._cb_push_finishFr^   )r5   r   r>   r/   warningappendr&   rW   r   ra   rA   )r   rL   r\   ri   rB   r]   r)   r   rh   r   init_root_push   s   



zServerBase.init_root_pushc                 C   s   t  | _| jd  d S r   )_writerG   rT   )r'   r   r   r   set_out_document_push
  s   z ServerBase.set_out_document_pushr   )__name__
__module____qualname____doc__r   r   propertyr   setterr   r+   r3   r;   rD   rM   rK   get_out_stringr   rW   rY   r@   ra   rA   rl   staticmethodrn   r   r   r   r   r       s4    



r   c                  c   s    d V } | V  d S r   r   )vr   r   r   rm     s   
rm   )logging	getLoggerro   r/   inspectr   spyner   r   spyne.auxprocr   spyne.modelr   r   spyne.protocolr	   
spyne.utilr
   r   objectr   rm   r   r   r   r   <module>   s   
 q