
    FwgB:                     z   d Z ddlZej                  dk\  rddlZn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 	 ddlmZmZ ej                  dd d	k  rddlZ ej&                  e      Zej-                  ej.                          eej2                         eej4                        z    eej6                        z    eej8                        z   Zg d
Z G d d      Zd Z d Z!d Z"d Z# G d de      Z$ G d de      Z% e%       Z&ddZ'd Z(d Z) G d de      Z* G d de      Z+e'Z,y# e$ r dZeZY w xY w)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)
PathFinderSourceFileLoader   )r      )#z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                   @    e Zd ZdZdZdZdZdZed        Z	ed        Z
y)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                      dt         j                  t         j                  fv r4t        t              t         _        t        t        ddi      t         _        yyzj
        Call this before using the refactoring tools to create them on demand
        if needed.
        Nprint_functionT)r   _rt_rtpr   myfixes     P/var/www/horilla/myenv/lib/python3.12/site-packages/past/translation/__init__.pysetupz	RTs.setup   s>     CGGSXX&&%g.CG&w1A40HICH 'r   c                      dt         j                  t         j                  fv r4t        t              t         _        t        t        ddi      t         _        yyr   )r   _rt_py2_detect_rtp_py2_detectr   py2_detect_fixersr   r   r   setup_detect_python2zRTs.setup_detect_python2   sI     C&&(;(;<<!01B!CC"12C3CT2J#LC =r   )__name__
__module____qualname____doc__r   r   r   r   staticmethodr   r   r   r   r   r   r      sG     CDNOJ J L Lr   r   c                     g }	 t         j                  j                  |       }|d   | k(  r|j                  d|d          	 |S |d   | k(  r|j                  d|d          	 |S |d   } |j                  d|d          z)zA
    Split a path into all components. From Python Cookbook.
    r      )ospathsplitinsert)r%   allpartspartss      r   splitallr*      s     H
d#8tOOAuQx( O 1XOOAuQx( O 8DOOAuQx( r   c                     g }t        |       }t        |      }t        ||      D ]  \  }}||k7  r n|j                  |        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chunkspath1path2dir1dir2s          r   common_substringr6      s`    
 FRLERLEE5) t4<d 77<<  r   c                    t         j                          	 t         j                  j                  | |      }| t        |      dd k7  r%t        j                  dj                  |             yt        j                  dj                  |             y# t        $ rI}|j
                  dk7  s|j                  dk7  r t         j                  j                  | |      }Y d}~d}~ww xY w)	zD
    Returns a bool indicating whether we think the code is Py2
    	bad input=Nz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es       r   detect_python2rG      s     :!!11&(C T3B299(CD299(CD  :55K177c>xx''9:s    B 	C#?CC#c                 <   t         j                          | dz  } 	 t         j                  j                  | |      }t        |      d d S # t        $ rI}|j
                  dk7  s|j                  dk7  r t         j                  j                  | |      }Y d }~[d }~ww xY w)N
r8   r9   r:   )	r   r   r   r;   r   r<   r=   r   r>   rB   s       r   	transformrJ      s     IIK
dNF:ww&&vx8 t9Sb>  :55K177c>xx''9:s    A	 		B?BBc                   <     e Zd Zg Zg Zd Zd Z fdZ fdZ xZ	S )PastSourceFileLoaderc                     | j                   t        fd| j                  D              rd}|S t        fd| j                  D              rd}|S d}|S )Nc              3   @   K   | ]  }j                  |        y wN
startswith.0r%   fullnames     r   	<genexpr>z7PastSourceFileLoader._convert_needed.<locals>.<genexpr>  s     HTx""4(H   Fc              3   @   K   | ]  }j                  |        y wrO   rP   rR   s     r   rU   z7PastSourceFileLoader._convert_needed.<locals>.<genexpr>  s     Jt$$T*JrV   T)nameanyexclude_pathsinclude_paths)selfconvertrT   s     @r   _convert_neededz$PastSourceFileLoader._convert_needed	  sZ    99HT5G5GHHG
 	 Jt7I7IJJG  Gr   c                     | j                  | j                        }| j                  }t        ||      rt	        ||      }t        ||d      }t        ||j                         y )Nexec)
