U
    ~fh7                     @   s   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	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gee Zedddd Zdd eedD ZdZedd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
    )	lru_cache)chainproduct)ascii_uppercasedigitsN   )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-eyn_au5k/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_coordinate:   s    

r0   r#   )maxsizec                 C   s~   d|   krdks$n t d| g }| dk r8t|  S | rtt| d\} }|dt|  |s8| d8 } |dd q8d|S )a  
    Convert decimal column position to its ASCII (base 26) form.

    Because column indices are 1-based, strides are actually pow(26, n) + 26
    Hence, a correction is applied between pow(26, n) and pow(26, 2) + 26 to
    prevent and additional column letter being prepended

    "A" == 1 == pow(26, 0)
    "Z" == 26 == pow(26, 0) + 26 // decimal equivalent 10
    "AA" == 27 == pow(26, 1) + 1
    "ZZ" == 702 == pow(26, 2) + 26 // decimal equivalent 100
    r   ifG  zInvalid column index {0}   r   Zr#   )r'   r*   __decimal_to_alphadivmodinsertjoin)Zcol_idxresult	remainderr   r   r   r   N   s    r   c                 C   s   i | ]\}}||qS r   r   )r   posletterr   r   r   
<dictcomp>o   s      r<   )r   r2   i  c              	   C   s   d|  d}t | dkr t|d}t|  } t| tD ]>\}}zt| }W n tk
rj   t|Y nX ||| 7 }q:d|  k rdk sn t||S )z
    Convert ASCII column name (base 26) to decimal with 1-based index

    Characters represent descending multiples of powers of 26

    "AFZ" == 26 * pow(26, 0) + 6 * pow(26, 1) + 1 * pow(26, 2)
    'z<' is not a valid column name. Column names are from A to ZZZ   r   igG  )lenr'   reversedupperzip__powers__alpha_to_decimalKeyError)col	error_msgidxr;   powerr:   r   r   r   r   r   s    	r   c           
      C   s   d | }t| }|s t|| \}}}}}|rz||f}||f}	t||	 szt|rbt|	rzt|	rrt|rzt||dk	rt|}|dk	rt|}|dk	rt|}n|}|dk	rt|}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<    




rR   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   rF   r   r   r   r      s     z#rows_from_range.<locals>.<listcomp>c                 3   s   | ]}d  | V  qdS z{0}{1}Nr*   rT   r!   r   r   	<genexpr>   s     z"rows_from_range.<locals>.<genexpr>NrR   r   tuplerL   rM   rN   r$   r%   rQ   rP   r   rW   r   rows_from_range   s
    r\   c                 #   s\   t | \}}}}t||d }dd t||d D }|D ] t fdd|D V  q:dS )rS   r   c                 s   s   | ]}t |V  qd S )Nr   rT   r   r   r   rX      s     z"cols_from_range.<locals>.<genexpr>c                 3   s   | ]}d   |V  qdS rU   rV   )r   r!   rF   r   r   rX      s     NrY   r[   r   r]   r   cols_from_range   s
    r^   c                 C   sF   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   )Z
coordinaterH   crF   r!   r   r   r   coordinate_to_tuple   s    ra   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'   grouprR   )rL   r+   	sheetnamerc   Z
boundariesr   r   r   range_to_tuple   s    

rg   c                 C   s"   d| kr|  dd} d| } | S )z>
    Add quotes around sheetnames if they contain spaces.
    r=   z''z'{0}')replacer*   )rf   r   r   r   quote_sheetname   s    
ri   )&__doc__	functoolsr   	itertoolsr   r   stringr   r   re
exceptionsr   compiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRVERBOSEr&   ZSHEET_TITLEr*   rd   r   r"   r0   listr4   r   r_   rD   rC   r   rR   r\   r^   ra   rg   ri   r   r   r   r   <module>   sB   
 
 
+