B
    ÔùÓ`ü  ã               @   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dS )é    )Úsafe_string)ÚElement)ÚIndexedListé   )Ú
DescriptorÚAliasÚ_convert)Ú
namespacedc                   sB   e Zd ZdZedƒZeefZdZ	d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                sJ   t |ˆ jƒstdƒ‚‡ fdd„|D ƒ}ˆ jr4t|ƒ}ttˆ ƒ ||¡ d S )NzValue must be a sequencec                s   g | ]}t ˆ j|ƒ‘qS © )r   Úexpected_type)Ú.0Úvalue)Úselfr   úA/tmp/pip-unpacked-wheel-zhp1095k/openpyxl/descriptors/sequence.pyú
<listcomp>   s    z$Sequence.__set__.<locals>.<listcomp>)Ú
isinstanceÚ	seq_typesÚ	TypeErrorÚuniquer   Úsuperr
   Ú__set__)r   ÚinstanceÚseq)Ú	__class__)r   r   r      s    zSequence.__set__c             c   sZ   xTt || jƒD ]D\}}t|dƒr.| ||¡}nt|||ƒ}t|ƒ}t|ƒ|_|V  qW dS )zV
        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   Ú__classcell__r   r   )r   r   r
      s   
r
   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   s4   t | ||ƒ}x"|D ]}t|| jt|ƒiƒV  qW d S )N)r	   r   Ú	attributer   )r   r    r!   r"   r$   r   r   r   r   8   s    
zValueSequence.to_treec             C   s   |  | j¡S )N)Úgetr0   )r   Únoder   r   r   Ú	from_tree>   s    zValueSequence.from_tree)N)r&   r'   r(   r)   r0   r   r3   r   r   r   r   r.   /   s   
r.   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   sN   t | ||ƒ}t|ƒ}| jr.| dtt|ƒƒ¡ x|D ]}| | ¡ ¡ q4W |S )NÚcount)r	   r   r5   ÚsetÚstrÚlenÚappendr   )r   r    r!   r"   Ú	containerr$   r   r   r   r   J   s    
zNestedSequence.to_treec                s   ‡ fdd„|D ƒS )Nc                s   g | ]}ˆ j  |¡‘qS r   )r   r3   )r   r%   )r   r   r   r   U   s    z,NestedSequence.from_tree.<locals>.<listcomp>r   )r   r2   r   )r   r   r3   T   s    zNestedSequence.from_tree)N)r&   r'   r(   r)   r5   r   r3   r   r   r   r   r4   C   s   

r4   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 )NzValue must be a sequence)r   r,   r+   Ú
ValueErrorr   r   )r   r   r   r   r   r   r   ]   s    zMultiSequence.__set__Nc             c   s$   x|D ]}|j |d�}|V  qW dS )zV
        Convert the sequence represented by the descriptor to an XML element
        )r"   N)r   )r   r    r!   r"   r$   r%   r   r   r   r   d   s    
zMultiSequence.to_tree)N)r&   r'   r(   r)   r   r   r   r   r   r   r;   X   s   r;   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 )N)r   Ústore)r   r   r>   r   r   r   Ú__init__t   s    zMultiSequencePart.__init__c             C   s"   t | j|ƒ}|j| j  |¡ d S )N)r   r   Ú__dict__r>   r9   )r   r   r   r   r   r   r   y   s    zMultiSequencePart.__set__c             C   s   | S )Nr   )r   r   Úclsr   r   r   Ú__get__~   s    zMultiSequencePart.__get__N)r&   r'   r(   r)   r?   r   rB   r   r   r   r   r=   m   s   r=   N)Zopenpyxl.compatr   Zopenpyxl.xml.functionsr   Zopenpyxl.utils.indexed_listr   Úbaser   r   r   r"   r	   r
   r.   r4   r;   r=   r   r   r   r   Ú<module>   s   $