o
    j                     @   sB  d dl mZ d dlZede Zede Zd dlmZm	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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" d dl#m$Z$ d dl%m&Z& dd Z'e(de)fi Z*d"ddZ+dd Z,			d#ddZ-G dd de)Z.e-e.e.j/ G d d! d!e$e.Z0dS )$    )print_functionNz%s.clothz%s.serializer)htmletree)deepcopy)isgenerator)Break	coroutine)oset)string_types)RB)
ArrayAnyXmlAnyHtml	ModelBaseComplexModelBasePushBaseXmlAttributeAnyUriXmlDataAny)OutProtocolBase)cdictc                 C   s   t tt|  S N)listreversedtupleiterancestors)elt r   ]/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/protocol/cloth/to_cloth.py<lambda>)   s    r!   _NODATAc                 C   s   t tt| ||S r   )r   r   _prevsibls_since)r   strip_commentssincer   r   r    
_prevsibls.   s   r&   c                 c   sd    || u rd S | j ddD ]"}||u r d S |r,t| tjr,| jdr+| jdr+nq|V  qd S )NT	preceding[if [endif])itersiblings
isinstancer   CommentBasetext
startswithendswith)r   r$   r%   prevsiblr   r   r    r#   2   s   r#   mrpciddataattrroottagbagc	           	      C   s   || _ d||| _d||| _d||| _d||| _d||| _d||| _d||| _d|| _	| j| j| j| j| j| j	h| _
d S )Nz{}{}z{}write-contents)	ID_PREFIXformatMRPC_IDID_ATTR_NAMEDATA_TAG_NAMEDATA_ATTR_NAMEATTR_ATTR_NAMEROOT_ATTR_NAMETAGBAG_ATTR_NAMEWRITE_CONTENTS_WHEN_NOT_NONESPYNE_ATTRS)	objprefixmrpc_idid_attrdata_tag	data_attr	attr_attr	root_attrtagbag_attrr   r   r    _set_identifier_prefixC   s    
rL   c                   @   st   e Zd ZdZdZdZdZdZdZdZ	dZ
dd	 ZedddZedddZedd Zdd Zdd Zdd ZdS )ClothParserMixinzspyne-zspyne-idz
spyne-dataz
spyne-attrz
spyne-rootzspyne-tagbagzspyne-write-contentsc                 C   s   t | | | S r   )rL   )selfwhatr   r   r    set_identifier_prefixi   s   
z&ClothParserMixin.set_identifier_prefixTc                 C      |  }|j |t |d |S N)cloth_parserr$   )_init_clothr   	XMLParserclsclothr$   retvalr   r   r    from_xml_clothm   
   zClothParserMixin.from_xml_clothc                 C   rQ   rR   )rT   r   
HTMLParserrV   r   r   r    from_html_clotht   r[   z ClothParserMixin.from_html_clothc                 C   sR   |   D ]"}t|tjr&| d ur&|jdr|jdrq| | qd S )Nr)   r*   )	iterr,   r   r-   	getparentr.   r/   r0   remove)r6   r   r   r   r    _strip_comments{   s   
z ClothParserMixin._strip_commentsc                 C   s   t j||d}| S )N)parser)r   parsegetroot)rN   	file_namerS   rX   r   r   r    _parse_file   s   zClothParserMixin._parse_filec                 C   s   d| _ d| _|| _d | _| _|du rdS t|tr | ||}|r'| | d| j }|	|}t
|dkrCtd| |d | _n	td| || _ | |d| _dS )zaCalled from XmlCloth.__init__ in order to not break the dunder init
        signature consistencyNz//*[@%s]r   zUsing %r as root cloth.zUsing %r as plain cloth.
mrpc_entry)_cloth_root_clothr$   _mrpc_clothr,   r
   rf   ra   r?   xpathlenlogger_cdebug_pop_elt)rN   rX   rS   r$   qeltsr   r   r    rT      s$   



