
    fh:                         d dl m Z  d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ  G d d	e	          Z G d
 de          ZdS )    copy)
attrgetter)Strict)MinMax)UniqueSequence)Serialisable)range_boundariesrange_to_tupleget_column_letterquote_sheetnamec                      e Zd ZdZ edde          Z edde          Z edde          Z edde          Z		 	 d'dZ
ed             Zed	             Zed
             Zed             Zed             Zd Zd Zd Zd Zd Zd(dZd Zd Zd ZeZd Zd Zd ZeZd Zd Z d Z!d Z"e"Z#d Z$e$Z%d Z&d)d Z'd)d!Z(ed"             Z)ed#             Z*ed$             Z+ed%             Z,ed&             Z-dS )*	CellRangea  
    Represents a range in a sheet: title and coordinates.

    This object is used to perform operations on ranges, like:

    - shift, expand or shrink
    - union/intersection with another sheet range,

    We can check whether a range is:

    - equal or not equal to another,
    - disjoint of another,
    - contained in another.

    We can get:

    - the size of a range.
    - the range bounds (vertices)
    - the coordinates,
    - the string representation,

       ifG  )minmaxexpected_typei   Nc                 `   |0d|v rt          |          \  }\  }}}}nt          |          \  }}}}|| _        || _        || _        || _        || _        ||k    r&d}t          |                    ||                    ||k    r&d}t          |                    ||                    d S )N!z({max_col} must be greater than {min_col})min_colmax_colz({max_row} must be greater than {min_row})min_rowmax_row)	r   r
   r   r   r   r   title
ValueErrorformat)selfrange_stringr   r   r   r   r   fmts           T/var/www/histauto/venv/lib/python3.11/site-packages/openpyxl/worksheet/cell_range.py__init__zCellRange.__init__0   s    #l"">L\>Z>Z;;'775El5S5S2'7
W<CSZZZIIJJJW<CSZZZIIJJJ     c                 6    | j         | j        | j        | j        fS )z2
        Vertices of the range as a tuple
        r   r   r   r   r   s    r    boundszCellRange.boundsF   s    
 |T\4<EEr"   c                     d}| j         | j        k    r| j        | j        k    rd}|                    t          | j                   | j        t          | j                  | j                  S )z9
        Excel-style representation of the range
        z%{min_col}{min_row}:{max_col}{max_row}z{min_col}{min_row}r$   )r   r   r   r   r   r   r   r   s     r    coordzCellRange.coordN   sk    
 6LDL((,,&Czz%dl33L%dl33L	  
 
 	
r"   c              #      K   t          | j        | j        dz             D ]-fdt          | j        | j        dz             D             V  .dS )z1
        Return cell coordinates as rows
        r   c                     g | ]}|fS  r,   ).0colrows     r    
<listcomp>z"CellRange.rows.<locals>.<listcomp>e       MMM#C:MMMr"   N)ranger   r   r   r   )r   r/   s    @r    rowszCellRange.rows_   m      
 t|A~66 	N 	NCMMMMt|T\!^)L)LMMMMMMM	N 	Nr"   c              #      K   t          | j        | j        dz             D ]-fdt          | j        | j        dz             D             V  .dS )z4
        Return cell coordinates as columns
        r   c                     g | ]}|fS r,   r,   )r-   r/   r.   s     r    r0   z"CellRange.cols.<locals>.<listcomp>n   r1   r"   N)r2   r   r   r   r   )r   r.   s    @r    colszCellRange.colsh   r4   r"   c                     ddl m}  |t          | j        | j        dz             t          | j        | j        dz                       S )Nr   )productr   )	itertoolsr9   r2   r   r   r   r   )r   r9   s     r    cellszCellRange.cellsq   sN    %%%%%%wuT\4<>::E$,PTP\]^P^<_<_```r"   c                     t          |t                    s)t          t          t	          |                              |j        r| j        |j        k    rt          d          dS dS )z
        Check whether comparisons between ranges are possible.
        Cannot compare ranges from different worksheets
        Skip if the range passed in has no title.
        z1Cannot work with ranges from different worksheetsN)
