U
    rÎ·hˆ  ã                   @   sþ   d Z ddlZddlmZ ddlmZ e d¡ZdZdZ	d	Z
e d
e
 d ej¡ZdZe d ee
¡ej¡Zdd„ Zdd„ Zdd„ Zdd„ Zi Zi ZeddƒD ]ZeeƒZeee< eee< qœdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Z dS )'zT
Collection of utilities used within the package and also available for client code
é    N)Údigitsé   )ÚCellCoordinatesExceptionz^[$]?([A-Za-z]{1,3})[$]?(\d+)$z[A-Z]{1,3}:[A-Z]{1,3}:z\d+:\d+:zq
[$]?(?P<min_col>[A-Za-z]{1,3})?
[$]?(?P<min_row>\d+)?
(:[$]?(?P<max_col>[A-Za-z]{1,3})?
[$]?(?P<max_row>\d+)?)?
ú^ú$z7
(('(?P<quoted>([^']|'')*)')|(?P<notquoted>[^'^ ^!]*))!z{0}(?P<cells>{1})(?=,?)c                 C   s<   t | tƒrt| ƒ} t |tƒr$t|ƒ}dd„ t| |d ƒD ƒS )z¤
    Given the start and end columns, return all the columns in the series.

    The start and end columns can be either column letters or 1-based
    indexes.
    c                 S   s   g | ]}t |ƒ‘qS © ©Úget_column_letter)Ú.0Úxr   r   ú7/tmp/pip-unpacked-wheel-49e_hfpc/openpyxl/utils/cell.pyÚ
<listcomp>'   s     z'get_column_interval.<locals>.<listcomp>r   )Ú
isinstanceÚstrÚcolumn_index_from_stringÚrange)ÚstartÚendr   r   r   Úget_column_interval   s
    

