HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/past/translation/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/past/translation/__pycache__/__init__.cpython-36.pyc
3

+��hB:�+@s�dZddlZejdFkr ddlZnddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
yddlmZmZWnek
r�dZeZYnXejdd	�dGkr�ddlZeje�Zejej�ee
j�ee
j�ee
j�ee
j�Zddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-g#ZGd.d/�d/�Zd0d1�Z d2d3�Z!d4d5�Z"d6d7�Z#Gd8d9�d9e�Z$Gd:d;�d;e�Z%e%�Z&fffd<d=�Z'd>d?�Z(d@dA�Z)GdBdC�dCe�Z*GdDdE�dEe�Z+e'Z,dS)Ha'
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)�fixes)�
PathFinder�SourceFileLoader��zlib2to3.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�rr�@/root/tmp/pip-build-yeh9mt8d/future/past/translation/__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.
        NrT)r�_rt_py2_detect�_rtp_py2_detectr�py2_detect_fixersrrrr�setup_detect_python2�s
zRTs.setup_detect_python2)�__name__�
__module__�__qualname__�__doc__r
rrr�staticmethodrrrrrrr�s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�partsrrr�splitall�sr"cCsLg}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�s2�chunks�path1�path2Zdir1Zdir2rrr�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�errr�detect_python2�s r9cCsztj�|d7}ytjj||�}WnFtk
rh}z*|jdksH|jdkrJ�tjj||�}WYdd}~XnXt|�dd�S)N�
z	bad inputr,rr-)	rrr
r.rr/r0rr1)r5r6r7r8rrr�	transform�s r;cs@eZdZgZgZdd�Zdd�Z�fdd�Z�fdd�Z�Z	S)	�PastSourceFileLoadercsJ|j�t�fdd�|jD��r$d}n"t�fdd�|jD��rBd}nd}|S)Nc3s|]}�j|�VqdS)N)�
startswith)�.0r)�fullnamerr�	<genexpr>sz7PastSourceFileLoader._convert_needed.<locals>.<genexpr>Fc3s|]}�j|�VqdS)N)r=)r>r)r?rrr@
sT)�name�any�
exclude_paths�
include_paths)�self�convertr)r?r�_convert_needed	sz$PastSourceFileLoader._convert_neededcCsB|j|j�}|j}t||�r&t||�}t||d�}t||j�dS)N�exec)�
get_sourcerArr9r;�compilerH�__dict__)rE�moduler5r6�coderrr�_exec_transformed_modules

z-PastSourceFileLoader._exec_transformed_modulecs�tjd|�|tjkr"tj|}nv|j�r�tjd|�tj|�}|tj|<|j|_||_	|j
|�rpg|_||_n|j
d�d|_|j|�nt�j|�}|S)NzRunning load_module for %szAutoconverting %s�.r)r2r3�sys�modulesrG�imp�
new_moduler�__file__�
__loader__�
is_package�__path__�__package__�
rpartitionrN�super�load_module)rEr?�mod)�	__class__rrr[s 



z PastSourceFileLoader.load_modulecs>tjd|�|j�r.tjd|j�|j|�nt�j|�dS)NzRunning exec_module for %szAutoconverting %s)r2r3rGrArNrZ�exec_module)rErL)r]rrr^4s
z PastSourceFileLoader.exec_module)
rrrrCrDrGrNr[r^�
__classcell__rr)r]rr<s
	r<c@s@eZdZdZdZdd�Zdd�Zdd�Zdd
d�Zddd
�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)N�futureZpast)�foundZbase_exclude_paths�copyrCrD)rErrr�__init__Hs
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)rD)rE�pathsrrr�includeNszPy2Fixer.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)rC)rErerrr�excludeVszPy2Fixer.excludeNcCsHtjd||�tj||�}|s.tjd|�dSt|_|j|_|j|_|S)NzRunning find_module: (%s, %s)zPy2Fixer could not find %s)r2r3r�find_moduler<r]rCrD)rEr?r�loaderrrrrh_szPy2Fixer.find_modulecCsRtjd|||�tj|||�}|s2tjd|�dSt|j_|j|j_|j|j_|S)NzRunning find_spec: (%s, %s, %s)zPy2Fixer could not find %s)	r2r3r�	find_specr<rir]rCrD)rEr?r�target�specrrrrjks

zPy2Fixer.find_spec)N)NN)
rrrrZPY2FIXERrdrfrgrhrjrrrrr`=s	
r`cCs~t|t�r|f}t|t�r |f}t|�t|�dks<td��tj|�tj|�tjddk}|rzttj	krztj	j
dt�dS)NrzPass at least one argumentr)�
isinstancer1�len�AssertionError�_hookrfrgrP�version_info�	meta_pathr )rDrC�enablerrr�
install_hookszs



rtcCsttjkrtjjt�dS)N)rprPrr�removerrrr�remove_hooks�s
rvcCs
ttjkS)zG
    Returns True if the import hooks are installed, False if not.
    )rprPrrrrrr�detect_hooks�srwc@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)rw�hooks_were_installedrt)rErrr�	__enter__�szhooks.__enter__cGs|jst�dS)N)ryrv)rE�argsrrr�__exit__�szhooks.__exit__N)rrrrrzr|rrrrrx�s	rxc@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)rwryrv)rErrrrz�szsuspend_hooks.__enter__cGs|jrt�dS)N)ryrt)rEr{rrrr|�szsuspend_hooks.__exit__N)rrrrrzr|rrrrr}�s
r})rr)rr
)-rrPrq�	importlibrR�loggingrrcZlib2to3.pgen2.parser�lib2to3.refactorrZlibfuturizer�importlib.machineryrr�ImportError�object�	getLoggerrr2�setLevel�DEBUG�listZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2rrrr"r+r9r;r<r`rprtrvrwrxr}Z
autotranslaterrrr�<module>!s�



58: