U
    ËZjÜ  ã                   @   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d„ Ze d¡Zd	d
„ Zdd„ ZG dd„ dejƒZdS )aL  
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.
é    N)ÚLeafÚNodeÚComma)Ú
fixer_base)ÚtokenÚfuture_importÚtouch_import_topÚwrap_in_fn_callc                 C   s,   t j}| j|ko*| jj|k o*| jj|k S )zw
    __future__.division redefines the meaning of a single slash for division,
    so we match that and only that.
    )r   ÚSLASHÚtypeÚnext_siblingÚprev_sibling)ÚnodeÚslash© r   úf/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/libfuturize/fixes/fix_division_safe.pyÚmatch_division   s    ÿr   z^[0-9]*[.][0-9]*$c                 C   s   t | jƒpt | jƒS )N)Ú
_is_floatyr   r   )r   r   r   r   Ú	is_floaty"   s    r   c                 C   sV   t | tƒr| d } t | tƒr(t | j¡S t | tƒrRt | jd tƒrR| jd jdkS dS )Nr   ÚfloatF)Ú
isinstanceÚlistr   Úconst_reÚmatchÚvaluer   Úchildren)Úexprr   r   r   r   &   s    


r   c                       s:   e Zd ZdZejZdZ‡ fdd„Zdd„ Z	dd„ Z
‡  ZS )	ÚFixDivisionSafeé   z4
    term<(not('/') any)+ '/' ((not('/') any))>
    c                    s"   t t| ƒ ||¡ d|jk| _dS )zO
        Skip this fixer if "__future__.division" is already imported.
        ÚdivisionN)Úsuperr   Ú
start_treeZfuture_featuresÚskip)ÚselfÚtreeÚname©Ú	__class__r   r   r!   >   s    zFixDivisionSafe.start_treec                 C   sž   |j | jjkršd}d}g }|jD ]b}|r.d}q t|ƒrtt|ƒstd}d|d _td|tƒ |j	 
¡ g |jd�g}d}q | | 
¡ ¡ q |ršt|j ||jd�S dS )z™
        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.
        FTÚ r   Úold_div)Úprefix)Úfixers_applied)r   ZsymsÚtermr   r   r   r*   r	   r   r   ÚcloneÚappendr   r+   )r#   r   Zmatchedr"   r   Úchildr   r   r   r   E   s(    

þzFixDivisionSafe.matchc                 C   s$   | j r
d S td|ƒ tdd|ƒ |S )Nr   z
past.utilsr)   )r"   r   r   )r#   r   Úresultsr   r   r   Ú	transformc   s
    
zFixDivisionSafe.transform)Ú__name__Ú
__module__Ú__qualname__Z	run_orderr   r
   Z_accept_typeZPATTERNr!   r   r1   Ú__classcell__r   r   r&   r   r   4   s   r   )Ú__doc__ÚreZlib2to3.fixer_utilr   r   r   Zlib2to3r   Zlibfuturize.fixer_utilr   r   r   r	   r   Úcompiler   r   r   ZBaseFixr   r   r   r   r   Ú<module>   s   	
