o
    jv                     @   sx  d dl mZmZ d dlZeeZd dlZd dlZd dl	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mZ d dlmZmZ zd d	lmZ d d
lmZ W n eyd   dZdZY nw d dlmZ d dlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)mZm*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d dl>m?Z?m@Z@mAZAmBZBmCZCmDZD d dlEmFZFmGZG d dlHmIZI d dlJmKZK G dd deZLdeMeMeMdeMddd ddd ddd ddd dd d eNd!d d"d#d i
ZOdejPeMejPdejPdd$d dd%d dd&d dd'd dd(d eNd)d d"d*d eNeNfd+d d"eNfd,d eNeMfd-d d"eMfd.d iZQeIjRrXeQd"eNf eQd"eSf< eQd"eNf eQeNeSf< dLd/d0ZTd1d ZUd2d ZVd3d ZWd4d ZXd5d ZYi d6eUd7eUd8eUd9eUd:eVd;eVd<eVd=eVd>eWd?eWd@eWdAeWdBeWdCeWdDeXdEeXdFeXeXeXeXeYeYeYeYeYeYdG	ZZdHdI Z[dJdKgZ\dS )M    )print_functionunicode_literalsN)isabsjoinabspath)deque)datetime)Decimal)mmapACCESS_READ)mktimestrftime)etree)html)ProtocolMixin)	ModelBaseXmlAttributeSimpleModelNull	ByteArrayFileComplexModelBaseAnyXmlAnyHtmlUnicoder	   DoubleIntegerTimeDateTimeUuidDurationBooleanAnyDictAnyUriPushBaseDate)FileData)HTTP_400HTTP_401HTTP_404HTTP_405HTTP_413HTTP_500)FaultInternalErrorResourceNotFoundErrorRequestTooLongErrorRequestNotAllowedInvalidCredentialsError)binary_encoding_handlersBINARY_ENCODING_USE_DEFAULT)six)cdictc                       s  e Zd ZdZ		ds fdd	Zdd Zdtdd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zdtd"d#Zdtd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dtdLdMZ(dtdNdOZ)dPdQ Z*dtdRdSZ+dtdTdUZ,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1d`da Z2dbdc Z3ddde Z4dfdg Z5dhdi Z6djdk Z7dldm Z8e9:dnZ;e<dodp Z=e>dqdr Z?  Z@S )uOutProtocolBasea  This is the abstract base class for all out protocol implementations.
    Child classes can implement only the required subset of the public methods.

    An output protocol must implement :func:`serialize` and
    :func:`create_out_string`.

    The OutProtocolBase class supports the following events:

    * ``before_serialize``:
      Called before after the serialization operation is attempted.

    * ``after_serialize``:
      Called after the serialization operation is finished.

    The arguments the constructor takes are as follows:

    :param app: The application this protocol belongs to.
    :param mime_type: The mime_type this protocol should set for transports
        that support this. This is a quick way to override the mime_type by
        default instead of subclassing the releavant protocol implementation.
    :param ignore_uncap: Silently ignore cases when the protocol is not capable
        of serializing return values instead of raising a TypeError.
    NFc                    s  t t| j|||||d || _d | _|d ur|| _ti t| jt	| j
