B
    ÔùÓ`‡  ã               @   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x*eddƒD ]ZeeƒZeee< eee< qžW 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-zhp1095k/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   st   t  | ¡}|st| › d�ƒ‚| d¡}x&| ¡ D ]\}}|r0d|› �||< q0W |d s^|d rdd}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   sx   d|   krdks$n t d | ¡ƒ‚g }x@| dkrht| dƒ\} }|dkrTd}| d8 } | t|d ƒ¡ q*W 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  zInvalid column index {0}r   é   é@   r   )r"   r%   ÚdivmodÚappendÚchrÚjoinÚreversed)Zcol_idxÚlettersÚ	remainderr   r   r   Ú_get_column_letterJ   s    

r5   igG  c             C   s0   yt |  S  tk
r*   td | ¡ƒ‚Y nX dS )z?Convert a column index into a column letter
    (3 -> 'C')
    zInvalid column index {0}N)Ú_STRING_COL_CACHEÚKeyErrorr"   r%   )Úidxr   r   r   r   i   s    r   c             C   s4   yt |  ¡  S  tk
r.   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Úupperr7   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.    



rC   c             #   s`   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}x$|D ]‰ t‡ fdd„|D ƒƒV  q<W dS )z[
    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%   )r	   rD   )r   r   r   ú	<genexpr>²   s    z"rows_from_range.<locals>.<genexpr>N)rC   r   Útuple)r=   r>   r?   r   r    rB   rA   r   )r   r   Úrows_from_range©   s
    
rG   c             #   s`   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}x$|D ]‰ t‡ fdd„|D ƒƒV  q<W dS )z[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c             s   s   | ]}t |ƒV  qd S )N)r   )r	   rD   r   r   r   rE   ¼   s    z"cols_from_range.<locals>.<genexpr>c             3   s   | ]}d   ˆ |¡V  qdS )z{0}{1}N)r%   )r	   r   )rD   r   r   rE   ¾   s    N)rC   r   rF   )r=   r>   r?   r   r    rB   rA   r   )rD   r   Úcols_from_rangeµ   s
    
rH   c             C   sL   xt | ƒD ]\}}|tkr
P q
W | d|…  ¡ }| |d… }t|ƒt| fS )zA
    Convert an Excel style coordinate to (row, colum) tuple
    N)Ú	enumerater   r:   r   r9   )Z
coordinater8   ÚcrD   r   r   r   r   Úcoordinate_to_tupleÁ   s    rK   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"   ÚgrouprC   )r=   r&   Ú	sheetnamerM   Z
boundariesr   r   r   Úrange_to_tupleÍ   s    

rQ   c             C   s"   d| kr|   dd¡} d | ¡} | S )z>
    Add quotes around sheetnames if they contain spaces.
    ú'z''z'{0}')Úreplacer%   )rP   r   r   r   Úquote_sheetnameÛ   s    
rT   )!Ú__doc__ÚreÚstringr   Ú
exceptionsr   Úcompiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRÚVERBOSEr!   ZSHEET_TITLEr%   rN   r   r   r+   r5   r9   r6   r   ÚirD   r   r   rC   rG   rH   rK   rQ   rT   r   r   r   r   Ú<module>   s:   

+