o ,]D@sdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z e eZeejee jee jee jee jZgdZGdddZd d Zd d Zd dZGdddeZeZdddZ ddZ!ddZ"GdddeZ#GdddeZ$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@s8eZdZdZdZdZdZdZeddZ eddZ 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. NcC2dtjtjfvrttt_ttddit_dSdSzj Call this before using the refactoring tools to create them on demand if needed. Nprint_functionT)r_rt_rtprmyfixesr r ;/usr/lib/python3/dist-packages/past/translation/__init__.pysetups z RTs.setupcCrr)r_rt_py2_detect_rtp_py2_detectrpy2_detect_fixersr r r r setup_detect_python2s   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|dq)zA Split a path into all components. From Python Cookbook. Tr)ospathsplitinsert)rallpartspartsr r r splitalls   rcCsJg}t|}t|}t||D]\}}||krn||qtjj|S)z^ Returns the longest common substring to the two strings, starting from the left. )rzipappendrrjoin)s1s2chunkspath1path2dir1dir2r r r common_substrings  r*c Cstz tj||}Wn$ty1}z|jdks|jdkr tj||}WYd}~nd}~ww|t|ddkrFt d |dSt d |dS) zD Returns a bool indicating whether we think the code is Py2 bad input=NzDetected Python 2 code: {0}TzDetected Python 3 code: {0}F) rrrrefactor_stringrmsgvaluer strloggerdebugformat)sourcepathnametreeer r r detect_python2sr9c@sFeZdZdZdZddZddZddZdd d Zd d Z ddZ 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)Nfuturepast)foundbase_exclude_pathscopy exclude_paths include_pathsselfr 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)rArCpathsr r r includezPy2Fixer.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 excluderIzPy2Fixer.excludeNc Cstd|d|vr(|dd\}}|dur&|||}||}|j}|}z t|||_Wn!t yR}ztdtd||WYd}~dSd}~ww|jdd|_ |j tj krnt j |jdd|_|S|j tjkrz|jd|_|S)NzRunning find_module: {0}....rzPy2Fixer could not find {0}zException was: {0})r-z __init__.py)r2r3r4rsplit find_module load_module__path__impr= Exceptionkind PKG_DIRECTORYrrr"r6 PY_SOURCE)rCfullnamerparentchildloadermodr8r r r rMs.     zPy2Fixer.find_modulec Cst|d7}z tj||j}Wn%ty7}z|jdks$|jdkr%tj||j}WYd}~nd}~wwt |ddS)N r+r,r-) rrr r.r6rr/r0r r1)rCr5r7r8r r r transformszPy2Fixer.transformc stdtjvrtj}nl|jtjtjtj tj fvr$d}n t fdd|j Dr3d}nt fdd|j DrBd}nd}|sZtdtjg|jR}n&tdt}|tj<|j|_|_||_|jtjkrtj|jg|_|_n g|_d d |_zt|j}tj|sd}nt|jj}t|j}||k}|st |d }|!} zt"#| } Wn t$yd}YnwWdn1swY|rf|jd r|jd !} n!|jtjkrt |j }|!} Wdn 1swYt%| |jr |&| } t'| |jd } tj|} z.tj| s:t(| t |d }t")| } |*| Wdn 1sUwYWn t$yeYnwt+| |j,Wnt$y} ztj=d} ~ ww|jd r|jd -|S)NzRunning load_module for {0}...Fcg|]}|qSr  startswith.0rrUr r @z(Py2Fixer.load_module..cr\r r]r_rar r rbBrcTzExcluded {0} from translationzAutoconverting {0} ...rKrrbexecwb).r2r3r4sysmodulesrRrP PY_COMPILED C_EXTENSION C_BUILTIN PY_FROZENanyr@rArNr= new_moduler6__file__r __loader__rSrrdirnamerO __package__ rpartitioncache_from_sourceexistsstatst_mtimeopenreadmarshalloadsrQr9r[compilemakedirsdumpswritere__dict__close)rCrUrYconvert cachename update_cache sourcetime cachetimefdatacoder5rqr8r rar rN1s                   zPy2Fixer.load_moduleN) rrrrPY2FIXERrDrHrJrMr[rNr r r r r:s  r:r cCst|tr|f}t|tr|f}t|t|dksJdt|t|tjddk}|r?ttjvrAtj dtdSdSdS)NrzPass at least one argument) isinstancer1len_hookrHrJrg version_info meta_pathr)rAr@enabler r r install_hookss    rcCsttjvr tjtdSdSr)rrgrremover r r r remove_hookss rcCs ttjvS)zG Returns True if the import hooks are installed, False if not. )rrgrr r r r detect_hookss rc@ eZdZdZddZddZdS)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. cCt|_t|Sr)rhooks_were_installedrrBr r r __enter__zhooks.__enter__cGs|jstdSdSr)rrrCargsr r r __exit__ zhooks.__exit__Nrrrrrrr r r r rs rc@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. cCrr)rrrrBr r r rrzsuspend_hooks.__enter__cGs|jrtdSdSr)rrrr r r rrzsuspend_hooks.__exit__Nrr r r r rs r)r r )&rrPloggingrzrrgr?lib2to3.pgen2.parserlib2to3.refactorr libfuturizer getLoggerrr2setLevelDEBUGlistlibfuturize_fix_names_stage1lib2to3_fix_names_stage1libfuturize_fix_names_stage2lib2to3_fix_names_stage2r rrrr*r9objectr:rrrrrr autotranslater r r r sB!      ;54