
    Mwg:                         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y)    )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-y))	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                 *   |(d|v rt        |      \  }\  }}}}nt        |      \  }}}}|| _        || _        || _        || _        || _        ||kD  rd}t        |j                  ||            ||kD  rd}t        |j                  ||            y )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/horilla/myenv/lib/python3.12/site-packages/openpyxl/worksheet/cell_range.py__init__zCellRange.__init__0   s    #l">L\>Z;;'75El5S2'7
W<CSZZZIJJW<CSZZZIJJ     c                 ^    | 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}|j	                  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   sj    
 6LLDLL(,&Czz%dll3LL%dll3LL	  
 	
r!   c              #      K   t        | j                  | j                  dz         D ]7  }t        | j                  | j                  dz         D cg c]  }||f c} 9 yc c}w w)z1
        Return cell coordinates as rows
        r   N)ranger   r   r   r   )r   rowcols      r   rowszCellRange.rows_   W     
 t||A~6 	NC).t||T\\!^)LM#C:MM	NM   AA&A!A&c              #      K   t        | j                  | j                  dz         D ]7  }t        | j                  | j                  dz         D cg c]  }||f c} 9 yc c}w w)z4
        Return cell coordinates as columns
        r   N)r*   r   r   r   r   )r   r,   r+   s      r   colszCellRange.colsh   r.   r/   c                     ddl m}  |t        | j                  | j                  dz         t        | j
                  | j                  dz               S )Nr   )productr   )	itertoolsr3   r*   r   r   r   r   )r   r3   s     r   cellszCellRange.cellsq   s=    %uT\\4<<>:E$,,PTP\P\]^P^<_``r!   c                     t        |t              st        t        t	        |                  |j
                  r%| j
                  |j
                  k7  rt        d      yy)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   sK     %+De-..;;4::4PQQ 5;r!   c                     d}| j                   rd}|j                  | 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__   s:     ::.Czzdnn55TZZtzzz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Mr!   c                 v    d}| j                   }|rd}t        |      }|j                  || j                        S )Nz{coord}z{title}!{coord})r   r(   )r   r   r   r(   )r   r   r   s      r   __str__zCellRange.__str__   s9    

$C#E*EzzTZZz88r!   c                     | j                  | j                  | j                  | j                  | j                  | j
                        S )Nr   r   r   r   r   )r@   r   r   r   r   r   r$   s    r   __copy__zCellRange.__copy__   s8    ~~dllDLL&*llDLL$(JJ  0 	0r!   c                 *   | j                   |z   dk  s| j                  |z   dk  rt        dj                  ||            | xj                   |z  c_         | xj                  |z  c_        | xj                  |z  c_        | xj
                  |z  c_        y)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   sy     LL9$)||i'1,PWWXaclmnn	!	!	!	!r!   c                    	 | j                  |       |j                  | j                  k7  xsO | j                  |j                  k7  xs4 |j                  | j                  k7  xs | j
                  |j
                  k7  S # t        $ r Y yw xY w)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)r=   r   r   r   r   r   r;   s     r   __ne__zCellRange.__ne__   s    	e$
 MMT\\) -||u}},-}},- ||u}},		
  		s   A= =	B	B	c                 &    | j                  |       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*.
        )rQ   r;   s     r   __eq__zCellRange.__eq__   s     ;;u%%%r!   c                 F    | j                  |       |j                  |       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*.
        r=   _CellRange__supersetr;   s     r   issubsetzCellRange.issubset   s#     	% %%r!   c                 J    | j                  |      xr | j                  |      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__rQ   r;   s     r   __lt__zCellRange.__lt__   !     {{5!8dkk%&88r!   c                    | j                   |j                   cxk  xr! |j                  cxk  xr | j                  k  nc xr@ | j                  |j                  cxk  xr! |j                  cxk  xr | j                  k  S c S rD   )r   r   r   r   r;   s     r   
__supersetzCellRange.__superset   s\    \\U]]KemmKt||K M\\U]]KemmKt||K	
 L	
r!   c                 F    | j                  |       | j                  |      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*).
        rU   r;   s     r   
issupersetzCellRange.issuperset   s!     	% u%%r!   c                 F    | j                  |      }| j                  |      S )zO
        Check whether the range contains a particular cell coordinate
        )r@   rV   r   r(   crs      r   __contains__zCellRange.__contains__  s!     ^^E"r""r!   c                 J    | j                  |      xr | j                  |      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__rQ   r;   s     r   __gt__zCellRange.__gt__  r[   r!   c                     | j                  |       | j                  |j                  kD  r|| }} | j                  |j                  k  xs4 | j                  |j
                  k  xs |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.
        )r=   r%   r   r   r   r   r;   s     r   
isdisjointzCellRange.isdisjoint  sl     	%  ;;%%Du}}, 0<<%--/0==4<</	1r!   c                 x   | j                  |      rt        dj                  | |            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#   )
rh   r   r   r   r   r   r   r   r   r   r   r<   r   r   r   r   s         r   intersectionzCellRange.intersection*  s     ??5!>EEdERSSdllEMM2dllEMM2dllEMM2dllEMM2'7!(* 	*r!   c                 X   | j                  |       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*.
        rJ   )	r=   r   r   r   r   r   r   r   r   rj   s         r   unionzCellRange.unionB  s~     	% dllEMM2dllEMM2dllEMM2dllEMM2'7!(

