HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/past/translation/__pycache__/__init__.cpython-310.pyc
o

,�]�D�@sdZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZe�
e�Ze�ej�eej�eej�eej�eej�Zgd�ZGdd�d�Zd	d
�Zdd�Zd
d�ZGdd�de�Ze�Zddd�Z dd�Z!dd�Z"Gdd�de�Z#Gdd�de�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)�fixes)#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.
    NcC�2dtjtjfvrtt�t_ttddi�t_dSdS�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/dist-packages/past/translation/__init__.py�setup�s
�z	RTs.setupcCrr)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__r	r
rr�staticmethodrrrrrr
rvs

rcCspg}	tj�|�}|d|kr|�d|d�	|S|d|kr+|�d|d�	|S|d}|�d|d�q)zA
    Split a path into all components. From Python Cookbook.
    Tr�)�os�path�split�insert)r�allparts�partsrrr
�splitall�s���rcCsJg}t|�}t|�}t||�D]\}}||krn|�|�qtjj|�S)z^
    Returns the longest common substring to the two strings, starting from the
    left.
    )r�zip�appendrr�join)�s1�s2�chunks�path1�path2�dir1�dir2rrr
�common_substring�sr*c
Cs�t��z	tj�||�}Wn$ty1}z|jdks|jdkr �tj�||�}WYd}~nd}~ww|t|�dd�krFt	�
d�|��dSt	�
d�|��dS)	zD
    Returns a bool indicating whether we think the code is Py2
    �	bad input�=N���zDetected 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��r9c@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|_t�|j�|_g|_dS)N�future�past)�found�base_exclude_paths�copy�
exclude_paths�
include_paths��selfrrr
�__init__�s

zPy2Fixer.__init__cC�|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)rA�rC�pathsrrr
�include��zPy2Fixer.includecCrE)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)r@rFrrr
�exclude�rIzPy2Fixer.excludeNc
Cs�t�d�|��d|vr(|�dd�\}}|dur&|�||�}|�|�}|j}|}z	t�||�|_Wn!t	yR}zt�d�t�d�||��WYd}~dSd}~ww|jdd|_
|j
tjkrntj
�|jdd�|_|S|j
tjkrz|jd|_|S)NzRunning find_module: {0}...�.rzPy2Fixer could not find {0}zException was: {0})r-z__init__.py)r2r3r4�rsplit�find_module�load_module�__path__�impr=�	Exception�kind�
PKG_DIRECTORYrrr"r6�	PY_SOURCE)rC�fullnamer�parent�child�loader�modr8rrr
rMs.

���zPy2Fixer.find_modulec
Cs�t��|d7}z
tj�||j�}Wn%ty7}z|jdks$|jdkr%�tj�||j�}WYd}~nd}~wwt	|�dd�S)N�
r+r,r-)
rrr	r.r6rr/r0r
r1)rCr5r7r8rrr
�	transforms��zPy2Fixer.transformc
st�d�����tjvrtj�}�nl|jtjtjtj	tj
fvr$d}n t�fdd�|jD��r3d}nt�fdd�|j
D��rBd}nd}|sZt�d����tj�g|j�R�}�n&t�d����t���}|tj�<|j|_�|_||_|jtjkr�tj�|j�g|_�|_ng|_��d	�d
|_z�t�|j�}tj�|�s�d}nt�|j�j}t�|�j}||k}|s�t |d��}|�!�}	zt"�#|	�}
Wnt$y�d}YnwWd�n1s�wY|�rf|jd
r�|jd
�!�}n!|jtjk�rt |j��}|�!�}Wd�n	1�swYt%||j��r |�&|�}t'||jd�}
tj�|�}z.tj�|��s:t�(|�t |d
��}t"�)|
�}	|�*|	�Wd�n	1�sUwYWn
t$�yeYnwt+|
|j,�Wnt$�y}
ztj�=�d}
~
ww|jd
�r�|jd
�-�|S)NzRunning load_module for {0}...Fc�g|]}��|��qSr��
startswith��.0r�rUrr
�
<listcomp>@�z(Py2Fixer.load_module.<locals>.<listcomp>cr\rr]r_rarr
rbBrcTzExcluded {0} from translationzAutoconverting {0} ...rKr�rb�exec�wb).r2r3r4�sys�modulesrRrP�PY_COMPILED�C_EXTENSION�	C_BUILTIN�	PY_FROZEN�anyr@rArNr=�
new_moduler6�__file__r�
__loader__rSrr�dirnamerO�__package__�
rpartition�cache_from_source�exists�stat�st_mtime�open�read�marshal�loadsrQr9r[�compile�makedirs�dumps�writere�__dict__�close)rCrUrY�convert�	cachename�update_cache�
sourcetime�	cachetime�f�data�coder5rqr8rrar
rN1s�
�


���

�


�����zPy2Fixer.load_module�N)rrrr�PY2FIXERrDrHrJrMr[rNrrrr
r:�s
r:rcCs�t|t�r|f}t|t�r|f}t|�t|�dksJd��t�|�t�|�tjddk}|r?ttjvrAtj�	dt�dSdSdS)NrzPass at least one argument�)
�
isinstancer1�len�_hookrHrJrg�version_info�	meta_pathr)rAr@�enablerrr
�
install_hooks�s



�r�cCsttjvr
tj�t�dSdSr�)r�rgr��removerrrr
�remove_hooks�s
�r�cCs
ttjvS)zG
    Returns True if the import hooks are installed, False if not.
    )r�rgr�rrrr
�detect_hooks�s
r�c@� 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.
    cC�t�|_t�|Sr�)r��hooks_were_installedr�rBrrr
�	__enter__��zhooks.__enter__cGs|jst�dSdSr�)r�r��rC�argsrrr
�__exit__��
�zhooks.__exit__N�rrrrr�r�rrrr
r��s	r�c@r�)�
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.
    cCr�r�)r�r�r�rBrrr
r��r�zsuspend_hooks.__enter__cGs|jrt�dSdSr�)r�r�r�rrr
r��r�zsuspend_hooks.__exit__Nr�rrrr
r��s
r�)rr)&rrP�loggingrzrrgr?�lib2to3.pgen2.parser�lib2to3.refactorr�libfuturizer�	getLoggerrr2�setLevel�DEBUG�list�libfuturize_fix_names_stage1�lib2to3_fix_names_stage1�libfuturize_fix_names_stage2�lib2to3_fix_names_stage2rrrrr*r9�objectr:r�r�r�r�r�r��
autotranslaterrrr
�<module>sB!
���	;54