????

Your IP : 3.23.92.150


Current Path : /usr/lib/python3.6/site-packages/past/translation/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/past/translation/__pycache__/__init__.cpython-36.pyc

3

�c�D�#@sbdZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZej
e�Zejej�eej�eej�eej�eej�Zdddd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(g#ZGd)d*�d*�Zd+d,�Zd-d.�Zd/d0�ZGd1d2�d2e�Ze�Zfffd3d4�Z d5d6�Z!d7d8�Z"Gd9d:�d:e�Z#Gd;d<�d<e�Z$e Z%dS)=a'
past.translation
==================

The ``past.translation`` package provides an import hook for Python 3 which
transparently runs ``futurize`` fixers over Python 2 code on import to convert
print statements into functions, etc.

It is intended to assist users in migrating to Python 3.x even if some
dependencies still only support Python 2.x.

Usage
-----

Once your Py2 package is installed in the usual module search path, the import
hook is invoked as follows:

    >>> from past.translation import autotranslate
    >>> autotranslate('mypackagename')

Or:

    >>> autotranslate(['mypackage1', 'mypackage2'])

You can unregister the hook using::

    >>> from past.translation import remove_hooks
    >>> remove_hooks()

Author: Ed Schofield.
Inspired by and based on ``uprefix`` by Vinay M. Sajip.
�N)�
ParseError)�RefactoringTool)�fixeszlib2to3.fixes.fix_applyzlib2to3.fixes.fix_exceptzlib2to3.fixes.fix_execfilezlib2to3.fixes.fix_exitfunczlib2to3.fixes.fix_funcattrszlib2to3.fixes.fix_filterzlib2to3.fixes.fix_has_keyzlib2to3.fixes.fix_idiomszlib2to3.fixes.fix_importzlib2to3.fixes.fix_internzlib2to3.fixes.fix_isinstancezlib2to3.fixes.fix_methodattrszlib2to3.fixes.fix_nezlib2to3.fixes.fix_numliteralszlib2to3.fixes.fix_parenzlib2to3.fixes.fix_printzlib2to3.fixes.fix_raisezlib2to3.fixes.fix_renameszlib2to3.fixes.fix_reducezlib2to3.fixes.fix_reprzlib2to3.fixes.fix_standarderrorzlib2to3.fixes.fix_sys_exczlib2to3.fixes.fix_throwzlib2to3.fixes.fix_tuple_paramszlib2to3.fixes.fix_typeszlib2to3.fixes.fix_ws_commazlib2to3.fixes.fix_xreadlineszlib2to3.fixes.fix_basestringzlib2to3.fixes.fix_execzlib2to3.fixes.fix_getcwduzlib2to3.fixes.fix_longzlib2to3.fixes.fix_nextzlib2to3.fixes.fix_nonzerozlib2to3.fixes.fix_raw_inputzlib2to3.fixes.fix_xrangec@s8eZdZdZdZdZdZdZedd��Z	edd��Z
dS)�RTsa&
    A namespace for the refactoring tools. This avoids creating these at
    the module level, which slows down the module import. (See issue #117).

    There are two possible grammars: with or without the print statement.
    Hence we have two possible refactoring tool implementations.
    NcCs.dtjtjgkr*tt�t_ttddi�t_dS)zj
        Call this before using the refactoring tools to create them on demand
        if needed.
        N�print_functionT)r�_rt�_rtpr�myfixes�r
r
�/usr/lib/python3.6/__init__.py�setup�s
z	RTs.setupcCs.dtjtjgkr*tt�t_ttddi�t_dS)zj
        Call this before using the refactoring tools to create them on demand
        if needed.
        NrT)r�_rt_py2_detect�_rtp_py2_detectr�py2_detect_fixersr
r
r
r�setup_detect_python2�s
zRTs.setup_detect_python2)�__name__�
__module__�__qualname__�__doc__rrr
r�staticmethodrrr
r
r
rrvsrcCsrg}xhtjj|�}|d|kr2|jd|d�Pq|d|krR|jd|d�Pq|d}|jd|d�qW|S)zA
    Split a path into all components. From Python Cookbook.
    r�)�os�path�split�insert)rZallparts�partsr
r
r�splitall�srcCsLg}t|�}t|�}x*t||�D]\}}||kr2P|j|�q Wtjj|�S)z^
    Returns the longest common substring to the two strings, starting from the
    left.
    )r�zip�appendrr�join)�s1�s2ZchunksZpath1Zpath2Zdir1Zdir2r