isinstancer   	TypeErrorreprtyper   r   r   others     r    _check_titlezCellRange._check_titlew   sl     %++ 	/De--...; 	R4:44PQQQ	R 	R44r"   c                 p    d}| j         rd}|                    | j        j        | j         | j                  S )Nz<{cls} {coord}>z<{cls} {title!r}!{coord}>)clsr   r)   )r   r   	__class____name__r)   r(   s     r    __repr__zCellRange.__repr__   s7     : 	/.Czzdn5TZtzzZZZr"   c                 P    t          | j        | j        | j        | j        f          S N)hashr   r   r   r   r%   s    r    __hash__zCellRange.__hash__   s!    T\4<t|LMMMr"   c                 r    d}| j         }|rd}t          |          }|                    || j                  S )Nz{coord}z{title}!{coord})r   r)   )r   r   r   r)   )r   r   r   s      r    __str__zCellRange.__str__   s@    
 	+$C#E**EzzTZz888r"   c                 h    |                      | j        | j        | j        | j        | j                  S )Nr   r   r   r   r   )rF   r   r   r   r   r   r%   s    r    __copy__zCellRange.__copy__   s3    ~~dlDL&*lDL$(J  0 0 	0r"   r   c                    | j         |z   dk    s| j        |z   dk    r#t          d                    ||                    | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a{  
        Shift the focus of the range according to the shift values (*col_shift*, *row_shift*).

        :type col_shift: int
        :param col_shift: number of columns to be moved by, can be negative
        :type row_shift: int
        :param row_shift: number of rows to be moved by, can be negative
        :raise: :class:`ValueError` if any row or column index < 1
        r   z1Invalid shift value: col_shift={0}, row_shift={1}N)r   r   r   r   r   r   )r   	col_shift	row_shifts      r    shiftzCellRange.shift   s     L9$))|i'1,,PWWXaclmmnnn	!	!	!	!r"   c                     	 |                      |           n# t          $ r Y dS w xY w|j        | j        k    p/| j        |j        k    p|j        | j        k    p| j        |j        k    S )z
        Test whether the ranges are not equal.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* != *other*.
        T)rC   r   r   r   r   r   rA   s     r    __ne__zCellRange.__ne__   s    	e$$$$ 	 	 	44	 MT\) -|u},-},- |u},		
s    
&&c                 .    |                      |           S )z
        Test whether the ranges are equal.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* == *other*.
        )rW   rA   s     r    __eq__zCellRange.__eq__   s     ;;u%%%%r"   c                 V    |                      |           |                    |           S )z
        Test whether every cell in this range is also in *other*.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* <= *other*.
        rC   _CellRange__supersetrA   s     r    issubsetzCellRange.issubset   s,     	%   %%%r"   c                 V    |                      |          o|                     |          S )z
        Test whether *other* contains every cell of this range, and more.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* < *other*.
        )__le__rW   rA   s     r    __lt__zCellRange.__lt__   '     {{5!!8dkk%&8&88r"   c                     | j         |j         cxk    o|j        cxk    o
| j        k    nc o*| j        |j        cxk    o|j        cxk    o
| j        k    nc S rJ   )r   r   r   r   rA   s     r    
__supersetzCellRange.__superset   sw    \U]KKKKemKKKKt|KKKK M\U]KKKKemKKKKt|KKKK	
r"   c                 V    |                      |           |                     |          S )z
        Test whether every cell in *other* is in this range.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* >= *other* (or *other* in *range*).
        r[   rA   s     r    
issupersetzCellRange.issuperset   s*     	%   u%%%r"   c                 V    |                      |          }|                     |          S )zO
        Check whether the range contains a particular cell coordinate
        )rF   r\   r   r)   crs      r    __contains__zCellRange.__contains__  s'     ^^E""r"""r"   c                 V    |                      |          o|                     |          S )z
        Test whether this range contains every cell in *other*, and more.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* > *other*.
        )__ge__rW   rA   s     r    __gt__zCellRange.__gt__  ra   r"   c                     |                      |           | j        |j        k    r|| }} | j        |j        k     p| j        |j        k     p|j        | j        k     S )aV  
        Return ``True`` if this range has no cell in common with *other*.
        Ranges are disjoint if and only if their intersection is the empty range.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: ``True`` if the range has no cells in common with other.
        )rC   r&   r   r   r   r   rA   s     r    
isdisjointzCellRange.isdisjoint  se     	%    ;%%%Du}, 0<%-/0=4</	1r"   c                 h   |                      |          r#t          d                    | |                    t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          ||||          S )aN  
        Return a new range with cells common to this range and *other*

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: the intersecting sheet range.
        :raise: :class:`ValueError` if the *other* range doesn't intersect
            with this range.
        zRange {0} doesn't intersect {0}r$   )
