U
    —Ð~dîD  ã                #   @   s^  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ e e¡Ze ej¡ eejƒeejƒ eejƒ eejƒ Zdddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(g#ZG d)d*„ d*ƒZd+d,„ Zd-d.„ Zd/d0„ ZG d1d2„ d2eƒZeƒ Zd>d4d5„Z d6d7„ Z!d8d9„ Z"G d:d;„ d;eƒZ#G d<d=„ d=eƒZ$e Z%dS )?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)Úfixeszlib2to3.fixes.fix_applyzlib2to3.fixes.fix_exceptzlib2to3.fixes.fix_execfilezlib2to3.fixes.fix_exitfunczlib2to3.fixes.fix_funcattrszlib2to3.fixes.fix_filterzlib2to3.fixes.fix_has_keyzlib2to3.fixes.fix_idiomszlib2to3.fixes.fix_importzlib2to3.fixes.fix_internzlib2to3.fixes.fix_isinstancezlib2to3.fixes.fix_methodattrszlib2to3.fixes.fix_nezlib2to3.fixes.fix_numliteralszlib2to3.fixes.fix_parenzlib2to3.fixes.fix_printzlib2to3.fixes.fix_raisezlib2to3.fixes.fix_renameszlib2to3.fixes.fix_reducezlib2to3.fixes.fix_reprzlib2to3.fixes.fix_standarderrorzlib2to3.fixes.fix_sys_exczlib2to3.fixes.fix_throwzlib2to3.fixes.fix_tuple_paramszlib2to3.fixes.fix_typeszlib2to3.fixes.fix_ws_commazlib2to3.fixes.fix_xreadlineszlib2to3.fixes.fix_basestringzlib2to3.fixes.fix_execzlib2to3.fixes.fix_getcwduzlib2to3.fixes.fix_longzlib2to3.fixes.fix_nextzlib2to3.fixes.fix_nonzerozlib2to3.fixes.fix_raw_inputzlib2to3.fixes.fix_xrangec                   @   s8   e Zd ZdZdZdZdZdZedd„ ƒZ	edd„ ƒZ
dS )Ú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.
    Nc                   C   s.   dt jt jfkr*ttƒt _ttddiƒt _dS ©zj
        Call this before using the refactoring tools to create them on demand
        if needed.
        NÚprint_functionT)r   Ú_rtÚ_rtpr   Úmyfixes© r   r   úV/var/www/html/myproject/myenv/lib/python3.8/site-packages/past/translation/__init__.pyÚsetupƒ   s    
z	RTs.setupc                   C   s.   dt jt jfkr*ttƒt _ttddiƒt _dS r   )r   Ú_rt_py2_detectÚ_rtp_py2_detectr   Úpy2_detect_fixersr   r   r   r   Úsetup_detect_python2Ž   s
    
ÿzRTs.setup_detect_python2)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r   r   Ústaticmethodr   r   r   r   r   r   r   v   s   

r   c                 C   sn   g }t j | ¡}|d | kr0| d|d ¡ qjq|d | krP| d|d ¡ qjq|d } | d|d ¡ q|S )zA
    Split a path into all components. From Python Cookbook.
    r   é   )ÚosÚpathÚsplitÚinsert)r   ZallpartsÚpartsr   r   r   Úsplitall«   s    r   c                 C   sJ   g }t | ƒ}t |ƒ}t||ƒD ]\}}||kr2 q>| |¡ qtjj|Ž S )z^
    Returns the longest common substring to the two strings, starting from the
    left.
    )r   ÚzipÚappendr   r   Újoin)Ús1Ús2ÚchunksZpath1Zpath2Zdir1Zdir2r   r   r   Úcommon_substring¾   s    r$   c              
   C   s¢   t  ¡  zt j | |¡}W nF tk
