î
æ^Q\7  ã               @   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	 Gd d „  d e
 ƒ Z Gd d	 „  d	 e ƒ Z d S)
é    )Úabsolute_importNé   )Ú	TokenizerÚToken)Úcoordinate_from_stringÚcolumn_index_from_stringÚget_column_letterc               @   s   e  Z d  Z d Z d S)ÚTranslatorErrora  
    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.

    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úO/var/www/dbchiro/venv/lib/python3.4/site-packages/openpyxl/formula/translate.pyr	      s   r	   c               @   s¯   e  Z d  Z d Z d d „  Z d d „  Z e j d ƒ Z e j d ƒ Z	 e j d ƒ Z
 e d	 d
 „  ƒ Z e d d „  ƒ Z e d d „  ƒ Z e d d „  ƒ Z d d „  Z d S)Ú
Translatora9  
    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   s7   t  | ƒ \ } |  _ t | ƒ |  _ t | ƒ |  _ d  S)N)r   Úrowr   Úcolr   Ú	tokenizer)ÚselfZformulaÚoriginr   r   r   r   Ú__init__,   s    zTranslator.__init__c             C   s
   |  j  j S)z6Returns a list with the tokens comprising the formula.)r   Úitems)r   r   r   r   Ú
get_tokens4   s    zTranslator.get_tokensz*(\$?[1-9][0-9]{0,6}):(\$?[1-9][0-9]{0,6})$z&(\$?[A-Za-z]{1,3}):(\$?[A-Za-z]{1,3})$z'(\$?[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)zL
        Translate a range row-snippet by the given number of rows.
        ú$r   zFormula out of rangeN)Ú
startswithÚintr	   Ústr)Zrow_strÚrdeltaZnew_rowr   r   r   Útranslate_row<   s    zTranslator.translate_rowc             C   sP   |  j  d ƒ r |  Sy t t |  ƒ | ƒ SWn t k
 rK t d ƒ ‚ Yn Xd S)zN
        Translate a range col-snippet by the given number of columns
        r   zFormula out of rangeN)r   r   r   Ú
ValueErrorr	   )Zcol_strÚcdeltar   r   r   Útranslate_colI   s    zTranslator.translate_colc             C   s<   d |  k r2 |  j  d d ƒ \ } }  | d |  f Sd |  f S)zCSplits out the worksheet reference, if any, from a range reference.ú!r   Ú )Úrsplit)Ú	range_strZsheetr   r   r   Ústrip_ws_nameW   s    	zTranslator.strip_ws_namec                s\  ˆ 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 d †  | j d ƒ Dƒ ƒ Sˆ j	 j | ƒ } | d k r&| S| ˆ j | j d ƒ ˆ  ƒ ˆ j | j d ƒ ˆ ƒ S)aV  
        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.

        Nr   ú:é   c             3   s$   |  ] } ˆ j  | ˆ ˆ  ƒ Vq d  S)N)Útranslate_range)Ú.0Zpiece)r    Úclsr   r   r   ú	<genexpr>€   s   z-Translator.translate_range.<locals>.<genexpr>)
r&   ÚROW_RANGE_REÚmatchr   ÚgroupÚCOL_RANGE_REr!   ÚjoinÚsplitÚCELL_REF_RE)r+   r%   r   r    Zws_partr.   r   )r    r+   r   r   r)   e   s    ::	zTranslator.translate_rangec       	      C   sì   |  j  ƒ  } | s d S| d j t j k r7 | d j Sd g } t | ƒ \ } } t | ƒ } | |  j } | |  j } xd | D]\ } | j t j	 k rË | j
 t j k rË | j |  j | j | | ƒ ƒ q | j | j ƒ q Wd j | ƒ S)z»
        Convert the formula into A1 notation.

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

        r#   r   ú=)r   Útyper   ÚLITERALÚvaluer   r   r   r   ZOPERANDÚsubtypeÚRANGEÚappendr)   r1   )	r   ÚdestÚtokensÚoutZdcolZdrowZ	row_deltaZ	col_deltaÚtokenr   r   r   Útranslate_formulaˆ   s     	$zTranslator.translate_formulaN)r
   r   r   r   r   r   ÚreÚcompiler-   r0   r3   Ústaticmethodr   r!   r&   Úclassmethodr)   r?   r   r   r   r   r       s   
#r   )Ú
__future__r   r@   r   r   r   Zopenpyxl.utilsr   r   r   Ú	Exceptionr	   Úobjectr   r   r   r   r   Ú<module>   s
   