rn   r   r   r   r   r   r   r   r   r   r   rB   r   r   r   r   s         r    intersectionzCellRange.intersection*  s     ??5!! 	T>EEdERRSSSdlEM22dlEM22dlEM22dlEM22'7!(* * * 	*r"   c                 .   |                      |           t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          ||||| j                  S )a  
        Return the minimal superset of this range and *other*. This new range
        will contain all cells from this range, *other*, and any additional
        cells required to form a rectangular ``CellRange``.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: a ``CellRange`` that is a superset of this and *other*.
        rP   )	rC   r   r   r   r   r   r   r   r   rp   s         r    unionzCellRange.unionB  s     	%   dlEM22dlEM22dlEM22dlEM22'7!(
< < < 	<r"   c              #   X   K   | j         D ]}|dk    r	t          | |          }||fV   dS )zC
        For use as a dictionary elsewhere in the library.
        r   N)	__attrs__getattr)r   xvs      r    __iter__zCellRange.__iter__X  sM        	 	AG||a  AQ$JJJJ		 	r"   c                     | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a  
        Expand the range by the dimensions provided.

        :type right: int
        :param right: expand range to the right by this number of cells
        :type down: int
        :param down: expand range down by this number of cells
        :type left: int
        :param left: expand range to the left by this number of cells
        :type up: int
        :param up: expand range up by this number of cells
        Nr$   )r   rightdownleftups        r    expandzCellRange.expandc  sH     	r"   c                     | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a  
        Shrink the range by the dimensions provided.

        :type right: int
        :param right: shrink range from the right by this number of cells
        :type down: int
        :param down: shrink range from the top by this number of cells
        :type left: int
        :param left: shrink range from the left by this number of cells
        :type up: int
        :param up: shrink range from the bottom by this number of cells
        Nr$   )r   r{   bottomr}   tops        r    shrinkzCellRange.shrinkv  sH     	r"   c                 T    | j         dz   | j        z
  }| j        dz   | j        z
  }||dS )zC Return the size of the range as a dictionary of rows and columns. r   )columnsr3   )r   r   r   r   )r   r7   r3   s      r    sizezCellRange.size  s8     |a$,.|a$,.t,,,r"   c                 V      fdt           j         j        dz             D             S )z=A list of cell coordinates that comprise the top of the rangec                 "    g | ]}j         |fS r,   )r   r-   r.   r   s     r    r0   z!CellRange.top.<locals>.<listcomp>       SSSs#SSSr"   r   r2   r   r   r%   s   `r    r   zCellRange.top  3     TSSSuT\4<PQ>/R/RSSSSr"   c                 V      fdt           j         j        dz             D             S )z@A list of cell coordinates that comprise the bottom of the rangec                 "    g | ]}j         |fS r,   )r   r   s     r    r0   z$CellRange.bottom.<locals>.<listcomp>  r   r"   r   r   r%   s   `r    r   zCellRange.bottom  r   r"   c                 V      fdt           j         j        dz             D             S )zCA list of cell coordinates that comprise the left-side of the rangec                 "    g | ]}|j         fS r,   )r   r-   r/   r   s     r    r0   z"CellRange.left.<locals>.<listcomp>       SSSdl#SSSr"   r   r2   r   r   r%   s   `r    r}   zCellRange.left  r   r"   c                 V      fdt           j         j        dz             D             S )zDA list of cell coordinates that comprise the right-side of the rangec                 "    g | ]}|j         fS r,   )r   r   s     r    r0   z#CellRange.right.<locals>.<listcomp>  r   r"   r   r   r%   s   `r    r{   zCellRange.right  r   r"   )NNNNNN)r   r   )r   r   r   r   ).rG   