< 	<r!   c              #   ^   K   | j                   D ]  }|dk(  r	t        | |      }||f  yw)zC
        For use as a dictionary elsewhere in the library.
        r   N)	__attrs__getattr)r   xvs      r   __iter__zCellRange.__iter__X  s:       	AG|a AQ$J		s   +-c                     | xj                   |z  c_         | xj                  |z  c_        | xj                  |z  c_        | xj                  |z  c_        y)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  s<     	r!   c                     | xj                   |z  c_         | xj                  |z  c_        | xj                  |z  c_        | xj                  |z  c_        y)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   ru   bottomrw   tops        r   shrinkzCellRange.shrinkv  s<     	r!   c                 |    | 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   )columnsr-   )r   r   r   r   )r   r1   r-   s      r   sizezCellRange.size  s=     ||a$,,.||a$,,.t,,r!   c                     t        | j                  | j                  dz         D cg c]  }| j                  |f c}S c c}w )z=A list of cell coordinates that comprise the top of the ranger   )r*   r   r   r   r   r,   s     r   r|   zCellRange.top  5     05T\\4<<PQ>/RSs#SSS   >c                     t        | j                  | j                  dz         D cg c]  }| j                  |f c}S c c}w )z@A list of cell coordinates that comprise the bottom of the ranger   )r*   r   r   r   r   s     r   r{   zCellRange.bottom  r   r   c                     t        | j                  | j                  dz         D cg c]  }|| j                  f c}S c c}w )zCA list of cell coordinates that comprise the left-side of the ranger   )r*   r   r   r   r   r+   s     r   rw   zCellRange.left  5     05T\\4<<PQ>/RSdll#SSSr   c                     t        | j                  | j                  dz         D cg c]  }|| j                  f c}S c c}w )zDA list of cell coordinates that comprise the right-side of the ranger   )r*   r   r   r   r   s     r   ru   zCellRange.right  r   r   )NNNNNN)r   r   )r   r   r   r   ).rA   
__module____qualname____doc__r   intr   r   r   r   r    propertyr%   r(   r-   r1   r5   r=   rB   rF   rH   rK   rO   rQ   rS   rW   rY   rZ   rV   r_   re   rc   rf   rh   rk   __and__rm   __or__rs   ry   r}   r   r|   r{   rw   ru    r!   r   r   r      s   . S9Gs;GS9Gs;G AE37K, F F 
 
  N N N N a a

R[N90"(
*&
& F9

& F#91(** G<& F&& - - T T
 T T
 T 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y)MultiCellRange)r   c                     t        |t              r&|j                         D cg c]  }t        |       }}t	        |      | _        y c c}w rD   )r7   strsplitr   setranges)r   r   rs      r   r    zMultiCellRange.__init__  s9    fc",2LLN;qil;F;&k <s   Ac                 j    t        |t              rt        |      }| j                  D ]	  }||k  s	 y y)NTF)r7   r   r   r   )r   r(   r   s      r   rc   zMultiCellRange.__contains__  s8    eS!e$E 	Az	 r!   c                     dj                  | j                         D cg c]  }t        |       c}      }d| j                  j                   d| dS c c}w )N <z [z]>)joinsortedr   r@   rA   r   r   r   s      r   rB   zMultiCellRange.__repr__  sJ    4;;=9a3q69:4>>**+2fXR88 :s   Ac                 |    dj                  | j                         D cg c]  }t        |       c}      }|S c c}w )Nr   )r   r   r   r   s      r   rH   zMultiCellRange.__str__  s/    DKKM:qCF:; ;s   9c                 *    t        t        |             S rD   )rE   r   r$   s    r   rF   zMultiCellRange.__hash__  s    CIr!   c           	      H    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!     dkkz)Y	S\']^^r!   c                     |}t        |t              rt        |      }nt        |t              st        d      || vr| j                  j                  |       yy)z4
        Add a cell coordinate or CellRange
        zYou can only add CellRangesN)r7   r   r   r   r   addra   s      r   r   zMultiCellRange.add  sO     eS!5!BE9-:;;T>KKOOB r!   c                 (    | j                  |       | S rD   )r   r   r(   s     r   __iadd__zMultiCellRange.__iadd__  s    r!   c                 v    t        |t              r| j                  |      }| j                  |j                  k(  S rD   )r7   r   r@   r   r;   s     r   rS   zMultiCellRange.__eq__  s-    uc"NN5)E{{ell**r!   c                     | |k(   S rD   r   r;   s     r   rQ   zMultiCellRange.__ne__  s    5=  r!   c                 ,    t        | j                        S rD   )boolr   r$   s    r   __bool__zMultiCellRange.__bool__  s    DKK  r!   c                 p    t        |t              st        |      }| j                  j                  |       y rD   )r7   r   r   remover   s     r   r   zMultiCellRange.remove  s(    %+e$E5!r!   c              #   6   K   | j                   D ]  }|  y wrD   )r   )r   rb   s     r   rs   zMultiCellRange.__iter__  s     ++ 	BH	s   c                 f    | j                   D ch c]  }t        |       }}t        |      S c c}w rD   )r   r   r   r   s      r   rK   zMultiCellRange.__copy__  s-    #';;/a$q'//f%% 0s   .N)rA   r   r   r   r   r   r   r    rc   rB   rH   rF   r   r   r   rS   rQ   r   r   rs   rK   r   r!   r   r   r     s\     )4F "e "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      sB      ' ' 8 : TT TTnW&V W&r!   