o
    gc                     @   s   d 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mZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ g d
ZG dd deZG dd de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S )a@  
zeep.xsd.elements.indicators
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Indicators are a collection of elements. There are four available, these are
All, Choice, Group and Sequence.

    Indicator -> OrderIndicator -> All
                                -> Choice
                                -> Sequence
              -> Group

    N)OrderedDictdefaultdictdeque)cached_property)UnexpectedElementErrorValidationError)NotSet	SkipValue)AnyElement)Base)NamePrefixGeneratorUniqueNameGeneratorcreate_prefixed_namemax_occurs_iter)AllChoiceGroupSequencec                       sB   e Zd ZdZ fddZedd Zedd Zdd	d
Z  Z	S )	Indicatorz#Base class for the other indicatorsc                    s   d| j jt  f S )Nz<%s(%s)>)	__class____name__super__repr__selfr    U/home/ubuntu/webapp/venv/lib/python3.10/site-packages/zeep/xsd/elements/indicators.pyr   %   s   zIndicator.__repr__c                 C   s&   t dd | jD }| jrd|iS |S )Nc                 S   s   g | ]	\}}||j fqS r   )default_value).0nameelementr   r   r   
<listcomp>+   s    z+Indicator.default_value.<locals>.<listcomp>_value_1)r   elementsaccepts_multipler   valuesr   r   r   r   (   s   zIndicator.default_valuec                 C      t  NNotImplementedErrorr   r   r   r   r%   2      zIndicator.elements   c                 C   r)   r*   r+   r   r!   
min_occurs
max_occursr   r   r   clone6   s   zIndicator.cloner.   r.   )
r   
__module____qualname____doc__r   propertyr   r%   r2   __classcell__r   r   r   r   r   "   s    
	
r   c                       s   e Zd ZdZdZd fdd	ZdddZedd	 Zed
d Z	dd Z
dddZdd Zdd Zdd Zdd ZdddZ  ZS ) OrderIndicatorz0Base class for All, Choice and Sequence classes.Nr.   c                    s0   || _ || _t   |d ur| | d S d S r*   )r0   r1   r   __init__extend)r   r%   r0   r1   r   r   r   r:   ?   s   
zOrderIndicator.__init__c                 C   s   | j t| ||dS )N)r%   r0   r1   )r   listr/   r   r   r   r2   F      
zOrderIndicator.clonec                 C   s<   g }| j D ]\}}|du r||j q|||f q|S z:List of tuples containing the element name and the elementN)elements_nestedr;   r%   append)r   resultr!   elmr   r   r   r%   K   s   zOrderIndicator.elementsc                 C   s   g }t  }t }| D ]K}t|ttttfr7|jr"||	 |f q
|j
D ]	\}}||}q%|d|f q
t|ttfrH||	 |f q
||j}|||f q
|S r>   )r   r   
isinstancer   r   r   r   r&   r@   get_namer%   create_namer
   	attr_name)r   rA   	generatorgenerator_2rB   sub_namesub_elmr!   r   r   r   r?   V   s   zOrderIndicator.elements_nestedc                 C   s|   | j s|g}t }|D ].}d}| jD ]!\}}t|tr,|j|v r+||j dur+|d7 }q|||7 }q|| qt|S )Return the number of values which are accepted by this choice.

        If not all required elements are available then 0 is returned.

        r   Nr.   )	r&   setr?   rC   r   r!   acceptaddmax)r   r(   resultsvaluenumr!   r"   r   r   r   rM   l   s   
zOrderIndicator.acceptr   c                 C   sd   | j D ]\}}t|trtdqi }| jD ]\}}|t|kr" n|| ||< |d7 }q|||fS )N0Choice elements only work with keyword argumentsr.   )r?   rC   r   	TypeErrorr%   len)r   argsindexr!   rB   rA   r"   r   r   r   
parse_args   s   


zOrderIndicator.parse_argsc              	   C   s@  | j r|sJ |r||vri S | j sJ ||}t|ts!|g}g }t| j|D ]D}zt| }W n ty>   t	dw t
 }| jD ]\}}	|	|||}
