B
    ÔùÓ`29  ã               @   sp   d dl m Z  d dlmZ d dlmZmZ d dl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 )é    )Úcopy)ÚStrict)ÚMinMaxÚSequence)ÚSerialisable)Úrange_boundariesÚrange_to_tupleÚget_column_letterÚquote_sheetnamec               @   sx  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	dDdd„Z
ed	d
„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdEdd„Zdd„ Zd d!„ Zd"d#„ ZeZd$d%„ Zd&d'„ Zd(d)„ ZeZd*d+„ Zd,d-„ Zd.d/„ Z d0d1„ Z!e!Z"d2d3„ Z#e#Z$d4d5„ Z%dFd6d7„Z&dGd8d9„Z'ed:d;„ ƒZ(ed<d=„ ƒZ)ed>d?„ ƒZ*ed@dA„ ƒZ+edBdC„ ƒZ,dS )HÚ	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 k	r6d|kr&t |ƒ\}\}}}}nt|ƒ\}}}}|| _|| _|| _|| _|| _||krrd}t|j||d�ƒ‚||kr�d}t|j||d�ƒ‚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)ÚselfZrange_stringr   r   r   r   r   Úfmt© r   úA/tmp/pip-unpacked-wheel-zhp1095k/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   ÚboundsE   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   r   r   r   ÚcoordM   s    zCellRange.coordc             #   sB   x<t | j| jd ƒD ]&‰ ‡ fdd„t | j| jd ƒD ƒV  qW dS )z1
        Return cell coordinates as rows
        r   c                s   g | ]}ˆ |f‘qS r   r   )Ú.0Úcol)Úrowr   r   ú
<listcomp>d   s    z"CellRange.rows.<locals>.<listcomp>N)Úranger   r   r   r   )r   r   )r!   r   Úrows^   s    zCellRange.rowsc             #   sB   x<t | j| jd ƒD ]&‰ ‡ fdd„t | j| jd ƒD ƒV  qW dS )z4
        Return cell coordinates as columns
        r   c                s   g | ]}|ˆ f‘qS r   r   )r   r!   )r    r   r   r"   m   s    z"CellRange.cols.<locals>.<listcomp>N)r#   r   r   r   r   )r   r   )r    r   Úcolsg   s    zCellRange.colsc             C   s2   ddl m} |t| j| jd ƒt| j| jd ƒƒS )Nr   )Úproductr   )Ú	itertoolsr&   r#   r   r   r   r   )r   r&   r   r   r   Úcellsp   s    zCellRange.cellsc             C   s8   t |tƒsttt|ƒƒƒ‚|jr4| j|jkr4tdƒ‚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_titlev   s    
zCellRange._check_titlec             C   s&   d}| j rd}|j| jj| j | jd�S )Nz<{cls} {coord}>z<{cls} {title!r}!{coord}>)Úclsr   r   )r   r   Ú	__class__Ú__name__r   )r   r   r   r   r   Ú__repr__ƒ   s    zCellRange.__repr__c             C   s*   d}| j }|rd}t|ƒ}|j|| jd�S )Nz{coord}z{title}!{coord})r   r   )r   r
   r   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   )r0   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 )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   Z	col_shiftZ	row_shiftr   r   r   Úshift™   s    zCellRange.shiftc             C   sT   y|   |¡ W n tk
r"   dS X |j| jkpR| j|jkpR|j| jkpR| 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)r.   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: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* == *other*.
        )r6   )r   r-   r   r   r   Ú__eq__Â   s    zCellRange.__eq__c             C   s   |   |¡ | | ¡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__superset)r   r-   r   r   r   ÚissubsetÍ   s    
zCellRange.issubsetc             C   s   |   |¡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__r6   )r   r-   r   r   r   Ú__lt__Ü   s    zCellRange.__lt__c             C   sT   | j |j   ko$|j  ko$| jkn  oR| j|j  koN|j  koN| jkS   S )N)r   r   r   r   )r   r-   r   r   r   Z
__supersetç   s    *zCellRange.__supersetc             C   s   |   |¡ |  |¡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.   r8   )r   r-   r   r   r   Ú
issupersetï   s    
zCellRange.issupersetc             C   s   |   |¡}|  |¡S )zO
        Check whether the range contains a particular cell coordinate
        )r0   r8   )r   r   Úcrr   r   r   Ú__contains__þ   s    
zCellRange.__contains__c             C   s   |   |¡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__r6   )r   r-   r   r   r   Ú__gt__  s    zCellRange.__gt__c             C   sD   |   |¡ | j|jkr ||  } }| j|jk pB| j|jk pB|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   r-   r   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 )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   r   r   r   )
rA   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 )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*.
        )r   r   r   r   r   )	r.   r   r   r   r   r   r   r   r   )r   r-   r   r   r   r   r   r   r   Úunion=  s    

zCellRange.unionc             c   s2   x,| j D ]"}|dkrqt| |ƒ}||fV  qW dS )zC
        For use as a dictionary elsewhere in the library.
        r   N)Ú	__attrs__Úgetattr)r   ÚxÚvr   r   r   Ú__iter__S  s
    
zCellRange.__iter__c             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
        N)r   r   r   r   )r   ÚrightZdownÚleftZupr   r   r   Úexpand^  s    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
        N)r   r   r   r   )r   rI   ÚbottomrJ   Útopr   r   r   Úshrinkq  s    zCellRange.shrinkc             C   s*   | j d | j }| jd | j }||dœS )zC Return the size of the range as a dictionary of rows and columns. r   )Úcolumnsr$   )r   r   r   r   )r   r%   r$   r   r   r   Úsize„  s    zCellRange.sizec                s    ‡ fdd„t ˆ jˆ jd ƒD ƒS )z=A list of cell coordinates that comprise the top of the rangec                s   g | ]}ˆ j |f‘qS r   )r   )r   r    )r   r   r   r"   �  s    z!CellRange.top.<locals>.<listcomp>r   )r#   r   r   )r   r   )r   r   rM   Œ  s    zCellRange.topc                s    ‡ fdd„t ˆ jˆ jd ƒD ƒS )z@A list of cell coordinates that comprise the bottom of the rangec                s   g | ]}ˆ j |f‘qS r   )r   )r   r    )r   r   r   r"   •  s    z$CellRange.bottom.<locals>.<listcomp>r   )r#   r   r   )r   r   )r   r   rL   ’  s    zCellRange.bottomc                s    ‡ fdd„t ˆ jˆ jd ƒD ƒS )zCA list of cell coordinates that comprise the left-side of the rangec                s   g | ]}|ˆ j f‘qS r   )r   )r   r!   )r   r   r   r"   ›  s    z"CellRange.left.<locals>.<listcomp>r   )r#   r   r   )r   r   )r   r   rJ   ˜  s    zCellRange.leftc                s    ‡ fdd„t ˆ jˆ jd ƒD ƒS )zDA list of cell coordinates that comprise the right-side of the rangec                s   g | ]}|ˆ j f‘qS r   )r   )r   r!   )r   r   r   r"   ¡  s    z#CellRange.right.<locals>.<listcomp>r   )r#   r   r   )r   r   )r   r   rI   ž  s    zCellRange.right)NNNNNN)r   r   )r   r   r   r   )r   r   r   r   )-r1   Ú
__module__Ú__qualname__Ú__doc__r   Úintr   r   r   r   r   Úpropertyr   r   r$   r%   r(   r.   r2   r3   r4   r5   r6   r7   r9   r:   r;   r8   r<   r?   r>   r@   rA   rB   Ú__and__rC   Ú__or__rH   rK   rN   rP   rM   rL   rJ   rI   r   r   r   r   r      sP    
			


r   c               @   s|   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
dd„ Zdd„ Zdd„ Zdd„ Zdd„ 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   s   g | ]}t |ƒ‘qS r   )r   )r   Úrr   r   r   r"   ¬  s    z+MultiCellRange.__init__.<locals>.<listcomp>)r)   ÚstrÚsplitÚranges)r   r\   r   r   r   r   ª  s    
zMultiCellRange.__init__c             C   s2   t |tƒrt|ƒ}x| jD ]}||krdS qW dS )NTF)r)   rZ   r   r\   )r   r   rY   r   r   r   r>   °  s    
zMultiCellRange.__contains__c             C   s&   d  dd„ | jD ƒ¡}d | jj|¡S )Nú c             S   s   g | ]}t |ƒ‘qS r   )rZ   )r   rY   r   r   r   r"   º  s    z+MultiCellRange.__repr__.<locals>.<listcomp>z<{0} [{1}]>)Újoinr\   r   r0   r1   )r   r\   r   r   r   r2   ¹  s    zMultiCellRange.__repr__c             C   s   d  dd„ | jD ƒ¡}|S )Nr]   c             S   s   g | ]}t |ƒ‘qS r   )rZ   )r   rY   r   r   r   r"   ¿  s    z*MultiCellRange.__str__.<locals>.<listcomp>)r^   r\   )r   r\   r   r   r   r3   ¾  s    zMultiCellRange.__str__c             C   sB   |}t |tƒrt|ƒ}nt |tƒs*tdƒ‚|| kr>| j |¡ dS )z4
        Add a cell coordinate or CellRange
        zYou can only add CellRangesN)r)   rZ   r   r   r\   Úappend)r   r   r=   r   r   r   ÚaddÅ  s    


zMultiCellRange.addc             C   s   |   |¡ | S )N)r`   )r   r   r   r   r   Ú__iadd__Ò  s    
zMultiCellRange.__iadd__c             C   s    t |tƒr|  |¡}| j|jkS )N)r)   rZ   r0   r\   )r   r-   r   r   r   r7   ×  s    

zMultiCellRange.__eq__c             C   s
   | |k S )Nr   )r   r-   r   r   r   r6   Ý  s    zMultiCellRange.__ne__c             C   s
   t | jƒS )N)Úboolr\   )r   r   r   r   Ú__bool__á  s    zMultiCellRange.__bool__c             C   s"   t |tƒst|ƒ}| j |¡ d S )N)r)   r   r\   Úremove)r   r   r   r   r   rd   å  s    
zMultiCellRange.removec             c   s   x| j D ]
}|V  qW d S )N)r\   )r   r=   r   r   r   rH   ë  s    zMultiCellRange.__iter__c             C   s*   t ƒ }x| jD ]}|j t|ƒ¡ qW |S )N)rX   r\   r_   r   )r   ÚnrY   r   r   r   r4   ð  s    zMultiCellRange.__copy__N)r   )r1   rQ   rR   r   r   r\   r   r>   r2   r3   r`   ra   r7   r6   rc   rd   rH   r4   r   r   r   r   rX   ¤  s   

	rX   N)r   Zopenpyxl.descriptorsr   r   r   Z!openpyxl.descriptors.serialisabler   Zopenpyxl.utilsr   r   r	   r
   r   rX   r   r   r   r   Ú<module>   s      