t| jt| jt| jt| jt| jt| jt| jt| jt| jt| jt| j t!| j"t#| j$t%| j&t'| j(t)| j*i| _+ti t| j,t	| j-t| j.t| j/t| j0t| j1t| j2t| j3t4| j5t6| j7t| j8t| j9t| j:t| j;t| j<t!| j=t#| j>t%| j?t'| j@t)| jAi| _Btt	| jCt%| jDt| jEtF| jGt)| jHi| _Id S )N)app	mime_typeignore_wrappersbinary_encodingstring_encoding)Jsuperr7   __init__ignore_uncapmessager9   r6   r   model_base_to_bytesr   file_to_bytesr   time_to_bytesr   uuid_to_bytesr   null_to_bytesr%   date_to_bytesr   double_to_bytesr   any_xml_to_bytesr   unicode_to_bytesr!   boolean_to_bytesr	   decimal_to_bytesr   integer_to_bytesr   any_html_to_bytesr   datetime_to_bytesr    duration_to_bytesr   byte_array_to_bytesr   xmlattribute_to_bytesr   complex_model_base_to_bytes_to_bytes_handlersmodel_base_to_unicodefile_to_unicodetime_to_unicodedate_to_unicodeuuid_to_unicodenull_to_unicodedouble_to_unicodeany_xml_to_unicoder#   any_uri_to_unicoder"   any_dict_to_unicodeany_html_to_unicodeunicode_to_unicodeboolean_to_unicodedecimal_to_unicodeinteger_to_unicodedatetime_to_unicodeduration_to_unicodebyte_array_to_unicodexmlattribute_to_unicodecomplex_model_base_to_unicode_to_unicode_handlersfile_to_bytes_iterablebyte_array_to_bytes_iterablemodel_base_to_bytes_iterabler   simple_model_to_bytes_iterablecomplex_model_to_bytes_iterable_to_bytes_iterable_handlers)selfr8   r9   r?   r:   r;   r<   	__class__ W/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/protocol/_outbase.pyr>   V   s   	

	

