a
    GBìjó  ã                   @   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   ú`/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/openpyxl/worksheet/datavalidation.pyÚ	<genexpr>(   ó    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   r   z%expand_cell_ranges.<locals>.<genexpr>c                 s   s   | ]}t |Ž V  qd S r   r   )r   r'   r   r   r   r   C   r   )ÚsplitÚsetr   )Zrange_stringÚrowsr%   r   r   r   Úexpand_cell_ranges;   s    r0   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)r8   r5   )ZwholeÚdecimalr   ÚdateÚtimeÚ
textLengthZcustom)Úvalues)ÚstopÚwarningÚ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 ur0|}|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _d S r   )r6   r9   ÚimeModeÚoperatorÚformula1Úformula2r:   ÚshowErrorMessageÚshowInputMessagerG   ÚpromptTitleÚ
errorStyleÚerrorÚpromptÚ
errorTitle)ÚselfrG   rJ   rK   rL   rM   r9   r:   r6   rN   rO   rP   rQ   rR   rH   rI   Ú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©ÚhasattrrV   r6   ©rS   r   r   r   r   Úadd�   s    
zDataValidation.addc                 C   s   t |dƒr|j}|| jv S )NrV   rW   rY   r   r   r   Ú__contains__–   s    
zDataValidation.__contains__)NNNFFFFr   NNNNNNNN)#Ú__name__Ú
__module__Ú__qualname__Útagnamer   r1   r6   r   r%   r&   r   r9   Zhide_drop_downrM   rL   r:   rT   r	   rR   rP   rN   rQ   r   ÚstrrJ   rK   r   rG   rO   rH   rI   Zvalidation_typerU   rZ   r[   r   r   r   r   r2   J   sP   












                ð
%r2   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dataValidationsTr7   r4   )r3   )ÚdisablePromptsÚxWindowÚyWindowÚcountNr   c                 C   s   || _ || _|| _|| _d S r   )rb   rc   rd   r3   )rS   rb   rc   rd   re   r3   r   r   r   rU   ¨   s    zDataValidationList.__init__c                 C   s   t | ƒS r   )r    ©rS   r   r   r   re   µ   s    zDataValidationList.countc                 C   s
   t | jƒS r   )r    r3   rf   r   r   r   Ú__len__º   s    zDataValidationList.__len__c                 C   s   | j  |¡ d S r   )r3   r   )rS   Zdvr   r   r   r   ¾   s    zDataValidationList.appendc                    s.   | j }dd„ | j D ƒ| _ tƒ  |¡}|| _ |S )zC
        Need to skip validations that have no cell ranges
        c                 S   s   g | ]}t |jƒr|‘qS r   )Úboolr6   )r   r*   r   r   r   Ú
<listcomp>Ç   r   z.DataValidationList.to_tree.<locals>.<listcomp>)r3   ÚsuperÚto_tree)rS   r_   r&   Úxml©Ú	__class__r   r   rk   Â   s
    zDataValidationList.to_tree)NNNNr   )N)r\   r]   r^   r_   r   rb   r   rc   rd   r
   r2   r3   Z__elements__Ú	__attrs__rU   Úpropertyre   rg   r   rk   Ú__classcell__r   r   rm   r   ra   œ   s$   



     û

ra   N)r   )Úcollectionsr   Ú	itertoolsr   rI   r   Z!openpyxl.descriptors.serialisabler   Zopenpyxl.descriptorsr   r   r	   r
   r   r   r   Zopenpyxl.descriptors.nestedr   Zopenpyxl.utilsr   r   r   r+   r0   Z
cell_ranger1   r2   ra   r   r   r   r   Ú<module>   s   $	
!R