|
durY||
 qE|rht	d| t|d f || q)||i}ttd|s~|| |S | j rJ t
 }| jD ]\}}	|	|||}|r|| q|S )a  Apply the given kwarg to the element.

        The available_kwargs is modified in-place. Returns a dict with the
        result.

        :param kwargs: The kwargs
        :type kwargs: dict
        :param name: The name as which this type is registered in the parent
        :type name: str
        :param available_kwargs: The kwargs keys which are still available,
         modified in place
        :type available_kwargs: set
        :rtype: dict

        z3A list of dicts is expected for unbounded SequencesN+%s() got an unexpected keyword argument %r.r   )r&   getrC   r<   r   r1   rL   keysAttributeErrorrT   r   r%   parse_kwargsupdater@   anyfilterremover?   )r   kwargsr!   available_kwargsitem_kwargsrA   
item_value	subresult	item_namer"   rQ   elm_name
sub_resultr   r   r   r]      sV   






zOrderIndicator.parse_kwargsc                 C   s"   t | D ]
\}}| | |< q| S r*   )	enumerateresolve)r   irB   r   r   r   rk      s   zOrderIndicator.resolvec           	      C   s   t |ts	|g}n|}| || t| j|D ]6}| jD ]0\}}|r5||v r0|| }||g }n	t}|}n|}|}|tu r>q|dusE|jsL|	||| qqdS )z.Create subelements in the given parent object.N)
rC   r<   validater   r1   r?   r   r	   is_optionalrender)	r   parentrQ   render_pathr(   r!   r"   element_value
child_pathr   r   r   ro      s*   
zOrderIndicator.renderc                 C   s"   |D ]}|t u rtd|dqd S )NzNo value setpath)r   r   )r   rQ   rq   itemr   r   r   rm      s
   zOrderIndicator.validateTc                 C   sr   g }| j D ]$\}}t|tr||j|dd q|j|dd}|d||f  qd|}| jr7d|f S |S )NF
standalonez%s: %sz, z[%s])r?   rC   r   r@   	signaturejoinr&   )r   schemarx   partsr!   r"   rQ   partr   r   r   ry      s   


zOrderIndicator.signature)Nr.   r.   r3   r   NT)r   r4   r5   r6   r!   r:   r2   threaded_cached_propertyr%   r?   rM   rX   r]   rk   ro   rm   ry   r8   r   r   r   r   r9   :   s     




Gr9   c                       s,   e Zd ZdZd	 fdd	Zd
ddZ  ZS )r   zlAllows the elements in the group to appear (or not appear) in any order
    in the containing element.

    Nr.   Fc                    s   t  ||| || _d S r*   )r   r:   _consume_other)r   r%   r0   r1   consume_otherr   r   r   r:     s   
zAll.__init__c                 C   s   t  }dd | jD }t }tt}t|D ]\}	}
|
j|v r,||	 ||
j |
 qt	|ddD ]}	||	= q3| jD ]\}}|
|j}|rR|j|||d||< q<| jrb|rbt||d< |  |S )  Consume matching xmlelements

        :param xmlelements: Dequeue of XML element objects
        :type xmlelements: collections.deque of lxml.etree._Element
        :param schema: The parent XML schema
        :type schema: zeep.xsd.Schema
        :param name: The name of the parent element
        :type name: str
        :param context: Optional parsing context (for inline schemas)
        :type context: zeep.xsd.context.XmlParserContext
        :rtype: dict or None

        c                 S   s   h | ]\}}|j qS r   )qname)r    __r"   r   r   r   	<setcomp>)  s    z(All.parse_xmlelements.<locals>.<setcomp>T)reversecontext_raw_elements)r   r%   rL   r   r   rj   tagrN   r@   sortedrZ   r   parse_xmlelementsr   r<   clear)r   xmlelementsr{   r!   r   rA   expected_tagsconsumed_tagsr(   rl   rB   r"   sub_elementsr   r   r   r     s,   



zAll.parse_xmlelements)Nr.   r.   FNN)r   r4   r5   r6   r:   r   r8   r   r   r   r   r     s    r   c                   @   sn   e Zd ZdZdddZedd Zedd Zdd
dZdd Z	dd Z
dd Zdd Zdd ZdddZd	S )r   z@Permits one and only one of the elements contained in the group.r   c                 C   s   |rt dd S )NrS   )rT   r   rV   rW   r   r   r   rX   F  s   zChoice.parse_argsc                 C   s   dS r   r   r   r   r   r   rn   J  s   zChoice.is_optionalc                 C   s   t  S r*   )r   r   r   r   r   r   N  r-   zChoice.default_valueNc              
   C   s  g }t | jD ]o}|s njg }| jD ]8\}}	t|}
