
nR\                 @   s  d  d l  Z  d d l m Z m Z d d l m Z e  j d e  j  Z e  j d  Z	 e  j d e  j  Z
 e  j d e  j  Z e  j d	 e  j  Z d
 d   Z d d   Z d d   Z d d d d  Z d d d d d d  Z d d   Z d d   Z d d   Z d d   Z d S)    N   )MAX_ROWMAX_COL)xrangez(\$?)([A-I]?[A-Z])(\$?)(\d+)z\$?(\d+):\$?(\d+)z!\$?([A-I]?[A-Z]):\$?([A-I]?[A-Z])z-\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)z\$?([A-I]?[A-Z]\$?\d+)c             C   sq   d } d } xZ t  t |   d d d  D]< } |  | } | t |  t d  d | 7} | d 9} q) W| d S)z)'A' -> 0, 'Z' -> 25, 'AA' -> 26, etc
    r   r   A   r   )r   lenord)Zcolnamecolpowerich r   ./var/www/dbchiro/venv/build/xlwt/xlwt/Utils.pycol_by_name   s    #
"r   c             C   s   t  j |   } | s( t d |    n  | j   \ } } } } t |  } t |  } t |  d } t | j    } | | | | f S)zConvert an Excel cell reference string in A1 notation
    to numeric row/col notation.

    Returns: row, col, row_abs, col_abs

    z$Ill-formed single_cell reference: %sr   )_re_cell_exmatch	Exceptiongroupsboolintr   upper)cellmcol_absr   row_absrowr   r   r   cell_to_rowcol   s    r   c             C   sh   t  j |   } | s$ t d   n  | j   \ } } } } t |  d } t | j    } | | f S)zsConvert an Excel cell reference string in A1 notation
    to numeric row/col notation.

    Returns: row, col

    zError in cell formatr   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   cell_to_rowcol2.   s    r   Fc             C   s   d |  k o t  k  n s" t  d | k o9 t k  n sD t  | d } | d } d } | rm d } n d } | r d } n d } | d k r t t d  | d  } n  t t d  |  } | | | | t |  d  S)z\Convert numeric row/col notation to an Excel cell reference string in
    A1 notation.

    r   r    $r   r   )r   AssertionErrorr   chrr
   str)r   r   r   r   dr   Zchr1Zchr2r   r   r   rowcol_to_cell@   s    ""

		r&   c             C   sN   |  | k s t   | | k s$ t   t |  | | |  d t | | | |  S)zsConvert two (row,column) pairs
    into a cell range string in A1:B2 notation.

    Returns: cell range string
    :)r"   r&   )row1col1row2col2Zrow1_absZcol1_absZrow2_absZcol2_absr   r   r   rowcol_pair_to_cellrangeX   s    r,   c             C   s  |  j    }  t j |   } | ro t | j d   d } d } t | j d   d } d } | | | | f St j |   } | r t | j d  j     } d } t | j d  j     } d } | | | | f St j |   } | r1t | j d   \ } } t | j d   \ } } | | | | f St	 j |   } | rqt | j d   \ } } | | | | f St
 d |    d S)zpConvert cell range string in A1 notation to numeric row/col
    pair.

    Returns: row1, col1, row2, col2

    r   r      zUnknown cell reference %sNr   r   )r   _re_row_ranger   r   group_re_col_ranger   _re_cell_ranger   _re_cell_refr   )Z	cellrangeresr(   r)   r*   r+   r   r   r   cellrange_to_rowcol_pairg   s2    r4   c             C   s   t  |   \ } } } } | t k r7 t d |    n  | t k r\ t d |  t f   n  | t |  d >O} | t |  d >O} | | f S)z5 pack row and column into the required 4 byte format z$Column %s greater than IV in formulaz!Row %s greater than %d in formula      )r   r   r   r   r   )r   r   r   r   r   r   r   r   cell_to_packed_rowcol   s    r7   c             C   sW   |  d k s. |  d d k s. t  |   d k r2 d Sx |  D] } | d k r9 d Sq9 Wd S)Nr    r   '   Fz[]:\?/* T)r	   )Z
sheet_namecr   r   r   valid_sheet_name   s    .r;   c             C   s7   t  |   s t d |    n  d |  j d d  d S)Nz-attempt to quote an invalid worksheet name %rr8   z'')r;   r   replace)Zunquoted_sheet_namer   r   r   quote_sheet_name   s    r=   )reZ
ExcelMagicr   r   compatr   compile
IGNORECASEr   r.   r0   r1   r2   r   r   r   r&   r,   r4   r7   r;   r=   r   r   r   r   <module>   s    '