zOutProtocolBase.__init__c                 C      dS )a\  Serializes ``ctx.out_object``.

        If ctx.out_stream is not None,  ``ctx.out_document`` and
        ``ctx.out_string`` are skipped and the response is written directly to
        ``ctx.out_stream``.

        :param ctx: :class:`MethodContext` instance.
        :param message: One of ``(ProtocolBase.REQUEST, ProtocolBase.RESPONSE)``.
        Nrr   )ro   ctxr@   rr   rr   rs   	serialize       zOutProtocolBase.serializec                 C   rt   )z+Uses ctx.out_document to set ctx.out_stringNrr   )ro   ru   out_string_encodingrr   rr   rs   create_out_string   rw   z!OutProtocolBase.create_out_stringc                 C   s`   t |trtS t |trtS t |trtS t |trtS t |t	r.|j
ds,|j
dkr.tS tS )z]Special function to convert native Python exceptions to Http response
        codes.
        zClient.Client)
isinstancer0   r+   r/   r)   r1   r*   r2   r(   r-   	faultcode
startswithr'   r,   )ro   faultrr   rr   rs   fault_to_http_response_code   s   




z+OutProtocolBase.fault_to_http_response_codec                 C   s   |du sJ d| _ dS )zXYou must override this function if you want your protocol to support
        validation.N)	validator)ro   r   rr   rr   rs   set_validator   s   
zOutProtocolBase.set_validatorc                 O   2   |d u rd S | j | }|||g|R i |}|S N)rS   ro   clsvalueargskwargshandlerretvalrr   rr   rs   to_bytes   s
   
zOutProtocolBase.to_bytesc                 O   r   r   )rh   r   rr   rr   rs   
to_unicode   s
   
	zOutProtocolBase.to_unicodec                 C   s.   |d u rg S t |tr|S | j| }|||S r   )r{   r$   rn   )ro   r   r   r   rr   rr   rs   to_bytes_iterable   s   


z!OutProtocolBase.to_bytes_iterablec                 K   rt   )N    rr   ro   r   r   _rr   rr   rs   rE         zOutProtocolBase.null_to_bytesc                 K   rt   )N rr   r   rr   rr   rs   rY      r   zOutProtocolBase.null_to_unicodec                 K   
   t |S r   r   tostringr   rr   rr   rs   rH         
z OutProtocolBase.any_xml_to_bytesc                 K      t j|ddS Nunicode)encodingr   r   rr   rr   rs   r[         z"OutProtocolBase.any_xml_to_unicodec                 K   s   t |S r   )reprr   rr   rr   rs   r]         z#OutProtocolBase.any_dict_to_unicodec                 K   r   r   r   r   r   rr   rr   rs   rM      r   z!OutProtocolBase.any_html_to_bytesc                 K   r   r   r   r   rr   rr   rs   r^      r   z#OutProtocolBase.any_html_to_unicodec                 K   sF   |  |j}| j||fd|i|}|ddddtjfv r|S |dS )Nsuggested_encodingbytesbytes_lefieldsintascii)get_cls_attrsserialize_asrX   r5   binary_typeencode)ro   r   r   r   r   ser_asr   rr   rr   rs   rD     s   
zOutProtocolBase.uuid_to_bytesc           	      K   sH   |  |}|j}|j}|d u r|}t| |}|dv r"t| |f}|S )N)r   r   )r   r   r   _uuid_serializer3   )	ro   r   r   r   r   attrr   r   r   rr   rr   rs   rX     s   
zOutProtocolBase.uuid_to_unicodec                 K   s   |}|  |}t|tjr/|jd ur||j}n| jd ur%|| j}n
tjs/t	d||  |j
d ur:|j
|S |jd urD|j| S |S NzMYou need to set either an encoding for %r or a default_string_encoding for %r)r   r{   r5   	text_typer   r   default_string_encodingPY2loggerwarning
str_formatformat)ro   r   r   r   r   	cls_attrsrr   rr   rs   rI     s    





z OutProtocolBase.unicode_to_bytesc                 K      | j ||fi |S r   )r_   r   rr   rr   rs   r\   /     z"OutProtocolBase.any_uri_to_unicodec                 K   s   |  |}|}t|tjr.|jd ur||j}| jd ur$|| j}n
tjs.t	d||  |j
d ur9|j
|S |jd urC|j| S |S r   )r   r{   r5   r   r   decoder   r   r   r   r   r   )ro   r   r   r   r   r   rr   rr   rs   r_   2  s    





z"OutProtocolBase.unicode_to_unicodec                 K      | j ||fi |dS Nutf8)ra   r   r   rr   rr   rs   rK   I     z OutProtocolBase.decimal_to_bytesc                 K   D   t | | |}|jd ur|j|S |jd ur|j| S t|S r   )Dr   r   r   strro   r   r   r   r   rr   rr   rs   ra   L     



z"OutProtocolBase.decimal_to_unicodec                 K   r   r   )rZ   r   r   rr   rr   rs   rG   W  r   zOutProtocolBase.double_to_bytesc                 K   r   r   )floatr   r   r   r   r   rr   rr   rs   rZ   Z  r   z!OutProtocolBase.double_to_unicodec                 K   r   r   )rb   r   r   rr   rr   rs   rL   e  r   z OutProtocolBase.integer_to_bytesc                 K   r   r   )r   r   r   r   r   r   rr   rr   rs   rb   h  r   z"OutProtocolBase.integer_to_unicodec                 K   r   r   )rV   ro   r   r   r   rr   rr   rs   rC   s  r   zOutProtocolBase.time_to_bytesc                 K   s   t |tr	| }| S )zReturns ISO formatted times.)r{   r   time	isoformatr   rr   rr   rs   rV   v  s   
zOutProtocolBase.time_to_unicodec                 K   r   r   )rW   r   )ro   r   valr   rr   rr   rs   rF   |  r   zOutProtocolBase.date_to_bytesc                 K   sL   t |tr	| }| |j}|d u s|tdfv r| ||S t| ||S Nr   )r{   r   dater   r   r   _date_to_bytes_datetime_smapro   r   r   r   sarr   rr   rs   rW     s   
zOutProtocolBase.date_to_unicodec                 K   sF   | j ||fi |}| |j}|d u s|tjtdfv r!|dS |S )Nr   r   )rc   r   r   r5   r   r   r   )ro   r   r   r   r   r   rr   rr   rs   rN     s
   
z!OutProtocolBase.datetime_to_bytesc                 K   s>   |  |j}|d u s|tjtdfv r| ||S t| ||S r   )r   r   r5   r   r   _datetime_to_unicoder   r   rr   rr   rs   rc     s   z#OutProtocolBase.datetime_to_unicodec                 K   r   r   )rd   r   r   rr   rr   rs   rO     r   z!OutProtocolBase.duration_to_bytesc                 K   sL  |j dk r| }d}nd}t| }|jd }|jd }|d }|d; }t|}|j}	t }
|r7|
d n|
d |j dkrI|
d|j   |dkr\|d dkr\|	dkr\d	|
S |
d
 |dkrl|
d|  |dkrw|
d|  |dks|	dkr|
d|  |	dkr|
d|	  |
d t	|
dkr|
d d	|
S )Nr   TF<   z-PPz%iDiQ r   Tz%iHz%iMz%iz.%iS   0S)
daysr   total_secondssecondsr   microsecondsr   appendr   len)ro   r   r   r   negativetot_secr   minuteshoursusecondsr   rr   rr   rs   rd     s@   









z#OutProtocolBase.duration_to_unicodec                 K   s   t t| dS )Nr   )r   boollowerr   r   rr   rr   rs   rJ        z OutProtocolBase.boolean_to_bytesc                 K   s   t t| S r   )r   r   r   r   rr   rr   rs   r`        z"OutProtocolBase.boolean_to_unicodec           	      K   s   |  |}|j}|tu r|d u r| j}n|}|d u r2t|ttfr2t|dkr2t|d tr2|d S t	| }t
d|| ||}|d urPt|tjrP|d}|S )N   r   z'Using binary encoder %r for encoding %rr   )r   r   r4   r;   r{   listtupler   r
   r3   r   debugr5   r   r   )	ro   r   r   r   r   r   r   encoderr   rr   rr   rs   rP     s"   