get_sourcerX   r%   rG   rJ   compiler`   __dict__)r\   modulerC   rD   codes        r   _exec_transformed_modulez-PastSourceFileLoader._exec_transformed_module  sO    +99&(+vx0Fvx0T6??#r   c                    t         j                  d|       |t        j                  v rt        j                  |   }|S | j	                         rt         j                  d|       t        j                  |      }|t        j                  |<   | j                  |_        | |_	        | j                  |      rg |_        ||_        n|j                  d      d   |_        | j                  |       |S t        | A  |      }|S )NzRunning load_module for %sAutoconverting %s.r   )r?   r@   sysmodulesr^   imp
new_moduler%   __file__
__loader__
is_package__path____package__
rpartitionrf   superload_module)r\   rT   mod	__class__s      r   ru   z PastSourceFileLoader.load_module  s    18<s{{"++h'C$ 
! ##%0(;nnX.(+H%  $yy!%??8,#%CL&.CO&.&9&9#&>q&ACO--c2 
 g)(3
r   c                     t         j                  d|       | j                         r2t         j                  d| j                         | j	                  |       y t
        |   |       y )NzRunning exec_module for %srh   )r?   r@   r^   rX   rf   rt   exec_module)r\   rd   rw   s     r   ry   z PastSourceFileLoader.exec_module4  sL    16:!LL,dii8))&1G'r   )
r   r   r   rZ   r[   r^   rf   ru   ry   __classcell__)rw   s   @r   rL   rL     s&    MM$0( (r   rL   c                   6    e Zd ZdZdZd Zd Zd Zd	dZd
dZ	y)Py2Fixerzi
    An import hook class that uses lib2to3 for source-to-source translation of
    Py2 code to Py3.
    Tc                 z    d | _         ddg| _        t        j                  | j                        | _        g | _        y )Nfuturepast)foundbase_exclude_pathscopyrZ   r[   r\   s    r   __init__zPy2Fixer.__init__H  s5    
#+V"4!YYt'>'>?r   c                 .    | xj                   |z  c_         y)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)r[   r\   pathss     r   includezPy2Fixer.includeN       	e#r   c                 .    | xj                   |z  c_         y)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)rZ   r   s     r   excludezPy2Fixer.excludeV  r   r   Nc                     t         j                  d||       t        j                  ||      }|st         j                  d|       y t        |_        | j                  |_        | j                  |_        |S )NzRunning find_module: (%s, %s)Py2Fixer could not find %s)r?   r@   r   find_modulerL   rw   rZ   r[   )r\   rT   r%   loaders       r   r   zPy2Fixer.find_module_  sc    4hE''$7LL5x@/#11#11r   c                 ,   t         j                  d|||       t        j                  |||      }|st         j                  d|       y t        |j
                  _        | j                  |j
                  _        | j                  |j
                  _        |S )NzRunning find_spec: (%s, %s, %s)r   )	r?   r@   r   	find_specrL   r   rw   rZ   r[   )r\   rT   r%   targetspecs        r   r   zPy2Fixer.find_speck  ss    6$O##HdF;LL5x@ 4$($6$6!$($6$6!r   rO   )NN)
r   r   r   r    PY2FIXERr   r   r   r   r   r   r   r   r|   r|   =  s'     H $$		r   r|   c                    t        | t              r| f} t        |t              r|f}t        |       t        |      z   dkD  sJ d       t        j	                  |        t        j                  |       t        j                  d   dk\  }|r<t        t        j                  vr%t        j                  j                  dt               y y y )Nr   zPass at least one argumentr   )

isinstancer>   len_hookr   r   rj   version_info	meta_pathr'   )r[   rZ   enables      r   install_hooksr   z  s    -%&(-%&(}M 22Q6T8TT6	MM- 	MM- a A%F%s}},Q& -vr   c                  x    t         t        j                  v r$t        j                  j                  t                y y rO   )r   rj   r   remover   r   r   remove_hooksr     s%    U# r   c                  .    t         t        j                  v S )zG
    Returns True if the import hooks are installed, False if not.
    )r   rj   r   r   r   r   detect_hooksr     s     CMM!!r   c                       e Zd ZdZd Zd Zy)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                 8    t               | _        t                | S rO   )r   hooks_were_installedr   r   s    r   	__enter__zhooks.__enter__  s    $0N!r   c                 2    | j                   st                y y rO   )r   r   r\   argss     r   __exit__zhooks.__exit__  s    ((N )r   Nr   r   r   r    r   r   r   r   r   r   r     s    
r   r   c                       e Zd ZdZd Zd Zy)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                 8    t               | _        t                | S rO   )r   r   r   r   s    r   r   zsuspend_hooks.__enter__  s    $0N!r   c                 2    | j                   rt                y y rO   )r   r   r   s     r   r   zsuspend_hooks.__exit__  s    $$O %r   Nr   r   r   r   r   r     s    r   r   )r   r   )-r    rj   r   	importlibrl   loggingr$   r   lib2to3.pgen2.parser   lib2to3.refactorr   libfuturizer   importlib.machineryr   r	   ImportErrorobject	getLoggerr   r?   setLevelDEBUGlistlibfuturize_fix_names_stage1lib2to3_fix_names_stage1libfuturize_fix_names_stage2lib2to3_fix_names_stage2r   r   r   r*   r6   rG   rJ   rL   r|   r   r   r   r   r   r   autotranslater   r   r   <module>r      s`  B v  	  * ,  BQ& 			8	$  223../02234 ../08 v!L !Lj&!"*&5(+ 5(p7v 7t 	
'"$
"F (F 0 _  Js   D. .	D:9D: