a
    �Ï?eˆ  ã                   @   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   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/openpyxl/utils/cell.pyÚ
<listcomp>'   ó    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¡}| ¡ D ]\}}|r.d|› �||< q.|d sZ|d r`d}nd}|jf i |¤Ž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    
r8   igG  c                 C   s0   z
t |  W S  ty*   td | ¡ƒ‚Y n0 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   s4   zt |  ¡  W S  ty.   td | ¡ƒ‚Y n0 dS )z@Convert a column name into a numerical index
    ('A' -> 1)
    z{0} is not a valid column nameN)Ú_COL_STRING_CACHEÚupperr:   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urŠt|ƒ}|duršt|ƒ}|dur¬t|ƒ}n|}|du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<    


ÿþþýý

rF   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   °   r   z#rows_from_range.<locals>.<listcomp>c                 3   s   | ]}d   |ˆ ¡V  qdS ©z{0}{1}N©r'   rH   ©r   r   r   Ú	<genexpr>²   r   z"rows_from_range.<locals>.<genexpr>N©rF   r   Útuple©r@   rA   rB   r!   r"   rE   rD   r   rL   r   Úrows_from_range©   s
    rQ   c                 #   s\   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}|D ]‰ t‡ fdd„|D ƒƒV  q:dS )rG   r   c                 s   s   | ]}t |ƒV  qd S )Nr   rH   r   r   r   rM   ¼   r   z"cols_from_range.<locals>.<genexpr>c                 3   s   | ]}d   ˆ |¡V  qdS rJ   rK   )r
   r   ©rI   r   r   rM   ¾   r   NrN   rP   r   rR   r   Úcols_from_rangeµ   s
    rS   c                 C   sJ   t | ƒD ]\}}|tv 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;   ÚcrI   r   r   r   r   Úcoordinate_to_tupleÁ   s    rV   c                 C   sH   t  | ¡}|du 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$   ÚgrouprF   )r@   r(   Ú	sheetnamerX   Z
boundariesr   r   r   Úrange_to_tupleÍ   s    

r\   c                 C   s"   d| v r|   dd¡} d | ¡} | S )z>
    Add quotes around sheetnames if they contain spaces.
    ú'z''z'{0}')Úreplacer'   )r[   r   r   r   Úquote_sheetnameÛ   s    
r_   )!Ú__doc__ÚreÚstringr   Ú
exceptionsr   Úcompiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRÚVERBOSEr#   ZSHEET_TITLEr'   rY   r   r   r-   r8   r<   r9   r   ÚirI   r	   r   rF   rQ   rS   rV   r\   r_   r   r   r   r   Ú<module>   s@   
ÿÿ

+