a 97aîDã@sdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z e  e¡Ze ej¡ee jƒee jƒee jƒee jƒZgd¢ZGdd„dƒZd d „Zd d „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. NcCs.dtjtjfvr*ttƒt_ttddiƒt_dS©zj Call this before using the refactoring tools to create them on demand if needed. NÚprint_functionT)rÚ_rtÚ_rtprÚmyfixes©r r út/private/var/folders/s6/9n5zrl012gv99k63s4q6ccsd4s6mqz/T/pip-target-f5cq3f2q/lib/python/past/translation/__init__.pyÚsetupƒs z RTs.setupcCs.dtjtjfvr*ttƒt_ttddiƒt_dSr)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__rr rrÚ staticmethodr rr r r r rvs rcCsng}tj |¡}|d|kr0| d|d¡qjq|d|krP| d|d¡qjq|d}| d|d¡q|S)zA Split a path into all components. From Python Cookbook. ré)ÚosÚpathÚsplitÚinsert)rZallpartsÚpartsr r r Úsplitall«s   rcCsJg}t|ƒ}t|ƒ}t||ƒD]\}}||kr2q>| |¡qtjj|ŽS)z^ Returns the longest common substring to the two strings, starting from the left. )rÚzipÚappendrrÚjoin)Ús1Ús2ÚchunksÚpath1Úpath2Zdir1Zdir2r r r Úcommon_substring¾s r&c Cs¤t ¡ztj ||¡}WnHtyb}z0|jdks>|jdkr@‚tj ||¡}WYd}~n d}~00|t|ƒdd…krŒt   d  |¡¡dSt   d  |¡¡dSdS) 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$r5c@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ÚfutureZpast)ÚfoundZbase_exclude_pathsÚcopyÚ 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 ÚincludeõszPy2Fixer.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 ÚexcludeýszPy2Fixer.excludeNc Csöt d |¡¡d|vrP| dd¡\}}|durL| ||¡}| |¡}|j}|}zt ||¡|_WnBt y¤}z*t d¡t d ||¡¡WYd}~dSd}~00|jdd|_ |j tj krÚt 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/r0ÚrsplitÚ find_moduleÚ load_moduleÚ__path__Úimpr8Ú ExceptionÚkindÚ PKG_DIRECTORYrrr r2Ú PY_SOURCE)r=ÚfullnamerÚparentÚchildÚloaderÚmodr4r r r rEs(      zPy2Fixer.find_modulec Cs€t ¡|d7}ztj ||j¡}WnJtyn}z2|jdksH|jdkrJ‚tj ||j¡}WYd}~n d}~00t |ƒdd…S)NÚ r'r(r)) rr rr*r2rr+r,r r-)r=r1r3r4r r r Ú transforms&zPy2Fixer.transformc s8t d ˆ¡¡ˆtjvr(tjˆ}nò|jtjtjtj tj fvrHd}n@t ‡fdd„|j Dƒƒrfd}n"t ‡fdd„|j Dƒƒr„d}nd}|s´t d ˆ¡¡tjˆg|j¢RŽ}nft d ˆ¡¡t ˆ¡}|tjˆ<|j|_ˆ|_||_|jtjkrtj |j¡g|_ˆ|_ng|_ˆ d ¡d |_z¼t |j¡}tj |¡sNd}n"t |j¡j}t |¡j}||k}|sÒt |d ƒ@}| !¡} zt" #| ¡} Wnt$y°d}Yn0Wdƒn1sÈ0Y|rÚ|jd rô|jd  !¡} nB|jtjkr6t |jƒ}| !¡} Wdƒn1s,0Yt%| |jƒrN| &| ¡} t'| |jd ƒ} tj |¡} z\tj | ¡s‚t (| ¡t |d ƒ$}t" )| ¡} | *| ¡Wdƒn1s¸0YWnt$yØYn0t+| |j,ƒWn0t$y} ztjˆ=‚WYd} ~ n d} ~ 00|jd r4|jd  -¡|S)NzRunning load_module for {0}...Fcsg|]}ˆ |¡‘qSr ©Ú startswith©Ú.0r©rMr r Ú @óz(Py2Fixer.load_module..csg|]}ˆ |¡‘qSr rTrVrXr r rYBrZTzExcluded {0} from translationzAutoconverting {0} ...rCrÚrbÚexecÚwb).r.r/r0ÚsysÚmodulesrJrHÚ PY_COMPILEDÚ C_EXTENSIONZ C_BUILTINZ PY_FROZENÚanyr:r;rFr8Z new_moduler2Ú__file__rÚ __loader__rKrrÚdirnamerGÚ __package__Ú rpartitionÚcache_from_sourceÚexistsÚstatÚst_mtimeÚopenÚreadÚmarshalÚloadsrIr5rSÚcompileÚmakedirsÚdumpsÚwriter\Ú__dict__Úclose)r=rMrQÚconvertZ cachenameZ update_cacheZ sourcetimeZ cachetimeÚfÚdataÚcoder1rer4r rXr rF1s‚ ÿ      *  (     . zPy2Fixer.load_module)N) rrrrZPY2FIXERr>rArBrErSrFr r r r r6äs r6r cCs~t|tƒr|f}t|tƒr |f}t|ƒt|ƒdks>> from past import translation >>> with translation.hooks(): ... import mypy2module >>> import requests # py2/3 compatible anyway >>> # etc. cCstƒ|_tƒ|Sr‚)r…Úhooks_were_installedrr<r r r Ú __enter__Ãszhooks.__enter__cGs|js tƒdSr‚)r‡r„©r=Úargsr r r Ú__exit__Èszhooks.__exit__N©rrrrrˆr‹r r r r r†¹s r†c@s eZdZdZdd„Zdd„ZdS)Ú 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ƒ|Sr‚)r…r‡r„r<r r r rˆÛszsuspend_hooks.__enter__cGs|jr tƒdSr‚)r‡rr‰r r r r‹ßszsuspend_hooks.__exit__NrŒr r r r rÍs r)r r )&rrHÚloggingrnrr^r9Zlib2to3.pgen2.parserÚlib2to3.refactorrZ libfuturizerÚ getLoggerrr.ÚsetLevelÚDEBUGÚlistZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2r rrrr&r5Úobjectr6r}rr„r…r†rZ autotranslater r r r Ús@!     ÿþý ;54