zClothParserMixin._init_clothc                 C   s^   d| j |f }||}t|dkrtd| t|dkr-|d }t| | |S d S Nz//*[@%s="%s"]   z(more than one element found for query %rr   )r;   rk   rl   
ValueErrornextr   r`   rN   r   rO   queryrY   r   r   r    ro      s   
zClothParserMixin._pop_eltN)T)__name__
__module____qualname__r8   r;   r<   r=   r>   r?   r@   rA   rP   classmethodrZ   r]   staticmethodra   rf   rT   ro   r   r   r   r    rM   \   s&    

rM   c                       s   e Zd Z		d. fdd	Zd/ddZd/dd	Zd
d Zdd Zdd Ze	dd Z
dd Zi dddfddZdd Ze	d0ddZedd Ze		d1ddZdd Zd d! Zd"d# Zd$d% Zd&d' Ze		d2d(d)Zd*d+ Zed/d,d-Z  ZS )3ToClothMixinNFTc                    sP   t t| j||||d || _tt| jt| jt	| j
t| jt| jt| ji| _d S )N)app	mime_typeignore_uncapignore_wrappers)superr}   __init__polymorphicr   r   model_base_to_clothr   xml_to_clothr   any_to_clothr   html_to_clothr   any_uri_to_clothr   complex_to_clothrendering_handlers)rN   r~   r   r   r   r   	__class__r   r    r      s   zToClothMixin.__init__c                 C   s,   |d u r| d| j S | d| j|f S )Nz#.//*[@*[starts-with(name(), "%s")]]z(.//*[@*[starts-with(name(), "%s")]="%s"])rk   r8   )rN   r   tag_idr   r   r    	_get_elts   s   zToClothMixin._get_eltsc                 c   s    t  }| ||D ]?}||u rtd q
tt dd | D |@ dkr-td q
t||v r9td q
| j|jv rF|	t| |V  q
d S )NzDon't send myselfc                 s   s    | ]}t |V  qd S r   )r3   ).0er   r   r    	<genexpr>   s    z1ToClothMixin._get_outmost_elts.<locals>.<genexpr>r   zDon't send grandchildrenz#Don't send what's already been sent)
setr   rm   rn   rl   r   r3   r;   attribadd)rN   tmplr   idsr   r   r   r    _get_outmost_elts   s    
"

zToClothMixin._get_outmost_eltsc                 C   sV   d| j |f }||}t|dkrtd| t|dkr)|d }|j| j = |S d S rr   )r;   rk   rl   rt   r   rv   r   r   r    _get_clean_elt   s   

zToClothMixin._get_clean_eltc                 C   s    | d| }td|| |S )Nz//*[@id="%s"]zid=%r got %r)rk   rm   rn   )rN   r   rO   rY   r   r   r    _get_elts_by_id   s   zToClothMixin._get_elts_by_idc                 C   s   | j |jv S r   )r@   r   )rN   r   r   r   r    
_is_tagbag   s   zToClothMixin._is_tagbagc                 c   s    |j jrt|jdkr|j \}|j jrt|jdks|j jd urB|j j D ]\}}d}|jd ur:||| }|rA||fV  q)d S d S )Nr   T)
Attributes_wrapperrl   
_type_infovaluesmethodsitemswhen)ctxrW   instkvis_shownr   r   r    _methods   s   

zToClothMixin._methodsc                 C   s   | j d u rtd d S | || jD ]C}| |||D ]9\}}|j }||j	|j }	t
| j }
| |
d}||jd< | |
d}|d urM|	|_n|	|_||
 qqd S )Nzmissing 'mrpc_template'	mrpc_linkhref	mrpc_text)rj   rm   warningr   r:   r   
in_messageget_type_name	translatelocaler   r   r   r.   append)rN   r   rW   r   templater   r   r   r   r.   mrpc_templateanchortext_eltr   r   r    _actions_to_cloth  s"   




zToClothMixin._actions_to_clothc              	      s  t d|j|j|j||| |jjs ||| |jj	}|jj
}	t|	ts(J |jj}
|jj}|
|	j }||	j }|rJt|dkrJ|d |u rJdS |  }||	vrj|	| |
|	j }||	j }|	j|ksjJ |	j|krz || |	j|ksod}t|dkr|d }t|}|dt| |kr| }| }|}|durǈ jd|f|| |ddd t d|j|j |jdur||j |dt| |kr|jddD ]}t d	|j|jt| || q|dt| |ks|t|d D ]}t| j|d
}|D ]&}t||v rt d|j|j q	t d|j|jt| || q	i }t|dkr>|j|d< |j |j|jfi |}|!  t d|j|jt|| |j"durg||j" |	|   |
|	j }||	j }|#| |#| qt| j|d
}|D ]*}||u rqt||v rt d|j|j qt d|j|j || q|p|j j$k}|r|t| |dur|%|}|!  nSd}nPt& fdd|j' D }|(|  jd|f||| i }t|dkr|j|d< |dur||d< |j |j|fi |}|!  |j"dur/||j" |	|   |
|	j }||	j }|#| |#| t d dS )a  Enters the given tag in the document by using the shortest path from
        current tag.

        1. Moves up the tree by writing all tags so that the set of ancestors
           of the current tag are a subset of the ancestors of the parent tag
        2. Writes all tags until hitting a direct ancestor, enters it, and
           keeps writing previous siblings of ancestor tags and entering
           ancestor tags until hitting the target tag.
        3. Enters the target tag and returns

        There is no _exit_cloth because exiting from tags is done
        automatically with subsequent calls to _enter_cloth and finally to
        _close_cloth.

        :param ctx: A MethodContext instance
        :param cloth: The target cloth -- an ``lxml.etree._Element`` instance.
        :param parent: The target stream -- typically an
            ``lxml.etree._IncrementalFileWriter`` instance.
        :param attrib: A dict of additional attributes for the target cloth.
        :param skip: When True, the target tag is actually not entered.
            Typically used for XmlData and friends.
        :param method: One of ``(None, 'html', 'xml')``. When not ``None``,
            overrides the output method of lxml.
        :param skip_dupe: When ``False`` (the default) if this function is
            called repeatedly for the same tag, the tag is exited and reentered.
            This typically happens for types with ``max_occurs`` > 1
            (eg. arrays).
        z3entering %s %r nsmap=%r attrib=%r skip=%s method=%sr   Nbefore_exitz	exit norm %s %sFr'   z	write exit sibl %s %r %d)r%   z	skip  anc prevsibl %s %rz	write anc prevsibl %s %r 0x%xnsmapz!	enter norm %s %r 0x%x method: %rz	skip  cloth prevsibl %s %rz	write cloth prevsibl %s %rc                    s"   g | ]\}}| j vr||fqS r   )rB   )r   r   r   rN   r   r    
<listcomp>  s    
z-ToClothMixin._enter_cloth.<locals>.<listcomp>before_entrymethod ))rm   rn   tagr   r   outprot_ctxdoctype_writtenwrite_doctypeprotocoltags	rootstackr,   r	   eltstackctxstackbackrl   getroottreerd   r   _close_cloth_revancestorspopevent_manager
fire_event__exit__tailwriter+   r3   r&   r$   element	__enter__r.   r   r<   r   dictr   update)rN   r   rX   parentr   skipr   	skip_duper   r   r   r   cureltstackcurctxstack
cloth_rootlast_elt	ancestorsr   elt_ctxsiblanc	prevsiblskwargsanc_ctxcurtagcloth_attribr   r   r    _enter_cloth  s   























zToClothMixin._enter_clothc           
      C   s  |j j}|j}|j j| }|j j| }ttt||D ]W\}}|d urG| j	d|f|| |
d d d  td|j |jd urG||j |jddD ]}	td|	j ||	 |	jd urf||	j qM||u rstd|  nq|j j|= |j j|= t|dkr|  d S d S )Nr   zexit %s closeFr'   zwrite %s nextsiblzclosed until %r, breaking outr   )r   r   r   r   r   r   r   zipr   r   r   rm   rn   r   r   r   r+   rl   r   )
rN   r   r   r   close_untilr   r   r   r   r   r   r   r    r     s4   




zToClothMixin._close_clothc                 k   s    |  |}	|	d urtd| |	}|j|  j| | |||||d}
t|
rbz
	 d V }|
| q+ t	ya } z"z|

| W n t	ttfyN   Y nw W Y d }~d S W Y d }~d S d }~ww d S )Nz%r to object clothr   )get_class_clothrm   rn   r   r   r   to_clothr   sendr   throwStopIterationGeneratorExit)rN   r   rW   r   rX   r   namefrom_arrr   	cls_clothretsv2r   r   r   r    to_parent_cloth  s0   

zToClothMixin.to_parent_clothc                 c   s    t |jjdkr|j|  j| | |}| j||||jd | |||||}t	|rgz
	 d V }	|
|	 q0 tyf }
 z"z||
 W n tttfyS   Y nw W Y d }
~
d S W Y d }
~
d S d }
~
ww d S )Nr   r   )rl   r   r   r   r   get_cls_attrsr   r   start_to_parentr   r   r   r   r   r   )rN   r   rW   r   rX   r   r   	cls_attrsr   r   r   r   r   r    to_root_cloth  s.   

zToClothMixin.to_root_clothc
                 k   s   | j j}t|tr|j}d}n
t|tr|j}d}	d}|d u r4td | j|||||fi |
}n| 	||\}}| 
|}| ||}|j}|d u rUt|rU| }|d u r\|j}|j}|d ur|| ur|rmJ d| j||||j|	d |j|||||f||	d|
}nd }|d u r|jdkri }|rd||< | j|||||jd	 d
|df }td|||  || ntd||  | j|||d|jd np|	r| ||}|d ur|| n^|r|j}|d u r|}|| ||i}| j|||||jd	 n>d}tdtd| || |jj|||f |s2|jjdkr2| j|||||||d}n| j | }||||||||d}t!|rz	 d V }|"| qI t#y } zIz+z|$| W n t#t%t&fyp   Y nw W |rtdt'd| || |jj(  n|rtdt'd| || |jj(  w w W Y d }~d S d }~ww |rtdt'd| || |jj(  d S d S )NTFz*No cloth fround, switching to to_parent...zNo subprot supported for fields to be serialized as attributes, use type casting with customized serializers in the current protocol instead.)r   r   )as_attras_datar   r   r   r   z%s.%snull_to_clothzWriting '%s' using %s type: %s.z%Skipping '%s' type: %s because empty.)r   r   z%s %r pushed %r %r#rs   )r   r   )r   r   z%s %r popped %r %r))r   rx   
issubclassr   typer   rm   rn   	to_parentget_polymorphic_targetr   	_sanitizedefault_factorycallabledefaultprotr   r   subserialize
min_occurslogger_sr   r   
to_unicodesub_namer   r   
inst_stackr   r   
max_occursarray_to_clothr   r   r   r   r   r   r   r   r   )rN   r   rW   r   rX   r   r   r   r   r   r   	prot_namepushedr   _r   _dfsubprotattrs
identifierr  handlerr   r   r   r   r    r     s   







	

zToClothMixin.to_clothc           
      K   sd   |  |}| j||||jd | j|jv r'td| |D ]}	||	 qd S || || d S )Nr   zWriting contents for %r)	r   r   r   rA   r   rm   rn   r   r  )
rN   r   rW   r   rX   r   r   r   r   cr   r   r    r     s   
z ToClothMixin.model_base_to_clothc           	      K   @   |  |}| j||||jd t|trt|}|| d S Nr   )r   r   r   r,   r
   r   
fromstringr   	rN   r   rW   r   rX   r   r   r  r   r   r   r    r     
   


zToClothMixin.xml_to_clothc           	      K   s,   |  |}| j||||jd || d S r  )r   r   r   r   r  r   r   r    r     s   
zToClothMixin.any_to_clothc           	      K   r  r  )r   r   r   r,   r
   r   r  r   r  r   r   r    r     r  zToClothMixin.html_to_clothc           	      K   s<   |  |}| j||||jd | j|||||fi | d S r  )r   r   r   any_uri_to_parent)	rN   r   rW   r   rX   r   r   r   r   r   r   r    r     s   
zToClothMixin.any_uri_to_clothc                 k   sp   | |}	| |}
t|	 }t }| ||	}| j|||||
jd | || j	D ]
}| 
|||| q,| |rJtd||j | |}ntd||j | |}|j| jd }|d urv| j||||||	||d||fi | |D ]}| jddf| jddf| jddffD ]\}}}|j|d }|d u rq|j| jkrd}| j||||||	|f||||d|}t|r
z
	 d V }|| q ty	 } z5zz|| W n	 ty   Y nw W |s|sW Y d }~ n%n|s|s   Y W Y d }~ nw W Y d }~qd }~ww |s|s nqqxt|dkr%tdt|  t|dkr6td	t| d S d S )
Nr   z%r(%r) IS a tagbagz%r(%r) is NOT a tagbagTF)r   r   	fti_check	elt_checkr   z-No element found for the following fields: %rz5No field found for element the following elements: %r) get_flat_type_infor   r   r   r   _gen_attrib_dictr   r   r   r:   r   r   rm   rn   r   r   getr=   _process_fieldr;   r>   r   r<   r   r   r   r   r   rl   r
  r   keys)rN   r   rW   r   rX   r   r   r   r   ftir   r  r   r   r   rq   as_data_fieldk_attrr   
field_namer   r   r   r   r   r    r     s   










zToClothMixin.complex_to_clothc                 K   s   | |d }|
|d  |d u r'td|| || | j|||dd d S | |}|jr7td| d S |j	}|d u r@|}t
|trH|}nt||d }|	r\| j|||dd|jd | j|||||f|||	d|S )Nzelt id %r not in %rTr   z*Skipping elt id %r because it was excluded)r   r   r   )r   r   r   )r#  r   rm   r   r   r   r   excrn   r  r   r   getattrr   r   )rN   r   rW   r   r   r   r&  r)  r   r   r  r   r   
field_typer   r  valr   r   r    r$    s8   


zToClothMixin._process_fieldc                 k   sR   t |trM	 d V }| j|||||f|dd|}	t|	rLz
	 d V }
|	|
 q  tyK } zz|	| W n	 ty@   Y nw W Y d }~nd }~ww qt}|D ]F}d}| j|||||fd|d|}	t|	rz
	 d V }
|	|
 qk ty } zz|	| W n	 ty   Y nw W Y d }~qQd }~ww qQ|tu r| j	|||dd d S d S )NT)r   r   F)r   r   r*  )
r,   r   r   r   r   r   r   r   r"   r   )rN   r   rW   r   rX   r   r   r   svr   r   r   	was_emptyr   r   r    r  7  sd   


zToClothMixin.array_to_cloth)NNFFTr   )F)NFFF)NF)rx   ry   rz   r   r   r   r   r   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$  r  __classcell__r   r   r   r    r}      sN    



 3
 
I%r}   r   )r2   r3   r4   r4   r5   r6   r7   )1
__future__r   logging	getLoggerrx   rm   r
  lxmlr   r   copyr   inspectr   
spyne.utilr   r   spyne.util.osetr	   spyne.util.sixr
   spyne.util.colorr   r   spyne.modelr   r   r   r   r   r   r   r   r   r   spyne.protocolr   spyne.util.cdictr   r   r   objectr"   r&   r#   rL   rM   r8   r}   r   r   r   r    <module>   s2   0

X