σ
]c           @@  s~   d  d l  m Z d  d l Z d d l m Z m Z d  d l m Z m Z m	 Z	 d e
 f d     YZ d e f d	     YZ d S(
   i    (   t   absolute_importNi   (   t	   Tokenizert   Token(   t   coordinate_to_tuplet   column_index_from_stringt   get_column_lettert   TranslatorErrorc           B@  s   e  Z d  Z RS(   s  
    Raised when a formula can't be translated across cells.

    This error arises when a formula's references would be translated outside
    the worksheet's bounds on the top or left. Excel represents these
    situations with a #REF! literal error. E.g., if the formula at B2 is
    '=A1', attempting to translate the formula to B1 raises TranslatorError,
    since there's no cell above A1. Similarly, translating the same formula
    from B2 to A2 raises TranslatorError, since there's no cell to the left of
    A1.

    (   t   __name__t
   __module__t   __doc__(    (    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyR      s   t
   Translatorc           B@  s   e  Z d  Z d   Z d   Z e j d  Z e j d  Z e j d  Z	 e
 d    Z e
 d    Z e
 d    Z e d	    Z d d
 d
 d  Z RS(   s9  
    Modifies a formula so that it can be translated from one cell to another.

    `formula`: The unicode string to translate. Must include the leading '='
               character.
    `origin`: The cell address (in A1 notation) where this formula was
              defined (excluding the worksheet name).

    c         C@  s+   t  |  \ |  _ |  _ t |  |  _ d  S(   N(   R   t   rowt   colR   t	   tokenizer(   t   selft   formulat   origin(    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   __init__/   s    c         C@  s
   |  j  j S(   s6   Returns a list with the tokens comprising the formula.(   R   t   items(   R   (    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt
   get_tokens6   s    s*   (\$?[1-9][0-9]{0,6}):(\$?[1-9][0-9]{0,6})$s&   (\$?[A-Za-z]{1,3}):(\$?[A-Za-z]{1,3})$s'   (\$?[A-Za-z]{1,3})(\$?[1-9][0-9]{0,6})$c         C@  sL   |  j  d  r |  St |   | } | d k r> t d   n  t |  Sd S(   sL   
        Translate a range row-snippet by the given number of rows.
        t   $i    s   Formula out of rangeN(   t
   startswitht   intR   t   str(   t   row_strt   rdeltat   new_row(    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   translate_row>   s    c         C@  sO   |  j  d  r |  Sy t t |   |  SWn t k
 rJ t d   n Xd S(   sN   
        Translate a range col-snippet by the given number of columns
        R   s   Formula out of rangeN(   R   R   R   t
   ValueErrorR   (   t   col_strt   cdelta(    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   translate_colK   s    c         C@  s<   d |  k r2 |  j  d d  \ } }  | d |  f Sd |  f S(   sC   Splits out the worksheet reference, if any, from a range reference.t   !i   t    (   t   rsplit(   t	   range_strt   sheet(    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   strip_ws_nameY   s    	c         @  sY   j  |  \ } }  j j |  } | d k	 rm |  j | j d    d  j | j d    S j j |  } | d k	 rΕ |  j | j d     d  j | j d     Sd | k r| d j     f d   | j	 d  D  S j
 j |  } | d k r#| S|  j | j d      j | j d    S(   sV  
        Translate an A1-style range reference to the destination cell.

        `rdelta`: the row offset to add to the range
        `cdelta`: the column offset to add to the range
        `range_str`: an A1-style reference to a range. Potentially includes
                     the worksheet reference. Could also be a named range.

        i   t   :i   c         3@  s$   |  ] }  j  |     Vq d  S(   N(   t   translate_range(   t   .0t   piece(   R   t   clsR   (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pys	   <genexpr>   s   N(   R%   t   ROW_RANGE_REt   matcht   NoneR   t   groupt   COL_RANGE_RER   t   joint   splitt   CELL_REF_RE(   R*   R#   R   R   t   ws_partR,   (    (   R   R*   R   s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyR'   g   s    ::	i    c   	      C@  sι   |  j    } | s d S| d j t j k r7 | d j Sd g } | ru t |  \ } } | |  j } | |  j } n  xd | D]\ } | j t j k rΘ | j	 t j
 k rΘ | j |  j | j | |   q| | j | j  q| Wd j |  S(   sά   
        Convert the formula into A1 notation, or as row and column coordinates

        The formula is converted into A1 assuming it is assigned to the cell
        whose address is `dest` (no worksheet name).

        R!   i    t   =(   R   t   typeR   t   LITERALt   valueR   R   R   t   OPERANDt   subtypet   RANGEt   appendR'   R0   (	   R   t   destt	   row_deltat	   col_deltat   tokenst   outR   R   t   token(    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   translate_formula   s"    	N(   R   R   R	   R   R   t   ret   compileR+   R/   R2   t   staticmethodR   R   R%   t   classmethodR'   R-   RB   (    (    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyR
   #   s   
		#(   t
   __future__R    RC   R   R   R   t   openpyxl.utilsR   R   R   t	   ExceptionR   t   objectR
   (    (    (    s9   lib/python2.7/site-packages/openpyxl/formula/translate.pyt   <module>   s
   