z#OutProtocolBase.byte_array_to_bytesc                 K   s^   |  |j}|tu r|d u r| j}n|}|d u rtdt| |}t|tjs-|	d}|S )Nz4Arbitrary binary data can't be serialized to unicoder   )
r   r   r4   r;   
ValueErrorr3   r{   r5   r   r   )ro   r   r   r   r   r   r   rr   rr   rs   re     s   
z%OutProtocolBase.byte_array_to_unicodec                 K   s   |S r   rr   r   rr   rr   rs   rj     r   z,OutProtocolBase.byte_array_to_bytes_iterablec           
   
   C   s  |  |j}|tu r|du r| j}n|}t|tjr|jdur&t| |jS |j	durqt
|j	dretjrB|j	 }t|dtdf}n)ddl}z|j	 }t|dtd}W n |jyd   |j	 f}Y nw |j	 f}t| |S |jdurt|jd}| }t|dtd}t| |S J dt|trzt| |jW S  ty }	 ztd|	|  d}	~	ww zt| |W S  ty }	 ztd|	|  d}	~	ww )	
        :param cls: A :class:`spyne.model.File` subclass
        :param value: Either a sequence of byte chunks or a
            :class:`spyne.model.File.Value` instance.
        Nfilenor   )accessrbFzUnhandled file typez4Error encoding value to binary. Error: %r, Value: %r)r   r   r4   r;   r{   r   Valuedatar3   handlehasattrr5   r   r   r
   r   ioUnsupportedOperationreadpathopenr&   	Exceptionr   error)
ro   r   r   r   r   r   r   r   r   err   rr   rs   rB     s^   





