U
    rÎ·h¢  ã                   @   s¨   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 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G dd„ deƒZdS )é    )Úsafe_string)ÚElement)ÚIndexedListé   )Ú
DescriptorÚAliasÚ_convert)Ú
namespacedc                       sF   e Zd ZdZedƒZeefZdZ	dZ
eZ‡ fdd„Zd	dd„Z‡  ZS )
ÚSequencez[
    A sequence (list or tuple) that may only contain objects of the declared
    type
    Nr   Fc                    sL   t |ˆ jƒstdƒ‚ˆ  ‡ fdd„|D ƒ¡}ˆ jr:t|ƒ}tƒ  ||¡ d S )NúValue must be a sequencec                 3   s   | ]}t ˆ j|ƒV  qd S ©N)r   Úexpected_type)Ú.0Úvalue©Úself© úA/tmp/pip-unpacked-wheel-49e_hfpc/openpyxl/descriptors/sequence.pyÚ	<genexpr>   s     z#Sequence.__set__.<locals>.<genexpr>)Ú
isinstanceÚ	seq_typesÚ	TypeErrorÚ	containerÚuniquer   ÚsuperÚ__set__©r   ÚinstanceÚseq©Ú	__class__r   r   r      s    zSequence.__set__c                 c   sV   t || jƒD ]D\}}t|dƒr,| ||¡}nt|||ƒ}t|ƒ}t|ƒ|_|V  qdS )úV
        Convert the sequence represented by the descriptor to an XML element
        Úto_treeN)Ú	enumerateÚidx_baseÚhasattrr"   r	   r   r   Útext)r   ÚtagnameÚobjÚ	namespaceÚidxÚvÚelr   r   r   r"   "   s    

zSequence.to_tree)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Útyper   ÚlistÚtupler   r$   r   r   r   r"   Ú__classcell__r   r   r   r   r
      s   
r
   c                   @   s   e Zd ZdZeeefZeZdS )ÚUniqueSequencez)
    Use a set to keep values unique
    N)	r-   r.   r/   r0   r2   r3   Úsetr   r   r   r   r   r   r5   0   s   
r5   c                   @   s&   e Zd ZdZdZddd„Zdd„ ZdS )	ÚValueSequencezq
    A sequence of primitive types that are stored as a single attribute.
    "val" is the default attribute
    ÚvalNc                 c   s0   t | ||ƒ}|D ]}t|| jt|ƒiƒV  qd S r   )r	   r   Ú	attributer   )r   r'   r(   r)   r+   r   r   r   r"   A   s    zValueSequence.to_treec                 C   s   |  | j¡S r   )Úgetr9   ©r   Únoder   r   r   Ú	from_treeG   s    zValueSequence.from_tree)N)r-   r.   r/   r0   r9   r"   r=   r   r   r   r   r7   8   s   
r7   c                   @   s&   e Zd ZdZdZddd„Zdd„ ZdS )	ÚNestedSequencez1
    Wrap a sequence in an containing object
    FNc                 C   sJ   t | ||ƒ}t|ƒ}| jr.| dtt|ƒƒ¡ |D ]}| | ¡ ¡ q2|S )NÚcount)r	   r   r?   r6   ÚstrÚlenÚappendr"   )r   r'   r(   r)   r   r+   r   r   r   r"   S   s    zNestedSequence.to_treec                    s   ‡ fdd„|D ƒS )Nc                    s   g | ]}ˆ j  |¡‘qS r   )r   r=   )r   r,   r   r   r   Ú
<listcomp>^   s     z,NestedSequence.from_tree.<locals>.<listcomp>r   r;   r   r   r   r=   ]   s    zNestedSequence.from_tree)N)r-   r.   r/   r0   r?   r"   r=   r   r   r   r   r>   L   s   

r>   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )ÚMultiSequencez;
    Sequences can contain objects with different tags
    c                 C   s0   t |ttfƒstdƒ‚t|ƒ}t | ||¡ d S )Nr   )r   r3   r2   Ú
ValueErrorr   r   r   r   r   r   r   f   s    zMultiSequence.__set__Nc                 c   s    |D ]}|j |d�}|V  qdS )r!   )r)   N)r"   )r   r'   r(   r)   r+   r,   r   r   r   r"   m   s    zMultiSequence.to_tree)N)r-   r.   r/   r0   r   r"   r   r   r   r   rD   a   s   rD   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚMultiSequencePartzŽ
    Allow a multisequence to be built up from parts

    Excluded from the instance __elements__ or __attrs__ as is effectively an Alias
    c                 C   s   || _ || _d S r   )r   Ústore)r   r   rG   r   r   r   Ú__init__}   s    zMultiSequencePart.__init__c                 C   s"   t | j|ƒ}|j| j  |¡ d S r   )r   r   Ú__dict__rG   rB   )r   r   r   r   r   r   r   ‚   s    zMultiSequencePart.__set__c                 C   s   | S r   r   )r   r   Úclsr   r   r   Ú__get__‡   s    zMultiSequencePart.__get__N)r-   r.   r/   r0   rH   r   rK   r   r   r   r   rF   v   s   rF   N)Zopenpyxl.compatr   Zopenpyxl.xml.functionsr   Zopenpyxl.utils.indexed_listr   Úbaser   r   r   r)   r	   r
   r5   r7   r>   rD   rF   r   r   r   r   Ú<module>   s   %