z|	j|
|||d}W n	 ty/   Y qw t|	tr9||i}t|t|
 }|rJ|	||f q|sQg } n&t
|tddd}|rv|	|d d  t|d d D ]}|  qnq | jr||i}|S |r|d ni }|S )r   )r   r{   r!   r   r   Tkeyr   r.   )r   r1   r?   copyr   r   rC   r   rU   r@   r   operator
itemgetterrangepopleftr&   )r   r   r{   r!   r   rA   _unusedoptionselement_namer"   local_xmlelementsri   num_consumedrl   r   r   r   r   R  sL   



zChoice.parse_xmlelementsc                 C   s  |r}||v r}| j sJ || pg }|| g }t|tr |g}|D ]N}| D ]?}t|trD||v r5|| n|}||rC||  n-q&t|trP||  n |j|v re|	|j}||j|i  nq&t
d||  f q"| j s||rz|d nd}nQ| j ri S i }d}	| jD ]1\}}
t|}|
|||}|rt| s|| || q|	s|| || d}	q|	r| jD ]
\}}
||d qni }|r| j r||i}|S )a  Processes the kwargs for this choice element.

        Returns a dict containing the values found.

        This handles two distinct initialization methods:

        1. Passing the choice elements directly to the kwargs (unnested)
        2. Passing the choice elements into the `name` kwarg (_value_1) (nested).
           This case is required when multiple choice elements are given.

        :param name: Name of the choice element (_value_1)
        :type name: str
        :param element: Choice element object
        :type element: zeep.xsd.Choice
        :param kwargs: dict (or list of dicts) of kwargs for initialization
        :type kwargs: list / dict

        zJNo complete xsd:Sequence found for the xsd:Choice %r.
The signature is: %sr   NFT)r&   ra   rC   dictr9   rM   r@   r
   r!   rZ   rT   ry   r?   r   r]   r_   r(   intersection_updater^   r%   
setdefault)r   rb   r!   rc   r(   rA   rQ   r"   choice_valuefoundchoicetemp_kwargsrf   choice_namer   r   r   r]     sp   














zChoice.parse_kwargsc                 C   sJ   | j s|g}| || |D ]}| |}|r"|\}}|||| qdS )zRender the value to the parent element tree node.

        This is a bit more complex then the order render methods since we need
        to search for the best matching choice element.

        N)r&   rm   _find_element_to_renderro   )r   rp   rQ   rq   rv   rA   r"   r   r   r   r   ro     s   
zChoice.renderc                 C   sB   d}|D ]}|  |}|r|d7 }q|s| jstd|dd S d S )Nr   r.   zMissing choice valuesrt   )r   rn   r   )r   rQ   rq   r   rv   rA   r   r   r   rm     s   

zChoice.validatec                    s   t  }| jD ]5\ }t|tr1| jr#t fdd|D r"|d q |v r0|  r0|d q||}|| q|rBt|S dS )rK   c                 3   s     | ]} |v o|  V  qd S r*   r   )r    rv   r!   r   r   	<genexpr>
  s    z Choice.accept.<locals>.<genexpr>r.   r   )	rL   r?   rC   r   r&   allrN   rM   rO   )r   r(   numsr"   rR   r   r   r   rM      s   



zChoice.acceptc              
   C   s   g }| j D ]W\}}t|tr3|j|v r2z||j }W n ty%   |}Y nw |dur2|d||f q|durKz|| }W n ttfyJ   |}Y nw |}||}|r\||||f q|rqt|t	
ddd}|d dd S dS )zReturn a tuple (element, value) for the best matching choice.

        This is used to decide which choice child is best suitable for
        rendering the available data.

        Nr.   r   Tr   )r?   rC   r   r!   KeyErrorr@   rT   rM   r   r   r   )r   rQ   matchesr!   r"   r   scorer   r   r   r     s6   


