B
    ÔùÓ`ù  ã               @   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mZmZmZmZ d dlmZ d dlmZmZmZ dd	d
„Zdd„ ZddlmZ G dd„ deƒZG dd„ deƒZdS )é    )Údefaultdict)Úchain)Ú
itemgetter)ÚSerialisable)ÚBoolÚNoneSetÚStringÚSequenceÚAliasÚIntegerÚConvertible)Ú
NestedText)Úrows_from_rangeÚcoordinate_to_tupleÚget_column_letter© c       	      C   s¨   t |ƒ}dd„ | D ƒ}tt ƒ}x*t|tdƒd�D ]\}}||  |¡ q0W xR| ¡ D ]F\}} t|ƒ}d}t| ƒdkrxd}| |t	| ƒ|t
| ƒ¡}| |¡ qTW d |¡S )ax   Collapse a collection of cell co-ordinates down into an optimal
        range or collection of ranges.

        E.g. Cells A1, A2, A3, B1, B2 and B3 should have the data-validation
        object applied, attempt to collapse down to a single range, A1:B3.

        Currently only collapsing contiguous vertical ranges (i.e. above
        example results in A1:A3 B1:B3).
    c             s   s   | ]}t |ƒV  qd S )N)r   )Ú.0Úcellr   r   úE/tmp/pip-unpacked-wheel-zhp1095k/openpyxl/worksheet/datavalidation.pyú	<genexpr>(   s    z*collapse_cell_addresses.<locals>.<genexpr>é   )Úkeyz{0}{1}:{2}{3}z{0}{1}ú )Úlistr   Úsortedr   ÚappendÚitemsr   ÚlenÚformatÚminÚmaxÚjoin)	ÚcellsZinput_rangesÚrangesZ
raw_coordsZgrouped_coordsÚrowÚcolÚfmtÚrr   r   r   Úcollapse_cell_addresses   s    r(   c             C   s,   dd„ |   ¡ D ƒ}dd„ |D ƒ}tt|Ž ƒS )z’
    Expand cell ranges to a sequence of addresses.
    Reverse of collapse_cell_addresses
    Eg. converts "A1:A2 B1:B2" to (A1, A2, B1, B2)
    c             s   s   | ]}t |ƒV  qd S )N)r   )r   Úrsr   r   r   r   B   s    z%expand_cell_ranges.<locals>.<genexpr>c             s   s   | ]}t |Ž V  qd S )N)r   )r   r$   r   r   r   r   C   s    )ÚsplitÚsetr   )Zrange_stringZrowsr"   r   r   r   Úexpand_cell_ranges;   s    r,   r   )ÚMultiCellRangec               @   sæ   e Zd ZdZeed�ZedƒZedƒZ	e
ƒ Ze
dd�ZedƒZe
ƒ Ze
ƒ Ze
ƒ ZedƒZedd�Zedd�Zedd�Zedd�Zeded�Zeded�Zed	d
�Zedd
�Zedd
�Zedd
�ZedƒZddd„Z dd„ Z!dd„ Z"dS )ÚDataValidationÚdataValidation)Úexpected_typeÚsqrefT)Ú
allow_noneÚshowDropDownÚ
allowBlank)r2   r0   )ZwholeÚdecimalr   ÚdateÚtimeÚ
textLengthZcustom)Úvalues)ÚstopÚwarningZinformation)Z	noControlÚoffÚonÚdisabledZhiraganaZfullKatakanaZhalfKatakanaZ	fullAlphaZ	halfAlphaZ
fullHangulZ
halfHangul)ZbetweenZ
notBetweenÚequalZnotEqualZlessThanZlessThanOrEqualZgreaterThanZgreaterThanOrEqualÚtypeNFr   c             C   sj   |	| _ || _|| _|| _|| _|| _|d k	r0|}|| _|| _|| _|| _	|
| _
|| _|| _|| _|| _d S )N)r1   r3   ÚimeModeÚoperatorÚformula1Úformula2r4   ÚshowErrorMessageÚshowInputMessager@   ÚpromptTitleÚ
errorStyleÚerrorÚpromptÚ
errorTitle)Úselfr@   rC   rD   Úallow_blankrE   rF   r3   r4   r1   rG   rH   rI   rJ   rK   rA   rB   r   r   r   Ú__init__k   s"    zDataValidation.__init__c             C   s"   t |dƒr|j}|  j|7  _dS )z0Adds a cell or cell coordinate to this validatorÚ
coordinateN)ÚhasattrrO   r1   )rL   r   r   r   r   Úadd�   s    
zDataValidation.addc             C   s   t |dƒr|j}|| jkS )NrO   )rP   rO   r1   )rL   r   r   r   r   Ú__contains__—   s    
zDataValidation.__contains__)NNNFTTNNr   NNNNNNN)#Ú__name__Ú
__module__Ú__qualname__Útagnamer   r-   r1   r
   r"   r#   r   rE   r3   Zhide_drop_downrF   r4   rM   r   rK   rI   rG   rJ   r   ÚstrrC   rD   r   r@   rH   rA   rB   Zvalidation_typerN   rQ   rR   r   r   r   r   r.   J   sP   









               
r.   c                   sx   e Zd ZdZedd�Zedd�Zedd�Ze	e
d�ZdZdZdd	d
„Zedd„ ƒZdd„ Zdd„ Zd‡ fdd„	Z‡  ZS )ÚDataValidationListZdataValidationsT)r2   )r0   )r/   )ÚdisablePromptsÚxWindowÚyWindowÚcountNr   c             C   s   || _ || _|| _|| _d S )N)rY   rZ   r[   r/   )rL   rY   rZ   r[   r\   r/   r   r   r   rN   ©   s    zDataValidationList.__init__c             C   s   t | ƒS )N)r   )rL   r   r   r   r\   ¶   s    zDataValidationList.countc             C   s
   t | jƒS )N)r   r/   )rL   r   r   r   Ú__len__»   s    zDataValidationList.__len__c             C   s   | j  |¡ d S )N)r/   r   )rL   Zdvr   r   r   r   ¿   s    zDataValidationList.appendc                s2   | j }dd„ | j D ƒ| _ tt| ƒ |¡}|| _ |S )zC
        Need to skip validations that have no cell ranges
        c             S   s   g | ]}t |jƒr|‘qS r   )Úboolr1   )r   r'   r   r   r   ú
<listcomp>È   s    z.DataValidationList.to_tree.<locals>.<listcomp>)r/   ÚsuperrX   Úto_tree)rL   rV   r#   Úxml)Ú	__class__r   r   ra   Ã   s
    zDataValidationList.to_tree)NNNNr   )N)rS   rT   rU   rV   r   rY   r   rZ   r[   r	   r.   r/   Z__elements__Ú	__attrs__rN   Úpropertyr\   r]   r   ra   Ú__classcell__r   r   )rc   r   rX   �   s    



    
rX   N)r   )Úcollectionsr   Ú	itertoolsr   rB   r   Z!openpyxl.descriptors.serialisabler   Zopenpyxl.descriptorsr   r   r   r	   r
   r   r   Zopenpyxl.descriptors.nestedr   Zopenpyxl.utilsr   r   r   r(   r,   Z
cell_ranger-   r.   rX   r   r   r   r   Ú<module>   s   $	
!S