o
    {Ú“ZÖ1  ã                   @   st   d dl mZmZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZmZmZ G dd„ deƒZG dd	„ d	eƒZd
S )é    )Úabsolute_importÚunicode_literals)Ú	safe_repr)ÚStrict)ÚMinMaxÚSequence)Úrange_boundariesÚrange_to_tupleÚget_column_letterÚquote_sheetnamec                   @   s$  e Zd ZdZedded�Zedded�Zedded�Zedded�Z			d4dd„Z
ed	d
„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ Zd5dd„Zdd„ Zdd„ Zdd„ ZeZd d!„ Zd"d#„ ZeZd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ ZeZ d,d-„ Z!e!Z"d6d.d/„Z#d6d0d1„Z$ed2d3„ ƒZ%dS )7Ú	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                 C   s¢   |d ur"zt |ƒ\}\}}}}W n ty!   t|ƒ\}}}}Y nw || _|| _|| _|| _|| _||kr@d}t|j||d�ƒ‚||krOd}t|j||d�ƒ‚d S )Nz({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	   Ú
ValueErrorr   r   r   r   r   ÚtitleÚformat)ÚselfÚrange_stringr   r   r   r   r   Úfmt© r   úO/var/www/html/env/lib/python3.10/site-packages/openpyxl/worksheet/cell_range.pyÚ__init__.   s$   ÿþzCellRange.__init__c                 C   s   | j | j| j| jfS )z2
        Vertices of the range as a tuple
        ©r   r   r   r   ©r   r   r   r   ÚboundsD   s   zCellRange.boundsc                 C   sB   d}| j | jkr| j| jkrd}|jt| j ƒ| jt| jƒ| jd�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   r   r   r   ÚcoordL   s   üzCellRange.coordc                 C   s<   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   Úotherr   r   r   Ú_check_title^   s
   
ÿzCellRange._check_titlec                 C   s*   d}| j rd}t|j| jj| j | jd�ƒS )Nz<{cls} {coord}>z<{cls} {title!r}!{coord}>)Úclsr   r"   )r   r   r   Ú	__class__Ú__name__r"   r!   r   r   r   Ú__repr__k   s   zCellRange.__repr__c                 C   s.   d}| j }| j rd}t| j ƒ}|j|| jd�S )Nz{coord}z{title}!{coord})r   r"   )r   r   r   r"   )r   r   r   r   r   r   Ú_get_range_stringr   s   
zCellRange._get_range_stringc                 C   s   |   ¡ }t|ƒS ©N)r.   r   ©r   r"   r   r   r   Ú__str__}   s   zCellRange.__str__c                 C   s   | j | j| j| j| j| jd�S )N©r   r   r   r   r   )r+   r   r   r   r   r   r   r   r   r   Ú__copy__‚   s   þzCellRange.__copy__r   c                 C   sh   | j | dks| j| dkrtd ||¡ƒ‚|  j |7  _ |  j|7  _|  j|7  _|  j|7  _dS )an  
        Shift 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_shiftr   r   r   Úshiftˆ   s   zCellRange.shiftc                 C   sT   z|   |¡ W n
 ty   Y dS 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: CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* != *other*.
        T)r)   r   r   r   r   r   r'   r   r   r   Ú__ne__œ   s   ÿ
ÿ
þ
üzCellRange.__ne__c                 C   s   |   |¡ S )z­
        Test whether the ranges are equal.

        :type other: CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* == *other*.
        )r7   r'   r   r   r   Ú__eq__±   s   zCellRange.__eq__c                 C   s^   |   |¡ |j| j  ko| j  ko|jkn  o.|j| j  ko,| j  ko,|jkS   S )zÂ
        Test whether every element in the range is in *other*.

        :type other: SheetRange
        :param other: Other sheet range
        :return: ``True`` if *range* <= *other*.
        ©r)   r   r   r   r   r'   r   r   r   Úissubset¼   ó   
*"ýýzCellRange.issubsetc                 C   ó   |   |¡o	|  |¡S )zë
        Test whether every element in the range is in *other*, but not all.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* < *other*.
        )Ú__le__r7   r'   r   r   r   Ú__lt__Ï   ó   zCellRange.__lt__c                 C   s^   |   |¡ | j|j  ko|j  ko| jkn  o.| j|j  ko,|j  ko,| jkS   S )a0  
        Test whether every element in *other* is in the range.

        :type other: openpyxl.worksheet.cell_range.CellRange or tuple[int, int]
        :param other: Other sheet range or cell index (*row_idx*, *col_idx*).
        :return: ``True`` if *range* >= *other* (or *other* in *range*).
        r9   r'   r   r   r   Ú
issupersetÚ   r;   zCellRange.issupersetc                 C   s&   |   |¡}|jdu r| j|_|  |¡S )zO
        Check whether the range contains a particular cell coordinate
        N)r+   r   r@   )r   r"   Úcrr   r   r   Ú__contains__î   s   