zChoice._find_element_to_renderTc              	   C   sv   g }| j D ]$\}}t|tr|d|j|dd  q|d||j|ddf  qdd| }| jr9d|f S |S )Nz{%s}Frw   z{%s: %s}z(%s)z | z%s[])r?   rC   r9   r@   ry   rz   r&   )r   r{   rx   r|   r!   r"   r}   r   r   r   ry   8  s   

zChoice.signaturer~   r   r   )r   r4   r5   r6   rX   r7   rn   r   r   r]   ro   rm   rM   r   ry   r   r   r   r   r   C  s    



>T
$r   c                   @   s   e Zd ZdZdddZdS )r   zoRequires the elements in the group to appear in the specified sequence
    within the containing element.

    Nc              
   C   s   g }| j r	|s	J t| jD ]G}|s nBt }| jD ]3\}}	z|	j||||d}
W n ty9   |jjr5 d}
Y nw t	|	t
rE||
 n|
||< |sM nq|rU|| q| j sa|r_|d S dS ||iS )r   r   Nr   )r&   r   r1   r   r%   r   r   settingsstrictrC   r9   r^   r@   )r   r   r{   r!   r   rA   r   item_resultrh   r"   item_subresultr   r   r   r   M  s:   


zSequence.parse_xmlelementsr   )r   r4   r5   r6   r   r   r   r   r   r   G  s    r   c                       s   e Zd ZdZd fdd	Zdd Zdd Zed	d
 ZdddZ	dd Z
dddZdd Zd ddZdd Zdd Zd!ddZ  ZS )"r   zyGroups a set of element declarations so that they can be incorporated as
    a group into complex type definitions.

    r.   c                    s6   t    || _|| _|r|jnd | _|| _|| _d S r*   )r   r:   childr   	localnamer!   r1   r0   )r   r!   r   r1   r0   r   r   r   r:     s   

zGroup.__init__c                 C   s   |   S r*   )ry   r   r   r   r   __str__  s   zGroup.__str__c                 o   s    | j E d H  d S r*   )r   )r   rV   rb   r   r   r   __iter__  s   zGroup.__iter__c                 C   s   | j r	d| jfgS | jjS )Nr$   )r&   r   r%   r   r   r   r   r%     s   zGroup.elementsc                 C   s   | j d | j||dS )N)r!   r   r0   r1   )r   r   r/   r   r   r   r2     r=   zGroup.clonec                 C   s   | j |S )rK   )r   rM   r'   r   r   r   rM     s   zGroup.acceptr   c                 C   s   | j ||S r*   )r   rX   r   r   r   r   rX     s   zGroup.parse_argsc           
      C   s   | j rP||vr	i S || || }g }| jj rdnd }t| j|D ]%}t| }| j|||}	|r@td| t	|d f |	rG|
|	 q"|rN||i}|S | j|||}|S )Nr$   rY   r   )r&   ra   r   r   r1   rL   r[   r]   rT   r<   r@   )
r   rb   r!   rc   rd   rA   rI   
sub_kwargsavailable_sub_kwargsrf   r   r   r   r]     s4   

zGroup.parse_kwargsNc              	   C   sP   g }t | jD ]}|| jj||||d |s nq| js$|r$|d S ||iS )r   r   r   )r   r1   r@   r   r   r&   )r   r   r{   r!   r   rA   r   r   r   r   r     s   
zGroup.parse_xmlelementsc                 C   s4   t |ts	|g}n|}|D ]
}| j||| qd S r*   )rC   r<   r   ro   )r   rp   rQ   rq   r(   r   r   r   ro     s   
zGroup.renderc                 C   s   | j  | _ | S r*   )r   rk   r   r   r   r   rk     s   zGroup.resolveTc                 C   s8   t | j|}|rd|| jj|ddf S | jj|ddS )Nz%s(%s)Frw   )r   r   r   ry   )r   r{   rx   r!   r   r   r   ry     s   zGroup.signaturer3   r~   r   r   )r   r4   r5   r6   r:   r   r   r   r%   r2   rM   rX   r]   r   ro   rk   ry   r8   r   r   r   r   r     s    



	r   )"r6   r   r   collectionsr   r   r   	functoolsr   r   zeep.exceptionsr   r   zeep.xsd.constr   r	   zeep.xsd.elementsr
   r   zeep.xsd.elements.baser   zeep.xsd.utilsr   r   r   r   __all__r   r<   r9   r   r   r   r   r   r   r   r   <module>   s(     W3  8