zOutProtocolBase.file_to_bytesc                 C   s`   |  |}|j}|tu r|}|du r|jtju rtd| |||}t|t	j
s.|d}|S )r   Nz5Arbitrary binary data can't be serialized to unicode.r   )r   r   r4   moder   TEXTr   rB   r{   r5   r   r   )ro   r   r   r   r   r   r   rr   rr   rs   rU   7  s   

zOutProtocolBase.file_to_unicodec              
   K   s   |j d ur!t|j ttfrt|j d trt|j d S t|j S |jd ur2|j}|d t|S |j	d us;J dz#|j	}t
|j	sWt|j|j	}t||jsWJ dtt|dW S  tyy } z|jtjkrqt|j	tdd }~ww )Nr   zOYou need to write data to persistent storage first if you want to read it back.zNo relative paths are allowedr   zError accessing requested file)r   r{   r   r   r
   _file_to_iteriterr   seekr   r   r   storer   r}   r   IOErrorerrnoENOENTr/   r.   )ro   r   r   r   fr   r   rr   rr   rs   ri   L  s2   





z&OutProtocolBase.file_to_bytes_iterablec                 K   s&   | j ||fi |}|d u rdS |fS )N)r   r   )ro   r   r   r   r   rr   rr   rs   rl   i  s   z.OutProtocolBase.simple_model_to_bytes_iterablec                 K   s   | j rt S td)Nz,This protocol can only serialize primitives.)r?   r   	TypeErrorr   rr   rr   rs   rm   o  s   z/OutProtocolBase.complex_model_to_bytes_iterablec                 K      t dNz,Only primitives can be serialized to string.r  r   rr   rr   rs   rR   t  r   z+OutProtocolBase.complex_model_base_to_bytesc                 K   r  r  r  r   rr   rr   rs   rg   w  r   z-OutProtocolBase.complex_model_base_to_unicodec                 K      | j |j|fi |S r   )r   typero   r   stringr   rr   rr   rs   rQ   z  r   z%OutProtocolBase.xmlattribute_to_bytesc                 K   r  r   )r   r  r	  rr   rr   rs   rf   }  r   z'OutProtocolBase.xmlattribute_to_unicodec                 K      |j |fi |S r   )r   r   rr   rr   rs   rk        z,OutProtocolBase.model_base_to_bytes_iterablec                 K   r  r   r  r   rr   rr   rs   rA     r  z#OutProtocolBase.model_base_to_bytesc                 K   r  r   )r   r   rr   rr   rs   rT     r  z%OutProtocolBase.model_base_to_unicodec           	      K   s   |  |}|jdur|jdur||j}|js|jdd}| |}|du r,| }ntj	rAt
|trA| ||dd}n| ||}|j}|du rQ|j}|durZ||S |j}|durf||S |S )z Returns ISO formatted datetimes.N)tzinfor   )r   as_timezoner  
astimezonetimezonereplace_get_datetime_formatr   r5   r   r{   r   r   r   r   string_formatr   r   interp_format)	ro   r   r   r   r   	dt_formatr   r   r  rr   rr   rs   r     s(   




z$OutProtocolBase._datetime_to_unicodec           	      K   s   |  |}|j}|d u r| }ntjr(t|tr(|d}| ||	d}n| ||}|j
}|d ur:||S |j}|d urF||S |S r   )r   date_formatr   r5   r   r{   r   r   r   r   r   r   )	ro   r   r   r   r   r  r   r   r   rr   rr   rs   r     s   




zOutProtocolBase._date_to_bytesz((^|[^%])(%%)*%s)c                 C   s8   g }d}	 |  ||}|dkr	 |S || |d }q)Nr   r   )findr   )textsubstrsitesijrr   rr   rs   _findall_datetime  s   
z!OutProtocolBase._findall_datetimec                 C   s$  | j |r
td|jdkr||S |j}d| }d|d |d   }||7 }|d| d d 7 }| }t||f|dd   }| |t|}t||d f|dd   }	| |	t|d }
g }|D ]}||
v rr|| qg|}d	|jf }|D ]}|d | | ||d
 d   }q}|S )Nz/This strftime implementation does not handle %sil  i     d   i     r   z%4d   )	
_illegal_ssearchr  yearr   	timetupler  r   r   )r   dtfmtr%  deltaoffr&  s1sites1s2sites2r  sitessyearrr   rr   rs   r     s0   


"zOutProtocolBase.strftime)NNFFNNr   )A__name__
__module____qualname____doc__r>   rv   ry   r   r   r   r   r   rE   rY   rH   r[   r]   rM   r^   rD   rX   rI   r\   r_   rK   ra   rG   rZ   rL   rb   rC   rV   rF   rW   rN   rc   rO   rd   rJ   r`   rP   re   rj   rB   rU   ri   rl   rm   rR   rg   rQ   rf   rk   rA   rT   r   r   recompiler#  staticmethodr  classmethodr   __classcell__rr   rr   rp   rs   r7   =   s|    C