__module____qualname____doc__r   intr   r   r   r   r!   propertyr&   r)   r3   r7   r;   rC   rH   rL   rN   rQ   rU   rW   rY   r]   r_   r`   r\   re   rk   ri   rl   rn   rq   __and__rs   __or__ry   r   r   r   r   r   r}   r{   r,   r"   r    r   r      s"        . fS999Gfs;;;GfS999Gfs;;;G AE37K K K K, F F XF 
 
 X
  N N XN N N XN a a Xa

R 
R 
R[ [ [N N N9 9 90 0 0" " " "(
 
 
*& & &
& 
& 
& F9 9 9
 
 

& 
& 
& F# # #9 9 91 1 1(* * ** G< < <& F     &   & - - X- T T XT
 T T XT
 T T XT
 T T XT T Tr"   r   c                       e Zd Z ee          Z e            fdZd Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd ZdS )MultiCellRange)r   c                     t          |t                    rd |                                D             }t          |          | _        d S )Nc                 ,    g | ]}t          |          S r,   )r   r-   rs     r    r0   z+MultiCellRange.__init__.<locals>.<listcomp>  s    ;;;qill;;;r"   )r=   strsplitsetrangesr   r   s     r    r!   zMultiCellRange.__init__  s@    fc"" 	<;;FLLNN;;;F&kkr"   c                 t    t          |t                    rt          |          }| j        D ]}||k    r dS dS )NTF)r=   r   r   r   )r   r)   r   s      r    ri   zMultiCellRange.__contains__  sL    eS!! 	%e$$E 	 	Azztt ur"   c                     d                     d |                                 D                       }d| j        j         d| dS )N c                 ,    g | ]}t          |          S r,   r   r   s     r    r0   z+MultiCellRange.__repr__.<locals>.<listcomp>  s    999a3q66999r"   <z [z]>)joinsortedrF   rG   r   s     r    rH   zMultiCellRange.__repr__  sH    994;;==999::84>*88f8888r"   c                 h    d                     d |                                 D                       }|S )Nr   c                 ,    g | ]}t          |          S r,   r   r   s     r    r0   z*MultiCellRange.__str__.<locals>.<listcomp>  s    :::qCFF:::r"   )r   r   r   s     r    rN   zMultiCellRange.__str__  s/    ::DKKMM:::;;r"   c                 :    t          t          |                     S rJ   )rK   r   r%   s    r    rL   zMultiCellRange.__hash__  s    CIIr"   c           	      N    t          | j        t          dddd                    S )z/
        Return a sorted list of items
        r   r   r   r   )key)r   r   r   r%   s    r    r   zMultiCellRange.sorted  s(     dkz)Y	S\']']^^^^r"   c                     |}t          |t                    rt          |          }n$t          |t                    st          d          || vr| j                            |           dS dS )z4
        Add a cell coordinate or CellRange
        zYou can only add CellRangesN)r=   r   r   r   r   addrg   s      r    r   zMultiCellRange.add  su     eS!! 	<5!!BBE9-- 	<:;;;T>>KOOB >r"   c                 0    |                      |           | S rJ   )r   r   r)   s     r    __iadd__zMultiCellRange.__iadd__  s    r"   c                 v    t          |t                    r|                     |          }| j        |j        k    S rJ   )r=   r   rF   r   rA   s     r    rY   zMultiCellRange.__eq__  s4    uc"" 	*NN5))E{el**r"   c                     | |k     S rJ   r,   rA   s     r    rW   zMultiCellRange.__ne__  s    5=  r"   c                 *    t          | j                  S rJ   )boolr   r%   s    r    __bool__zMultiCellRange.__bool__  s    DK   r"   c                     t          |t                    st          |          }| j                            |           d S rJ   )r=   r   r   remover   s     r    r   zMultiCellRange.remove  s=    %++ 	%e$$E5!!!!!r"   c              #   &   K   | j         D ]}|V  d S rJ   )r   )r   rh   s     r    ry   zMultiCellRange.__iter__  s,      + 	 	BHHHH	 	r"   c                 B    d | j         D             }t          |          S )Nc                 ,    h | ]}t          |          S r,   r   r   s     r    	<setcomp>z*MultiCellRange.__copy__.<locals>.<setcomp>  s    ///a$q''///r"   )r   r   r   s     r    rQ   zMultiCellRange.__copy__  s%    //4;///f%%%r"   N)rG   r   r   r   r   r   r   r!   ri   rH   rN   rL   r   r   r   rY   rW   r   r   ry   rQ   r,   r"   r    r   r     s        ^)444F "cee " " " "  9 9 9
  
  _ _ _
  
  
   
+ + +! ! !! ! !" " "  
& & & & &r"   r   N)r   operatorr   openpyxl.descriptorsr   r   openpyxl.descriptors.sequencer   !openpyxl.descriptors.serialisabler	   openpyxl.utilsr
   r   r   r   r   r   r,   r"   r    <module>r      s(               ' ' ' ' ' ' ' ' ' ' ' ' 8 8 8 8 8 8 : : : : : :           TT TT TT TT TT TT TT TTnW& W& W& W& W&V W& W& W& W& W&r"   