ó
,º]c           @   s§   d  Z  d d l Z d d l m Z m Z m Z d d l m Z d d l m	 Z	 m
 Z
 m Z m Z d „  Z e j d ƒ Z d „  Z d	 „  Z d
 e j f d „  ƒ  YZ d S(   sL  
For the ``future`` package.

Adds this import line:

    from __future__ import division

at the top and changes any old-style divisions to be calls to
past.utils.old_div so the code runs as before on Py2.6/2.7 and has the same
behaviour on Py3.

If "from __future__ import division" is already in effect, this fixer does
nothing.
iÿÿÿÿN(   t   Leaft   Nodet   Comma(   t
   fixer_base(   t   tokent   future_importt   touch_import_topt   wrap_in_fn_callc         C   s<   t  j } |  j | k o; |  j j | k o; |  j j | k S(   uw   
    __future__.division redefines the meaning of a single slash for division,
    so we match that and only that.
    (   R   t   SLASHt   typet   next_siblingt   prev_sibling(   t   nodet   slash(    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyt   match_division   s    	"s   ^[0-9]*[.][0-9]*$c         C   s   t  |  j ƒ p t  |  j ƒ S(   N(   t
   _is_floatyR   R
   (   R   (    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyt	   is_floaty"   s    c         C   s{   t  |  t ƒ r |  d }  n  t  |  t ƒ r; t j |  j ƒ St  |  t ƒ rw t  |  j d t ƒ rw |  j d j d k Sn  t S(   Ni    u   float(	   t
   isinstancet   listR    t   const_ret   matcht   valueR   t   childrent   False(   t   expr(    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyR   &   s    t   FixDivisionSafec           B   s8   e  Z d  Z e j Z d Z d „  Z d „  Z d „  Z	 RS(   i   s4   
    term<(not('/') any)+ '/' ((not('/') any))>
    c         C   s/   t  t |  ƒ j | | ƒ d | j k |  _ d S(   sO   
        Skip this fixer if "__future__.division" is already imported.
        t   divisionN(   t   superR   t
   start_treet   future_featurest   skip(   t   selft   treet   name(    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyR   >   s    c         C   sì   | j  |  j j k rè t } t } g  } xœ | j D]‘ } | rI t } q1 n  t | ƒ r¯ t | ƒ r¯ t } d | d _ t	 d | t
 ƒ  | j j ƒ  g d | j ƒg } t } q1 | j | j ƒ  ƒ q1 W| rè t | j  | d | j ƒSn  t S(   u™   
        Since the tree needs to be fixed once and only once if and only if it
        matches, we can start discarding matches after the first.
        u    i    t   old_divt   prefixt   fixers_applied(   R	   t   symst   termR   R   R   R   t   TrueR#   R   R   R
   t   clonet   appendR   R$   (   R   R   t   matchedR   R   t   child(    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyR   E   s&    	c         C   s.   |  j  r d  St d | ƒ t d d | ƒ | S(   Nu   divisionu
   past.utilsu   old_div(   R   R   R   (   R   R   t   results(    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyt	   transformc   s
    	(
   t   __name__t
   __module__t	   run_orderR   R   t   _accept_typet   PATTERNR   R   R-   (    (    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyR   4   s   			(   t   __doc__t   ret   lib2to3.fixer_utilR    R   R   t   lib2to3R   t   libfuturize.fixer_utilR   R   R   R   R   t   compileR   R   R   t   BaseFixR   (    (    (    sG   /usr/lib/python2.7/site-packages/libfuturize/fixes/fix_division_safe.pyt   <module>   s   "				