-



<$
)
r7   r   hexc                 C      | j S r   r;  urr   rr   rs   <lambda>      r@  urnc                 C   r<  r   )rB  r>  rr   rr   rs   r@    rA  r   c                 C   r<  r   r   r>  rr   rr   rs   r@    rA  r   c                 C   r<  r   r   r>  rr   rr   rs   r@    rA  r   c                 C   r<  r   r   r>  rr   rr   rs   r@    rA  c                 C   r<  r   r   r>  rr   rr   rs   r@    rA  r   c                 C   r<  r   rF  r>  rr   rr   rs   r@    rA  c                 C      t j| dS Nr=  uuidUUIDr0  rr   rr   rs   r@         c                 C   rG  rH  rI  rL  rr   rr   rs   r@  !  rM  c                 C   rG  )NrC  rI  rL  rr   rr   rs   r@  "  rM  c                 C   rG  )NrD  rI  rL  rr   rr   rs   r@  #  rM  c                 C   rG  )NrE  rI  rL  rr   rr   rs   r@  $  rM  c                 C   rG  NrF  rI  rL  rr   rr   rs   r@  &  rM  c                 C   rG  rN  rI  rL  rr   rr   rs   r@  '  rM  c                 C   rG  rN  rI  rL  rr   rr   rs   r@  )  rM  c                 C   rG  rN  rI  rL  rr   rr   rs   r@  *  rM  c                 C      t jt| dS rN  rJ  rK  r   rL  rr   rr   rs   r@  ,      c                 C   rO  rN  rP  rL  rr   rr   rs   r@  -  rQ  c           
   	   C   s   |   }t|d}t|d}t|d}t|d}t|d}t|d}|d}	|	d u r:d}	n
ttt|	d	 }	t|||||||	|S )
Nr%  monthdayhrminsecsec_fracr       .A)	groupdictr   getroundr   r   )

date_matchtzr   r%  rR  rS  hourminutesecondusecondrr   rr   rs   _parse_datetime_iso_match5  s   
rb  c                 C   s   t t| S r   )r   r   r&  r   r   rr   rr   rs   r@  I  r   c                 C   s   t | |jd  S )NrX  r   r&  microsecondrc  rr   rr   rs   r@  K  r   c                 C   s   t t| d |jd  S )N  r   r   r&  re  rc  rr   rr   rs   r@  N  s   c                 C   s   t | d |jd  S )Nrf  g     @@rd  rc  rr   rr   rs   r@  P  r   c                 C   s   t t| d |j S )Ni@B rg  rc  rr   rr   rs   r@  S  r   rV  secsr`  r   	sec_float
secs_floatsecond_floatseconds_floatmsecmsecsmsecondmsecondsmillisecondmilliseconds
msec_floatmsecs_floatmsecond_float)	mseconds_floatmillisecond_floatmilliseconds_floatusecusecsra  r   re  r   c                 c   s\    z'|  d}t|dkr"|V  |  d}t|dksW |   d S W |   d S |   w )Ni    r   )r   r   close)r  r   rr   rr   rs   r   x  s   

r   nullabledefault_factoryr   )]
__future__r   r   logging	getLoggerr2  r   r6  rJ  r   os.pathr   r   r   collectionsr   r   decimalr	   r   r
   r   r   r   r   lxmlr   r   ImportErrorspyne.protocol._baser   spyne.modelr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   spyne.model.relationalr&   spyne.const.httpr'   r(   r)   r*   r+   r,   spyne.errorr-   r.   r/   r0   r1   r2   spyne.model.binaryr3   r4   
spyne.utilr5   spyne.util.cdictr6   r7   r   r   r   rK  _uuid_deserializer   longrb  _dt_sec_dt_sec_float_dt_msec_dt_msec_float_dt_usecr   r   	META_ATTRrr   rr   rr   rs   <module>   s   
`       U
	"