
^Q\"1                 @   s   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 Gd d   d e  Z Gd 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 d d d e  Z e d d d d d e  Z e d d d d d e  Z e d d d d d e  Z	 d d d d d d d	 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 d d d 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" d d d d d0 d1  Z# d d d d d2 d3  Z$ e d4 d5    Z% d S)6	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,

    min   maxifG  expected_typei   Nc             C   s   | d  k	 r^ y" t  |  \ } \ } } } } Wq^ t k
 rZ t |  \ } } } } Yq^ Xn  | |  _ | |  _ | |  _ | |  _ | |  _ | | k r d } t | j d | d |    n  | | k r d } t | j d | d |    n  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)selfZrange_stringr   r   r   r   r   fmt r   R/var/www/dbchiro/venv/lib/python3.4/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   sm   d } |  j  |  j k r3 |  j |  j k r3 d } n  | j d t |  j   d |  j d t |  j  d |  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   r
   )r   r   r   r   r   coordL   s    			zCellRange.coordc             C   sX   t  | t  s* t t t |     n  | j rT |  j | j k rT t d   n  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   sF   d } |  j  r d } n  t | j d |  j j d |  j  d |  j   S)Nz<{cls} {coord}>z<{cls} {title!r}!{coord}>clsr   r   )r   r   r   	__class____name__r   )r   r   r   r   r   __repr__k   s    		zCellRange.__repr__c             C   sI   d } |  j  } |  j  r0 d } t |  j   } n  | j d | d |  j  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   |  j    } t |  S)N)r)   r   )r   r   r   r   r   __str__}   s    zCellRange.__str__c             C   s7   |  j  d |  j d |  j d |  j d |  j d |  j  S)Nr   r   r   r   r   )r&   r   r   r   r   r   )r   r   r   r   __copy__   s    zCellRange.__copy__r   c             C   s   |  j  | d k s& |  j | d k rA t d j | |    n  |  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   Z	col_shiftZ	row_shiftr   r   r   shift   s    zCellRange.shiftc             C   sp   y |  j  |  Wn t k
 r) d SYn X| j |  j k po |  j | j k po | j |  j k po |  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   r   __ne__   s    	zCellRange.__ne__c             C   s   |  j  |  S)z
        Test whether the ranges are equal.

        :type other: CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* == *other*.
        )r-   )r   r#   r   r   r   __eq__   s    zCellRange.__eq__c             C   so   |  j  |  | j |  j k o; |  j k o; | j k n on | j |  j k ol |  j k ol | 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   r   issubset   s    3zCellRange.issubsetc             C   s   |  j  |  o |  j |  S)z
        Test whether every element in the range is in *other*, but not all.

        :type other: CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* < *other*.
        )__le__r-   )r   r#   r   r   r   __lt__   s    zCellRange.__lt__c             C   so   |  j  |  |  j | j k o; | j k o; |  j k n on |  j | j k ol | j k ol |  j k SS)a  
        Test whether every element in *other* is in the range.

        :type other: 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*).
        )r$   r   r   r   r   )r   r#   r   r   r   
issuperset   s    3zCellRange.issupersetc             C   s:   |  j  |  } | j d k r- |  j | _ n  |  j |  S)zO
        Check whether the range contains a particular cell coordinate
        N)r&   r   r2   )r   r   crr   r   r   __contains__   s    zCellRange.__contains__c             C   s   |  j  |  o |  j |  S)z
        Test whether every element in *other* is in the range, but not all.

        :type other: CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* > *other*.
        )__ge__r-   )r   r#   r   r   r   __gt__   s    zCellRange.__gt__c             C   sV   |  j  |  |  j | j k r4 |  } | }  | } n  |  j |  j f | j | j f k  S)a;  
        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: 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   s   |  j  |  r* t d j |  |    n  t |  j | j  } t |  j | j  } t |  j | j  } t |  j | j  } t	 d | d | d | d |  S)a;  
        Return a new range with elements common to the range and another

        :type others: tuple[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   r   r   r   )
