U
    rÎ·h  ã                   @   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 ƒ}t|tdƒd�D ]\}}||  |¡ q.| ¡ D ]F\}} t|ƒ}d}t| ƒdkrrd}| |t	| ƒ|t
| ƒ¡}| |¡ qNd |¡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-49e_hfpc/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 r   )r   )r   Úrsr   r   r   r   B   s     z%expand_cell_ranges.<locals>.<genexpr>c                 s   s   | ]}t |Ž V  qd S r   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
dd�ZedƒZe
dd�Ze
dd�Ze
dd�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)r6   r3   )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 r   )r4   r7   ÚimeModeÚoperatorÚformula1Úformula2r8   ÚshowErrorMessageÚshowInputMessagerD   ÚpromptTitleÚ
errorStyleÚerrorÚpromptÚ
errorTitle)ÚselfrD   rG   rH   rI   rJ   r7   r8   r4   rK   rL   rM   rN   rO   rE   rF   Úallow_blankr   r   r   Ú__init__j   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©ÚhasattrrS   r4   ©rP   r   r   r   r   Úadd�   s    
zDataValidation.addc                 C   s   t |dƒr|j}|| jkS )NrS   rT   rV   r   r   r   Ú__contains__–   s    
zDataValidation.__contains__)NNNFFFFr   NNNNNNNF)#Ú__name__Ú
__module__Ú__qualname__Útagnamer   r/   r4   r   r$   r%   r   r7   Zhide_drop_downrJ   rI   r8   rQ   r	   rO   rM   rK   rN   r   ÚstrrG   rH   r   rD   rL   rE   rF   Zvalidation_typerR   rW   rX   r   r   r   r   r0   J   sP   












                ð
%r0   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dataValidationsTr5   r2   )r1   )ÚdisablePromptsÚxWindowÚyWindowÚcountNr   c                 C   s   || _ || _|| _|| _d S r   )r_   r`   ra   r1   )rP   r_   r`   ra   rb   r1   r   r   r   rR   ¨   s    zDataValidationList.__init__c                 C   s   t | ƒS r   )r   ©rP   r   r   r   rb   µ   s    zDataValidationList.countc                 C   s
   t | jƒS r   )r   r1   rc   r   r   r   Ú__len__º   s    zDataValidationList.__len__c                 C   s   | j  |¡ d S r   )r1   r   )rP   Ú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   )Úboolr4   )r   r)   r   r   r   Ú
<listcomp>Ç   s     
 z.DataValidationList.to_tree.<locals>.<listcomp>)r1   Úsuperr^   Úto_tree)rP   r\   r%   Úxml©Ú	__class__r   r   ri   Â   s
    zDataValidationList.to_tree)NNNNr   )N)rY   rZ   r[   r\   r   r_   r   r`   ra   r
   r0   r1   Z__elements__Ú	__attrs__rR   Úpropertyrb   rd   r   ri   Ú__classcell__r   r   rk   r   r^   œ   s$   



     û

r^   N)r   )Úcollectionsr   Ú	itertoolsr   rF   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/   r0   r^   r   r   r   r   Ú<module>   s   $	
!R