B
    P\'                 @   s   d dl mZ 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 )    )absolute_import)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   <lib/python3.7/site-packages/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   9   s    
zValueSequence.to_treec             C   s   | | jS )N)getr1   )r   noder   r   r   	from_tree?   s    zValueSequence.from_tree)N)r'   r(   r)   r*   r1   r   r4   r   r   r   r   r/   0   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   r6   setstrlenappendr   )r   r!   r"   r#   Z	containerr%   r   r   r   r   K   s    
zNestedSequence.to_treec                s    fdd|D S )Nc                s   g | ]} j |qS r   )r   r4   )r   r&   )r   r   r   r   V   s    z,NestedSequence.from_tree.<locals>.<listcomp>r   )r   r3   r   )r   r   r4   U   s    zNestedSequence.from_tree)N)r'   r(   r)   r*   r6   r   r4   r   r   r   r   r5   D   s   

r5   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   e   s    
zMultiSequence.to_tree)N)r'   r(   r)   r*   r   r   r   r   r   r   r;   Y   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__u   s    zMultiSequencePart.__init__c             C   s"   t | j|}|j| j | d S )N)r	   r   __dict__r>   r:   )r   r   r   r   r   r   r   z   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=   n   s   r=   N)Z
__future__r   Zopenpyxl.compatr   Zopenpyxl.xml.functionsr   Zopenpyxl.utils.indexed_listr   baser   r   r	   r#   r
   r   r/   r5   r;   r=   r   r   r   r   <module>   s   $