r` } z(|jdks@|jdkrB‚ t j | |¡}W 5 d}~X Y nX | t|ƒdd… krŠt	 
d |¡¡ dS t	 
d |¡¡ dS dS )	zD
    Returns a bool indicating whether we think the code is Py2
    ú	bad inputú=NéÿÿÿÿzDetected Python 2 code: {0}TzDetected Python 3 code: {0}F)r   r   r   Úrefactor_stringr   ÚmsgÚvaluer	   ÚstrÚloggerÚdebugÚformat)ÚsourceÚpathnameÚtreeÚer   r   r   Údetect_python2Ï   s     r3   c                   @   sF   e Zd ZdZdZdd„ Zdd„ Zdd„ Zdd
d„Zdd„ Z	dd„ Z
d	S )ÚPy2Fixerzi
    An import hook class that uses lib2to3 for source-to-source translation of
    Py2 code to Py3.
    Tc                 C   s(   d | _ ddg| _t | j¡| _g | _d S )NÚfutureZpast)ÚfoundZbase_exclude_pathsÚcopyÚexclude_pathsÚinclude_paths©Úselfr   r   r   Ú__init__ï   s    
zPy2Fixer.__init__c                 C   s   |  j |7  _ dS )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)r9   ©r;   Úpathsr   r   r   Úincludeõ   s    zPy2Fixer.includec                 C   s   |  j |7  _ dS )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)r8   r=   r   r   r   Úexcludeý   s    zPy2Fixer.excludeNc              
   C   sø   t  d |¡¡ d|krP| dd¡\}}|d krL|  ||¡}| |¡}|j}|}zt ||¡| _W nD t	k
r¦ } z&t  d¡ t  d ||¡¡ W Y ¢d S d }~X Y nX | jd d | _
| j
tjkrÜtj | jd d¡| _n| j
tjkrô| jd | _| S )NzRunning find_module: {0}...Ú.r   zPy2Fixer could not find {0}zException was: {0})r'   z__init__.py)r,   r-   r.   ÚrsplitÚfind_moduleÚload_moduleÚ__path__Úimpr6   Ú	ExceptionÚkindÚPKG_DIRECTORYr   r   r    r0   Ú	PY_SOURCE)r;   Úfullnamer   ÚparentÚchildÚloaderÚmodr2   r   r   r   rC     s(    

zPy2Fixer.find_modulec              
   C   s~   t  ¡  |d7 }zt j || j¡}W nH tk
rl } z*|jdksJ|jdkrL‚ t j || j¡}W 5 d }~X Y nX t	|ƒd d… S )NÚ
r%   r&   r'   )
r   r   r   r(   r0   r   r)   r*   r	   r+   )r;   r/   r1   r2   r   r   r   Ú	transform  s    "zPy2Fixer.transformc              
      sö  t  d ˆ ¡¡ ˆ tjkr(tjˆ  }n°| jtjtjtj	tj
fkrHd}n@t‡ fdd„| jD ƒƒrfd}n"t‡ fdd„| jD ƒƒr„d}nd}|s²t  d ˆ ¡¡ tjˆ f| jžŽ }n&t  d ˆ ¡¡ t ˆ ¡}|tjˆ < | j|_ˆ |_| |_| jtjkrtj | j¡g|_ˆ |_ng |_ˆ  d	¡d
 |_z~t | j¡}tj |¡sLd}n"t | j¡j}t |¡j}||k }|s¼t |dƒ8}| !¡ }	zt" #|	¡}
W n t$k
r°   d}Y nX W 5 Q R X |rš| jd
 rÞ| jd
  !¡ }n,| jtjkr
t | jƒ}| !¡ }W 5 Q R X t%|| jƒr"|  &|¡}t'|| jdƒ}
tj |¡}zFtj |¡sVt (|¡ t |dƒ}t" )|
¡}	| *|	¡ W 5 Q R X W n t$k
r˜   Y nX t+|
|j,ƒ W n. t$k
rÖ } ztjˆ = ‚ W 5 d }~X Y nX | jd
 rò| jd
  -¡  |S )NzRunning load_module for {0}...Fc                    s   g | ]}ˆ   |¡‘qS r   ©Ú
startswith©Ú.0r   ©rK   r   r   Ú
<listcomp>@  s     z(Py2Fixer.load_module.<locals>.<listcomp>c                    s   g | ]}ˆ   |¡‘qS r   rR   rT   rV   r   r   rW   B  s     TzExcluded {0} from translationzAutoconverting {0} ...rA   r   ÚrbÚexecÚwb).r,   r-   r.   ÚsysÚmodulesrH   rF   ÚPY_COMPILEDÚC_EXTENSIONZ	C_BUILTINZ	PY_FROZENÚanyr8   r9   rD   r6   Z
new_moduler0   Ú__file__r   Ú
__loader__rI   r   r   ÚdirnamerE   Ú__package__Ú
rpartitionÚcache_from_sourceÚexistsÚstatÚst_mtimeÚopenÚreadÚmarshalÚloadsrG   r3   rQ   ÚcompileÚmakedirsÚdumpsÚwriterY   Ú__dict__Úclose)r;   rK   rO   ÚconvertZ	cachenameZupdate_cacheZ
sourcetimeZ	cachetimeÚfÚdataÚcoder/   rb   r2   r   rV   r   rD   1  s‚    
ÿ




zPy2Fixer.load_module)N)r   r   r   r   ZPY2FIXERr<   r?   r@   rC   rQ   rD   r   r   r   r   r4   ä   s   
r4   r   c                 C   s~   t | tƒr| f} t |tƒr |f}t| ƒt|ƒ dks<tdƒ‚t | ¡ t |¡ tjd dk}|rzttj	krztj	 
dt¡ d S )Nr   zPass at least one argumenté   )Ú
isinstancer+   ÚlenÚAssertionErrorÚ_hookr?   r@   r[   Úversion_infoÚ	meta_pathr   )r9   r8   Úenabler   r   r   Úinstall_hooksš  s    



r   c                   C   s   t tjkrtj t ¡ d S ©N)r{   r[   r}   Úremover   r   r   r   Úremove_hooks«  s    
r‚   c                   C   s
   t tjkS )zG
    Returns True if the import hooks are installed, False if not.
    )r{   r[   r}   r   r   r   r   Údetect_hooks°  s    rƒ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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.
    c                 C   s   t ƒ | _tƒ  | S r€   )rƒ   Úhooks_were_installedr   r:   r   r   r   Ú	__enter__Ã  s    zhooks.__enter__c                 G   s   | j stƒ  d S r€   )r…   r‚   ©r;   Úargsr   r   r   Ú__exit__È  s    zhooks.__exit__N©r   r   r   r   r†   r‰   r   r   r   r   r„   ¹  s   	r„   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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.
    c                 C   s   t ƒ | _tƒ  | S r€   )rƒ   r…   r‚   r:   r   r   r   r†   Û  s    zsuspend_hooks.__enter__c                 G   s   | j rtƒ  d S r€   )r…   r   r‡   r   r   r   r‰   ß  s    zsuspend_hooks.__exit__NrŠ   r   r   r   r   r‹   Í  s   r‹   )r   r   )&r   rF   Úloggingrk   r   r[   r7   Zlib2to3.pgen2.parser   Úlib2to3.refactorr   Zlibfuturizer   Ú	getLoggerr   r,   ÚsetLevelÚDEBUGÚlistZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2r
   r   r   r   r$   r3   Úobjectr4   r{   r   r‚   rƒ   r„   r‹   Zautotranslater   r   r   r   Ú<module>   s†   !
ÿþýÊ;5 4
	