r8   r   r   r   r   r   r   r   r   r   )r   r#   r   r   r   r   r   r   r   intersection  s    
zCellRange.intersectionc             C   s   |  j  |  t |  j | j  } t |  j | j  } t |  j | j  } t |  j | j  } t d | d | d | d | d |  j  S)z
        Return a new range with elements from the range and all *others*.

        :type others: tuple[CellRange]
        :param others: Other sheet ranges.
        :return: the current sheet range.
        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   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   expandD  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   r;   Zbottomr<   topr   r   r   shrinkW  s    zCellRange.shrinkc             C   s:   |  j  d |  j } |  j d |  j } i | d 6| d 6S)zD Return the size of the range as a dicitionary of rows and columns. r   columnsrows)r   r   r   r   )r   colsrA   r   r   r   sizej  s    zCellRange.size)&r'   
__module____qualname____doc__r   intr   r   r   r   r   propertyr   r   r$   r(   r)   __unicode__r*   r+   r,   r-   r.   r/   r0   r1   r2   r5   r4   r6   r8   r9   __and__r:   __or__r=   r?   rC   r   r   r   r   r      sB   	
r   c               @   s   e  Z d  Z e d e  Z f  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)MultiCellRanger   c             C   s8   t  | t  r+ d d   | j   D } n  | |  _ d  S)Nc             S   s   g  |  ] } t  |   q Sr   )r   ).0rr   r   r   
<listcomp>z  s   	 z+MultiCellRange.__init__.<locals>.<listcomp>)r   strsplitranges)r   rR   r   r   r   r   x  s    zMultiCellRange.__init__c             C   s(   x! |  j  D] } | | k r
 d Sq
 Wd S)NTF)rR   )r   r   rN   r   r   r   r4   ~  s    zMultiCellRange.__contains__c             C   s5   d j  d d   |  j D  } d j |  j j |  S)N c             S   s   g  |  ] } t  |   q Sr   )rP   )rM   rN   r   r   r   rO     s   	 z+MultiCellRange.__repr__.<locals>.<listcomp>z<{0} [{1}]>)joinrR   r   r&   r'   )r   rR   r   r   r   r(     s    zMultiCellRange.__repr__c             C   s#   d j  d d   |  j D  } | S)NrS   c             S   s   g  |  ] } t  |   q Sr   )rP   )rM   rN   r   r   r   rO     s   	 z*MultiCellRange.__str__.<locals>.<listcomp>)rT   rR   )r   rR   r   r   r   r*     s    zMultiCellRange.__str__c             C   s>   | |  k r: t  |  } |  j } | j |  | |  _ n  |  S)zD
        Add a cell coordinate. Will create a new CellRange
        )r   rR   append)r   r   r3   rR   r   r   r   add  s    	zMultiCellRange.addc             C   s1   t  | t  r! |  j |  } n  |  j | j k S)N)r   rP   r&   rR   )r   r#   r   r   r   r.     s    zMultiCellRange.__eq__c             C   s   |  | k S)Nr   )r   r#   r   r   r   r-     s    zMultiCellRange.__ne__c             C   s   t  |  j  S)N)boolrR   )r   r   r   r   __bool__  s    zMultiCellRange.__bool__c             C   s2   t  | t  s t |  } n  |  j j |  d  S)N)r   r   rR   remove)r   r   r   r   r   rY     s    zMultiCellRange.removec             c   s   x |  j  D] } | Vq
 Wd  S)N)rR   )r   r3   r   r   r   __iter__  s    zMultiCellRange.__iter__N)r'   rD   rE   r   r   rR   r   r4   r(   r*   rI   rV   __iadd__r.   r-   rX   Z__nonzero__rY   rZ   r   r   r   r   rL   r  s   rL   N)
__future__r   r   Zopenpyxl.compat.stringsr   Zopenpyxl.descriptorsr   r   r   Zopenpyxl.utilsr   r	   r
   r   r   rL   r   r   r   r   <module>   s   " c