zCellRange.__contains__c                 C   r<   )zë
        Test whether every element in *other* is in the range, but not all.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* > *other*.
        )Ú__ge__r7   r'   r   r   r   Ú__gt__ø   r?   zCellRange.__gt__c                 C   s:   |   |¡ | j|jkr| }|} |}| j| jf|j|jfk S )aY  
        Return ``True`` if the range has no elements 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 elements in common with other.
        )r)   r    r   r   r   )r   r(   Úir   r   r   Ú
isdisjoint  s   
	zCellRange.isdisjointc                 C   sb   |   |¡rtd | |¡ƒ‚t| j|jƒ}t| j|jƒ}t| j|jƒ}t| j|jƒ}t	||||d�S )aY  
        Return a new range with elements common to the range and another

        :type others: tuple[openpyxl.worksheet.cell_range.CellRange]
        :param others: Other sheet ranges.
        :return: the current sheet range.
        :raise: :class:`ValueError` if an *other* range don't intersect
            with the current range.
        zRange {0} don't intersect {0}r   )
rF   r   r   r   r   r   r   r   r   r   ©r   r(   r   r   r   r   r   r   r   Úintersection  s   

ÿzCellRange.intersectionc                 C   sV   |   |¡ t| j|jƒ}t| j|jƒ}t| j|jƒ}t| j|jƒ}t||||| jd�S )zî
        Return a new range with elements from the range and all *others*.

        :type others: tuple[openpyxl.worksheet.cell_range.CellRange]
        :param others: Other sheet ranges.
        :return: the current sheet range.
        r2   )	r)   r   r   r   r   r   r   r   r   rG   r   r   r   Úunion/  s   
ÿzCellRange.unionc                 C   s<   |  j |8  _ |  j|8  _|  j|7  _|  j|7  _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Úupr   r   r   ÚexpandD  ó   zCellRange.expandc                 C   s<   |  j |7  _ |  j|7  _|  j|8  _|  j|8  _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 bottown by this number of cells
        Nr   )r   rJ   ÚbottomrL   Útopr   r   r   ÚshrinkW  rO   zCellRange.shrinkc                 C   s*   | j d | j }| jd | j }||dœS )zD Return the size of the range as a dicitionary of rows and columns. r   )ÚcolumnsÚrows)r   r   r   r   )r   ÚcolsrT   r   r   r   Úsizej  s   
zCellRange.size)NNNNNN)r   r   )r   r   r   r   )&r,   Ú
__module__Ú__qualname__Ú__doc__r   Úintr   r   r   r   r   Úpropertyr    r"   r)   r-   r.   Ú__unicode__r1   r3   r6   r7   r8   r:   r=   r>   r@   rC   rB   rD   rF   rH   Ú__and__rI   Ú__or__rN   rR   rV   r   r   r   r   r      sL    
ÿ





r   c                   @   st   e Zd Zeed�Zddd„Zdd„ Zdd„ Zd	d
„ Z	e	Z
dd„ ZeZdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ ZdS )ÚMultiCellRange)r   r   c                 C   s&   t |tƒrdd„ | ¡ D ƒ}|| _d S )Nc                 S   ó   g | ]}t |ƒ‘qS r   )r   ©Ú.0Úrr   r   r   Ú
<listcomp>z  ó    z+MultiCellRange.__init__.<locals>.<listcomp>)r#   ÚstrÚsplitÚranges©r   rh   r   r   r   r   x  s   

zMultiCellRange.__init__c                 C   s   | j D ]	}||v r dS qdS )NTF©rh   )r   r"   rc   r   r   r   rB   ~  s
   
ÿzMultiCellRange.__contains__c                 C   s&   d  dd„ | jD ƒ¡}d | jj|¡S )Nú c                 S   r`   r   ©rf   ra   r   r   r   rd   †  re   z+MultiCellRange.__repr__.<locals>.<listcomp>z<{0} [{1}]>)Újoinrh   r   r+   r,   ri   r   r   r   r-   …  s   zMultiCellRange.__repr__c                 C   s   d  dd„ | jD ƒ¡}|S )Nrk   c                 S   r`   r   rl   ra   r   r   r   rd   ‹  re   z*MultiCellRange.__str__.<locals>.<listcomp>)rm   rh   ri   r   r   r   r1   Š  s   zMultiCellRange.__str__c                 C   s*   || vrt |ƒ}| j}| |¡ || _| S )zD
        Add a cell coordinate. Will create a new CellRange
        )r   rh   Úappend)r   r"   rA   rh   r   r   r   Úadd‘  s   
zMultiCellRange.addc                 C   s    t |tƒr
|  |¡}| j|jkS r/   )r#   rf   r+   rh   r'   r   r   r   r8   Ÿ  s   

zMultiCellRange.__eq__c                 C   s
   | |k S r/   r   r'   r   r   r   r7   ¥  ó   
zMultiCellRange.__ne__c                 C   s
   t | jƒS r/   )Úboolrh   r   r   r   r   Ú__bool__©  rp   zMultiCellRange.__bool__c                 C   s"   t |tƒs	t|ƒ}| j |¡ d S r/   )r#   r   rh   Úremover0   r   r   r   rs   ¯  s   
zMultiCellRange.removec                 c   s   � | j D ]}|V  qd S r/   rj   )r   rA   r   r   r   Ú__iter__µ  s   €
ÿzMultiCellRange.__iter__N)r   )r,   rW   rX   r   r   rh   r   rB   r-   r1   r\   ro   Ú__iadd__r8   r7   rr   Ú__nonzero__rs   rt   r   r   r   r   r_   r  s    

r_   N)Ú
__future__r   r   Úopenpyxl.compat.stringsr   Úopenpyxl.descriptorsr   r   r   Úopenpyxl.utilsr   r	   r
   r   r   r_   r   r   r   r   Ú<module>   s      d