r
r�common_substring�sr"cCs�tj�ytjj||�}WnFtk
r`}z*|jdks@|jdkrB�tjj||�}WYdd}~XnX|t|�dd	�kr�t	j
dj|��dSt	j
dj|��dSdS)
zD
    Returns a bool indicating whether we think the code is Py2
    z	bad input�=NrzDetected Python 2 code: {0}TzDetected Python 3 code: {0}F���)rrr
�refactor_stringr�msg�valuer�str�logger�debug�format)�source�pathname�tree�er
r
r�detect_python2�s r0c@sFeZdZdZdZdd�Zdd�Zdd�Zdd
d�Zdd
�Z	dd�Z
d	S)�Py2Fixerzi
    An import hook class that uses lib2to3 for source-to-source translation of
    Py2 code to Py3.
    TcCs(d|_ddg|_tj|j�|_g|_dS)NZfutureZpast)�foundZbase_exclude_paths�copy�
exclude_paths�
include_paths)�selfr
r
r�__init__�s
zPy2Fixer.__init__cCs|j|7_dS)z�
        Pass in a sequence of module names such as 'plotrique.plotting' that,
        if present at the leftmost side of the full package name, would
        specify the module to be transformed from Py2 to Py3.
        N)r5)r6�pathsr
r
r�include�szPy2Fixer.includecCs|j|7_dS)z�
        Pass in a sequence of strings such as 'mymodule' that, if
        present at the leftmost side of the full package name, would cause
        the module not to undergo any source transformation.
        N)r4)r6r8r
r
r�exclude�szPy2Fixer.excludeNcCs�tjdj|��d|krP|jdd�\}}|dkrL|j||�}|j|�}|j}|}ytj||�|_Wn<t	k
r�}z tjd�tjdj||��dSd}~XnX|jdd|_
|j
tjkr�tj
j|jdd�|_n|j
tjkr�|jd|_|S)	NzRunning find_module: {0}...�.rzPy2Fixer could not find {0}zException was: {0})z__init__.pyr$r$)r)r*r+�rsplit�find_module�load_module�__path__�impr2�	Exception�kind�
PKG_DIRECTORYrrrr-Z	PY_SOURCE)r6�fullnamer�parentZchild�loader�modr/r
r
rr=s(

zPy2Fixer.find_modulecCs~tj�|d7}ytjj||j�}WnHtk
rl}z,|jdksJ|jdkrL�tjj||j�}WYdd}~XnXt	|�dd�S)N�
z	bad inputr#rr$)
rrrr%r-rr&r'rr()r6r,r.r/r
r
r�	transforms"zPy2Fixer.transformc,s�tjdj����tjkr(tj�}�n�|jtjtjtj	tj
fkrHd}n@t�fdd�|jD��rfd}n"t�fdd�|j
D��r�d}nd}|s�tjdj���tj�f|j��}�n&tjdj���tj��}|tj�<|j|_�|_||_|jtjk�rtjj|j�g|_�|_ng|_�jd	�d
|_�y~tj|j�}tjj|��sLd}n"tj|j�j}tj|�j}||k}|�s�t |d��8}|j!�}	yt"j#|	�}
Wnt$k
�r�d}YnXWdQRX|�r�|jd
�r�|jd
j!�}n,|jtjk�r
t |j��}|j!�}WdQRXt%||j��r"|j&|�}t'||jd�}
tjj|�}yFtjj|��sVtj(|�t |d
��}t"j)|
�}	|j*|	�WdQRXWnt$k
�r�YnXt+|
|j,�Wn.t$k
�r�}
ztj�=�WYdd}
~
XnX|jd
�r�|jd
j-�|S)NzRunning load_module for {0}...Fcsg|]}�j|��qSr
)�
startswith)�.0r)rDr
r�
<listcomp>@sz(Py2Fixer.load_module.<locals>.<listcomp>csg|]}�j|��qSr
)rJ)rKr)rDr
rrLBsTzExcluded {0} from translationzAutoconverting {0} ...r;r�rb�exec�wb).r)r*r+�sys�modulesrBr@ZPY_COMPILEDZC_EXTENSIONZ	C_BUILTINZ	PY_FROZEN�anyr4r5r>r2Z
new_moduler-�__file__r�
__loader__rCrr�dirnamer?�__package__�
rpartition�cache_from_source�exists�stat�st_mtime�open�read�marshal�loadsrAr0rI�compile�makedirs�dumps�writerN�__dict__�close)r6rDrGZconvertZ	cachenameZupdate_cacheZ
sourcetimeZ	cachetime�f�data�coder,rUr/r
)rDrr>1s�







zPy2Fixer.load_module)N)rrrrZPY2FIXERr7r9r:r=rIr>r
r
r
rr1�s
r1cCs~t|t�r|f}t|t�r |f}t|�t|�dks<td��tj|�tj|�tjddk}|rzttj	krztj	j
dt�dS)NrzPass at least one argument�)�
isinstancer(�len�AssertionError�_hookr9r:rP�version_info�	meta_pathr)r5r4�enabler
r
r�
install_hooks�s



rqcCsttjkrtjjt�dS)N)rmrPro�remover
r
r
r�remove_hooks�s
rscCs
ttjkS)zG
    Returns True if the import hooks are installed, False if not.
    )rmrPror
r
r
r�detect_hooks�srtc@s eZdZdZdd�Zdd�ZdS)�hooksz�
    Acts as a context manager. Use like this:

    >>> from past import translation
    >>> with translation.hooks():
    ...     import mypy2module
    >>> import requests        # py2/3 compatible anyway
    >>> # etc.
    cCst�|_t�|S)N)rt�hooks_were_installedrq)r6r
r
r�	__enter__�szhooks.__enter__cGs|jst�dS)N)rvrs)r6�argsr
r
r�__exit__�szhooks.__exit__N)rrrrrwryr
r
r
rru�s	ruc@s eZdZdZdd�Zdd�ZdS)�
suspend_hooksax
    Acts as a context manager. Use like this:

    >>> from past import translation
    >>> translation.install_hooks()
    >>> import http.client
    >>> # ...
    >>> with translation.suspend_hooks():
    >>>     import requests     # or others that support Py2/3

    If the hooks were disabled before the context, they are not installed when
    the context is left.
    cCst�|_t�|S)N)rtrvrs)r6r
r
rrw�szsuspend_hooks.__enter__cGs|jrt�dS)N)rvrq)r6rxr
r
rry�szsuspend_hooks.__exit__N)rrrrrwryr
r
r
rrz�s
rz)&rr@Zloggingr^rrPr3Zlib2to3.pgen2.parserZlib2to3.refactorrZlibfuturizerZ	getLoggerrr)ZsetLevel�DEBUG�listZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2r	rrrr"r0�objectr1rmrqrsrtrurzZ
autotranslater
r
r
r�<module>!sz
54