B A[G#@sZdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z e eZeejee jee jee jee jZdddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(g#ZGd)d*d*Zd+d,Zd-d.Zd/d0ZGd1d2d2eZeZd>d4d5Z d6d7Z!d8d9Z"Gd:d;d;eZ#Gd>> from past 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)fixeszlib2to3.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. NcCs.dtjtjgkr*ttt_ttddit_dS)zj Call this before using the refactoring tools to create them on demand if needed. Nprint_functionT)r_rt_rtprmyfixesr r J/opt/alt/python37/lib/python3.7/site-packages/past/translation/__init__.pysetups z RTs.setupcCs.dtjtjgkr*ttt_ttddit_dS)zj Call this before using the refactoring tools to create them on demand if needed. NrT)r_rt_py2_detect_rtp_py2_detectrpy2_detect_fixersr r r r setup_detect_python2s zRTs.setup_detect_python2) __name__ __module__ __qualname____doc__rrr r staticmethodr rr r r r rvs rcCsrg}xhtj|}|d|kr2|d|dPq|d|krR|d|dPq|d}|d|dqW|S)zA Split a path into all components. From Python Cookbook. r)ospathsplitinsert)rZallpartspartsr r r splitalls   rcCsLg}t|}t|}x*t||D]\}}||kr2P||q Wtjj|S)z^ Returns the longest common substring to the two strings, starting from the left. )rzipappendrrjoin)s1s2ZchunksZpath1Zpath2Zdir1Zdir2r r r common_substringsr"c CsLtytj||}WnFtk r`}z(|jdks@|jdkrBtj||}Wdd}~XYnX|t|ddkrt d |t dd}| d||fWdQRXt d d$}| d |t|ddfWdQRXd St d |t dd}| d ||fWdQRXytdWntk rBYnXdSdS)zD Returns a bool indicating whether we think the code is Py2 z bad input=NzDetected Python 2 code: {0}z/tmp/original_code.pywz*### Original code (detected as py2): %s %sz/tmp/py2_detection_code.pyz1### Code after running py3 detection (from %s) %sTzDetected Python 3 code: {0}z*### Original code (detected as py3): %s %sz/tmp/futurize_code.pyF)rrr refactor_stringrmsgvaluerstrloggerdebugformatopenwriterremoveOSError)sourcepathnametreeefr r r detect_python2s2   " r6c@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)NZfutureZpast)foundZbase_exclude_pathscopy exclude_paths include_paths)selfr r r __init__s 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)r;)r<pathsr r r includeszPy2Fixer.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)r:)r<r>r r r exclude szPy2Fixer.excludeNc Cstd|d|krP|dd\}}|dkrL|||}||}|j}|}yt|||_Wn>t k r}z tdtd||dSd}~XYnX|jdd|_ |j tj krt j |jdd|_n|j tjkr|jd|_|S)NzRunning find_module: {0}....rzPy2Fixer could not find {0}zException was: {0})r$z __init__.py)r*r+r,rsplit find_module load_module__path__impr8 Exceptionkind PKG_DIRECTORYrrrr2Z PY_SOURCE)r<fullnamerparentZchildloadermodr4r r r rCs(      zPy2Fixer.find_modulec Cs~t|d7}ytj||j}WnHtk rl}z*|jdksJ|jdkrLtj||j}Wdd}~XYnXt |ddS)N z bad inputr#r$) rr rr&r2rr'r(rr))r<r1r3r4r r r transform,s"zPy2Fixer.transformc s tdtjkr(tj}n|jtjtjtj tj fkrHd}n@t fdd|j Drfd}n"t fdd|j Drd}nd}|stdtjf|j}nPtdt}|tj<|j|_|_||_|jtjkrtj|jg|_|_ng|_d d |_yt|j}tj|sLd}n"t|jj}t|j}||k}|st |d 8}|!} yt"#| } Wnt$k rd}YnXWdQRX|r|jd r|jd !} n,|jtjkr t |j}|!} WdQRXt%| |jrL|&| } t d d }|'d|j| fWdQRXt(| |jd} tj|} yFtj| st)| t |d}t"*| } |'| WdQRXWnt$k rYnXt+| |j,Wn.t$k r} ztj=Wdd} ~ XYnX|jd r|jd -|S)NzRunning load_module for {0}...Fcsg|]}|qSr ) startswith).0r)rJr r Msz(Py2Fixer.load_module..csg|]}|qSr )rP)rQr)rJr r rROsTzExcluded {0} from translationzAutoconverting {0} ...rArrbz/tmp/futurized_code.pyr%z### Futurized code (from %s) %sexecwb).r*r+r,sysmodulesrHrFZ PY_COMPILEDZ C_EXTENSIONZ C_BUILTINZ PY_FROZENanyr:r;rDr8Z new_moduler2__file__r __loader__rIrrdirnamerE __package__ rpartitioncache_from_sourceexistsstatst_mtimer-readmarshalloadsrGr6rOr.compilemakedirsdumpsrT__dict__close)r<rJrMconvertZ cachenameZ update_cacheZ sourcetimeZ cachetimer5datacoder1r[r4r )rJr rD>s                 zPy2Fixer.load_module)N) rrrrZPY2FIXERr=r?r@rCrOrDr r r r r7s r7r cCs~t|tr|f}t|tr |f}t|t|dks>> from past import translation >>> with translation.hooks(): ... import mypy2module >>> import requests # py2/3 compatible anyway >>> # etc. cCst|_t|S)N)rvhooks_were_installedrt)r<r r r __enter__szhooks.__enter__cGs|js tdS)N)rxru)r<argsr r r __exit__szhooks.__exit__N)rrrrryr{r r r r rws rwc@s eZdZdZddZddZdS) 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)rvrxru)r<r r r ryszsuspend_hooks.__enter__cGs|jr tdS)N)rxrt)r<rzr r r r{szsuspend_hooks.__exit__N)rrrrryr{r r r r r|s r|)r r )%rrFZloggingrcrrVr9Zlib2to3.pgen2.parserlib2to3.refactorrZ libfuturizerZ getLoggerrr*ZsetLevelDEBUGlistZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2r rrrr"r6objectr7rqrtrurvrwr|r r r r !sx      5"7