o
    ðäj7  ã                   @   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   ó   g | ]}t |ƒ‘qS © ©Úget_column_letter)Ú.0Úxr   r   úW/var/www/ticemtrilhas/avaliacao_env/lib/python3.10/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|ƒ}|s'd| › 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 s-|d r0d}nd}|jd	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}Nr   )ÚABSOLUTE_REr   Ú
ValueErrorÚ	groupdictÚitemsÚformat)r    ÚmÚdÚkÚvÚfmtr   r   r   Úabsolute_coordinate:   s   

€r2   r%   )Úmaxsizec                 C   s€   d|   kr
dksn t d | ¡ƒ‚g }| dk rt|  S | r;t| dƒ\} }| dt| ¡ |s9| d8 } | dd¡ | sd |¡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)Ú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   r4   i¤  c              	   C   sœ   d| › d�}t | ƒdkrt|ƒ‚d}t|  ¡ ƒ} t| tƒD ]\}}zt| }W n ty2   t|ƒ‚w ||| 7 }qd|  k rGdk sLt|ƒ‚ 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|ƒ‚| ¡ \}}}}}|r=||f}||f}	t||	 ƒs=t|ƒr1t|	ƒr=t|	ƒr9t|ƒr=t|ƒ‚|durEt|ƒ}|durMt|ƒ}|durVt|ƒ}n|}|durat|ƒ}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<   


ÿþþýý

rU   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   r   r   r   ©r   rI   r   r   r   r   ½   r   z#rows_from_range.<locals>.<listcomp>c                 3   s   � | ]	}d   |ˆ ¡V  qdS ©z{0}{1}N©r,   rW   ©r#   r   r   Ú	<genexpr>¿   ó   € z"rows_from_range.<locals>.<genexpr>N©rU   r   Útuple©rO   rP   rQ   r&   r'   rT   rS   r   rZ   r   Úrows_from_range¶   ó   €ÿr`   c                 #   s^   � t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}|D ]‰ t‡ fdd„|D ƒƒV  qdS )rV   r   c                 s   s   � | ]}t |ƒV  qd S )Nr   rW   r   r   r   r[   É   s   € z"cols_from_range.<locals>.<genexpr>c                 3   s   � | ]	}d   ˆ |¡V  qdS rX   rY   )r   r#   ©rI   r   r   r[   Ë   r\   Nr]   r_   r   rb   r   Úcols_from_rangeÂ   ra   rc   c                 C   sF   t | ƒD ]
\}}|tv r nq| d|… }| |d… }t|ƒt|ƒfS )zB
    Convert an Excel style coordinate to (row, column) tuple
    N)Ú	enumerater   r   r   )Ú
coordinaterK   ÚcrI   r#   r   r   r   Úcoordinate_to_tupleÎ   s   ÿrg   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ÚquotedÚ	notquotedÚcells)ÚSHEETRANGE_REr   r)   ÚgrouprU   )rO   r-   Ú	sheetnamerj   Ú
boundariesr   r   r   Úrange_to_tupleÚ   s   

ro   c                 C   s"   d| v r
|   dd¡} d | ¡} | S )z>
    Add quotes around sheetnames if they contain spaces.
    r@   z''z'{0}')Úreplacer,   )rm   r   r   r   Úquote_sheetnameè   s   
rq   )&Ú__doc__Ú	functoolsr   Ú	itertoolsr   r   Ústringr   r   ÚreÚ
exceptionsr   Úcompiler   Ú	COL_RANGEÚ	ROW_RANGEÚ
RANGE_EXPRÚVERBOSEr(   ÚSHEET_TITLEr,   rk   r   r$   r2   Úlistr6   r   rd   rG   rF   r   rU   r`   rc   rg   ro   rq   r   r   r   r   Ú<module>   sB   
ÿÿ
 
+