a
    V$c                     @   s   d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlZd dlZd dlZd dlZdd Zdd Zdd	 Zd
d ZG dd dejZe Zdd ZdS )    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeNc                 C   sx   | d }g }| D ]b}|j tjkrd|j tjkrd|jdkrd|  ttj|jd |j |j|j|j	}|
| |}q|S )z
    This fixes python-2 style octal numbers. Tokenize seems to report this
    as two numbers, the first of which has a string of '0'. This merges that
    with the next token.
    r   0o)typetokenNUMBERstringpoptokenize	TokenInfostartendlineappendtokensoldrvnew r%   renpy/compat\fixes.pyfix_octal_numbers    s    "&
r'   c                 C   sb   | d }g }| D ]L}|j tjkrN|j tjkrN|  ttjd|j|j	|j
}|| |}q|S )z:
    This fixes the Python 2 spaceship operator (<>).
    r   z!=)
exact_typer   LESSGREATERr   r   r   OPr   r   r   r   r    r%   r%   r&   fix_spaceship5   s    
r,   c                 C   s   dd }g }g }| D ]}|s|j tjkr4|| q|j tjkrL|| q|j tjkrd|| q|j tjkr||| q|j tjkr|| q|| |jtj	krq|
|| g }q|
|| |S )z?
    This tries to remove Python 2-style print statements.
    c              
   S   s   t | dk r| S | d jtjkr$| S | d jdkr6| S | d jtjkrJ| S | d jtjkrh| dd  }n| dd  }| d }|dt	
tjd|j|j|j |dt	
tjd|j|j|j |S )N   r   print   r   ,)lenr   r   NAMEr   r(   LPAR
RIGHTSHIFTinsertr   r   r   r   r   r+   r   )r   newliner"   r%   r%   r&   fix_lineN   s    ""zfix_print.<locals>.fix_line)r(   r   NLr   INDENTDEDENT	ENDMARKERENCODINGr   NEWLINEextend)r!   r7   r#   r   ir%   r%   r&   	fix_printI   s6    





r@   c              
   C   s   zXt r| W S t| d}tt|j}t|}t|}t	|}t
|d}|W S  ty } z|W Y d}~n
d}~0 0 dS )z
    This applies fixes that will help python 2 code run under python 3. Not all
    problem will be fixed, but this will attempt to handle common issues.

    These are fixes that apply at the source code level.
    zutf-8N)r   ioBytesIOencodelistr   readliner'   r,   r@   
untokenizedecode	Exception)sourcebior!   r#   er%   r%   r&   
fix_tokens   s    rL   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ReorderGlobalsz
    This removes all global statements from functions, and places the variables
    therein in a new global statement on the first line of the function.
    c                 C   s   t  | _d S N)setglobals)selfr%   r%   r&   __init__   s    zReorderGlobals.__init__c                 C   s    |j D ]}| j| qt S rN   )namesrP   addastPass)rQ   nr?   r%   r%   r&   visit_Global   s    
zReorderGlobals.visit_Globalc                 C   sV   | j }zB| |}t| j }|  |r>|jdtj|d |W || _ S || _ 0 d S )Nr   )rS   )rP   generic_visitrD   sortbodyr5   rU   Global)rQ   rW   old_globalsnew_globalsr%   r%   r&   visit_FunctionDef   s    

z ReorderGlobals.visit_FunctionDefN)__name__
__module____qualname____doc__rR   rX   r_   r%   r%   r%   r&   rM      s   rM   c              
   C   sF   t r| S zt| } | W S  ty@ } z|W Y d}~n
d}~0 0 dS )z
    This applies fixes that will help python 2 code run under python 3. Not all
    problems will be fixed, but this will attempt to handle common issues.

    These are fixes that apply at the AST level.
    N)r   reorder_globalsvisitrH   )treerK   r%   r%   r&   fix_ast   s    
rg   )
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   rA   rU   r'   r,   r@   rL   NodeTransformerrM   rd   rg   r%   r%   r%   r&   <module>   s   8F!