r   c                 C   sV   t  | ¡}|s"d| › d�}t|ƒ‚| ¡ \}}t|ƒ}|sNd| › d�}t|ƒ‚||fS )z;Convert a coordinate string like 'B12' to a tuple ('B', 12)zInvalid cell coordinates (ú)zThere is no row 0 ()ÚCOORD_REÚmatchr   ÚgroupsÚint)Úcoord_stringr   ÚmsgÚcolumnÚrowr   r   r   Úcoordinate_from_string*   s    
r   c                 C   sp   t  | ¡}|st| › d�ƒ‚| d¡}| ¡ D ]\}}|r.d|› �||< q.|d sZ|d r`d}nd}|jf |ŽS )zDConvert a coordinate to an absolute coordinate string (B12 -> $B$12)z  is not a valid coordinate rangeÚ r   Úmax_colÚmax_rowz%{min_col}{min_row}:{max_col}{max_row}z{min_col}{min_row})ÚABSOLUTE_REr   Ú
ValueErrorÚ	groupdictÚitemsÚformat)r   ÚmÚdÚkÚvÚfmtr   r   r   Úabsolute_coordinate8   s    

r,   c                 C   st   d|   krdks$n t d | ¡ƒ‚g }| dkrft| dƒ\} }|dkrRd}| d8 } | t|d ƒ¡ q(d t|ƒ¡S )zéConvert a column number into a column letter (3 -> 'C')

    Right shift the column col_idx by 26 to find column letters in reverse
    order.  These numbers are 1-based, and can be converted to ASCII
    ordinals by adding 64.

    r   ifG  úInvalid column index {0}r   é   é@   r   )r#   r&   ÚdivmodÚappendÚchrÚjoinÚreversed)Zcol_idxÚlettersÚ	remainderr   r   r   Ú_get_column_letterJ   s    
r7   igG  c                 C   s2   z
t |  W S  tk
r,   td | ¡ƒ‚Y nX dS )z?Convert a column index into a column letter
    (3 -> 'C')
    r-   N)Ú_STRING_COL_CACHEÚKeyErrorr#   r&   )Úidxr   r   r   r	   i   s    
r	   c                 C   s6   zt |  ¡  W S  tk
r0   td | ¡ƒ‚Y nX dS )z@Convert a column name into a numerical index
    ('A' -> 1)
    z{0} is not a valid column nameN)Ú_COL_STRING_CACHEÚupperr9   r#   r&   )Zstr_colr   r   r   r   s   s    r   c           
      C   sÒ   d  | ¡}t | ¡}|s t|ƒ‚| ¡ \}}}}}|rz||f}||f}	t||	 ƒszt|ƒrbt|	ƒrzt|	ƒrrt|ƒrzt|ƒ‚|dk	rŠt|ƒ}|dk	ršt|ƒ}|dk	r¬t|ƒ}n|}|dk	rÂt|ƒ}n|}||||fS )z³
    Convert a range string into a tuple of boundaries:
    (min_col, min_row, max_col, max_row)
    Cell coordinates will be converted into a range with the cell at both end
    z&{0} is not a valid coordinate or rangeN)	r&   r"   r   r#   r   ÚallÚanyr   r   )
Úrange_stringr   r'   Úmin_colÚmin_rowÚsepr    r!   ÚcolsÚrowsr   r   r   Úrange_boundaries~   s<    


ÿþþýý

rE   c                 #   s\   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}|D ]‰ t‡ fdd„|D ƒƒV  q:dS )ú[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c                 S   s   g | ]}t |ƒ‘qS r   r   ©r
   Úcolr   r   r   r   °   s     z#rows_from_range.<locals>.<listcomp>c                 3   s   | ]}d   |ˆ ¡V  qdS ©z{0}{1}N©r&   rG   ©r   r   r   Ú	<genexpr>²   s     z"rows_from_range.<locals>.<genexpr>N©rE   r   Útuple©r?   r@   rA   r    r!   rD   rC   r   rK   r   Úrows_from_range©   s
    rP   c                 #   s\   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}|D ]‰ t‡ fdd„|D ƒƒV  q:dS )rF   r   c                 s   s   | ]}t |ƒV  qd S )Nr   rG   r   r   r   rL   ¼   s     z"cols_from_range.<locals>.<genexpr>c                 3   s   | ]}d   ˆ |¡V  qdS rI   rJ   )r
   r   ©rH   r   r   rL   ¾   s     NrM   rO   r   rQ   r   Úcols_from_rangeµ   s
    rR   c                 C   sJ   t | ƒD ]\}}|tkr qq| d|…  ¡ }| |d… }t|ƒt| fS )zB
    Convert an Excel style coordinate to (row, column) tuple
    N)Ú	enumerater   r<   r   r;   )Z
coordinater:   ÚcrH   r   r   r   r   Úcoordinate_to_tupleÁ   s    rU   c                 C   sH   t  | ¡}|dkrtdƒ‚| d¡p,| d¡}| d¡}t|ƒ}||fS )zc
    Convert a worksheet range to the sheetname and maximum and minimum
    coordinate indices
    Nz)Value must be of the form sheetname!A1:E4ÚquotedZ	notquotedÚcells)ÚSHEETRANGE_REr   r#   ÚgrouprE   )r?   r'   Ú	sheetnamerW   Z
boundariesr   r   r   Úrange_to_tupleÍ   s    

r[   c                 C   s"   d| kr|   dd¡} d | ¡} | S )z>
    Add quotes around sheetnames if they contain spaces.
    ú'z''z'{0}')Úreplacer&   )rZ   r   r   r   Úquote_sheetnameÛ   s    
r^   )!Ú__doc__ÚreÚstringr   Ú
exceptionsr   Úcompiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRÚVERBOSEr"   ZSHEET_TITLEr&   rX   r   r   r,   r7   r;   r8   r   ÚirH   r	   r   rE   rP   rR   rU   r[   r^   r   r   r   r   Ú<module>   sB   
 ÿÿ

+