U
    d                     @   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-rwywncvq/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   %