JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3RbrJFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3RbrB  fk@sdZdZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z mZddlmZddlmZmZdd lmZd!d d ZGd ddeZddZddZddZddZddZGdddeZGdddeZ GdddeZ!Gdd d e Z"dS)"zRefactoring framework. Used as a main program, this can refactor any number of files and/or recursively descend down directories. Imported as a module, this provides infrastructure to write your own refactoring tool. z#Guido van Rossum N)chain)drivertokenizetoken) find_root)pytreepygram) btm_matcherTcCsXt|ggdg}g}x>t|jD].\}}}|dr"|rF|dd}||q"W|S)zEReturn a sorted list of all available fix names in the given package.*fix_N) __import__pkgutilZ iter_modules__path__ startswithappend)Z fixer_pkgZ remove_prefixZpkgZ fix_namesfindernameZispkgr5/opt/alt/python37/lib64/python3.7/lib2to3/refactor.pyget_all_fix_namess  rc@s eZdZdS) _EveryNodeN)__name__ __module__ __qualname__rrrrr+srcCst|tjtjfr(|jdkr t|jhSt|tjrH|jrDt|jStt|tj rt }x*|jD] }x|D]}| t|qlWqbW|St d|dS)zf Accepts a pytree Pattern Node and returns a set of the pattern types which will match first. Nz$Oh no! I don't understand pattern %s) isinstancerZ NodePatternZ LeafPatterntyperZNegatedPatternZcontent_get_head_typesZWildcardPatternsetupdate Exception)Zpatrpxrrrr/s      rc Cstt}g}x|D]|}|jrjyt|j}Wntk rJ||YqXxB|D]}|||qRWq|jdk r||j|q||qWx,tt j j t j j D]}|||qWt|S)z^ Accepts a list of fixers and returns a dictionary of head node type --> fixer list. N) collections defaultdictlistpatternrrrZ _accept_typerr python_grammarZ symbol2numbervaluestokensextenddict)Z fixer_listZ head_nodesZeveryfixerZheadsZ node_typerrr_get_headnode_dictKs"    r/csfddtdDS)zN Return the fully qualified names for fixers in the package pkg_name. csg|]}d|qS).r).0fix_name)pkg_namerr hsz+get_fixers_from_package..F)r)r3r)r3rget_fixers_from_packageds r5cCs|S)Nr)objrrr _identityksr7csVd}tt|jfdd}ttjtjtj h}t }yx|\}}||krVq@q@|tj krl|rfPd}q@|tj kr.|dkr.|\}}|tj ks|dkrP|\}}|tj ks|dkrP|\}}|tj kr|dkr|\}}xJ|tj kr*|||\}}|tj ks|d krP|\}}qWq@Pq@WWntk rLYnXt|S) NFcst}|d|dfS)Nrr)next)tok)genrradvancersz(_detect_future_features..advanceTfromZ __future__import(,)rgenerate_tokensioStringIOreadline frozensetrNEWLINENLCOMMENTrSTRINGNAMEOPadd StopIteration)sourceZhave_docstringr;ignorefeaturestpvaluer)r:r_detect_future_featuresosD          rRc@seZdZdZdS) FixerErrorzA fixer could not be loaded.N)rrr__doc__rrrrrSsrSc@seZdZdddZdZdZd4ddZdd Zd d Zd d Z ddZ ddZ d5ddZ d6ddZ ddZd7ddZddZd8ddZddZd d!Zd9d"d#Zd:d$d%Zd&Zd'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3ZdS);RefactoringToolF)print_functionwrite_unchanged_filesZFixr NcCs2||_|p g|_|j|_|dk r0|j||jdrDtj|_ntj |_|j d|_ g|_ t d|_g|_d|_tj|jtj|jd|_|\|_|_g|_t|_g|_g|_xXt|j|jD]F}|j r|j!|q||jkr|j"|q||jkr|j"|qWt#|j|_$t#|j|_%dS)zInitializer. Args: fixer_names: a list of fixers to import options: a dict with configuration. explicit: a list of fixers to run even if they are explicit. NrVrWrUF)convertlogger)&fixersexplicit_default_optionscopyoptionsr r !python_grammar_no_print_statementgrammarr)getrWerrorsloggingZ getLoggerrY fixer_logwroterZDriverrrX get_fixers pre_order post_orderfilesbmZ BottomMatcherBMZ bmi_pre_orderZbmi_post_orderrZ BM_compatibleZ add_fixerrr/bmi_pre_order_headsbmi_post_order_heads)selfZ fixer_namesr^r[r.rrr__init__s<           zRefactoringTool.__init__c Cs^g}g}x(|jD]}t|iidg}|ddd}||jrV|t|jd}|d}|jddd |D}yt ||}Wn&t k rt d ||fdYnX||j |j } | jr|jd k r||jkr|d |q|d || jdkr || q| jdkr"|| qt d| jqWtd} |j| d|j| d||fS)aInspects the options to load the requested patterns and handlers. Returns: (pre_order, post_order), where pre_order is the list of fixers that want a pre-order AST traversal, and post_order is the list that want post-order traversal. r r0rN_cSsg|] }|qSr)title)r1r#rrrr4sz.RefactoringTool.get_fixers..zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %sZpreZpostzIllegal fixer order: %rZ run_order)key)rZrrsplitr FILE_PREFIXlensplit CLASS_PREFIXjoingetattrAttributeErrorrSr^rdr[ log_message log_debugorderroperator attrgettersort) rnZpre_order_fixersZpost_order_fixersZ fix_mod_pathmodr2parts class_nameZ fix_classr.Zkey_funcrrrrfs8            zRefactoringTool.get_fixerscOsdS)zCalled when an error occurs.Nr)rnmsgargskwdsrrr log_errorszRefactoringTool.log_errorcGs|r ||}|j|dS)zHook to log a message.N)rYinfo)rnrrrrrr}szRefactoringTool.log_messagecGs|r ||}|j|dS)N)rYdebug)rnrrrrrr~ szRefactoringTool.log_debugcCsdS)zTCalled with the old version, new version, and filename of a refactored file.Nr)rnold_textnew_textfilenameequalrrr print_outputszRefactoringTool.print_outputcCs<x6|D].}tj|r&||||q||||qWdS)z)Refactor a list of files and directories.N)ospathisdir refactor_dir refactor_file)rnitemswrite doctests_onlyZ dir_or_filerrrrefactors  zRefactoringTool.refactorc Cstjd}xt|D]\}}}|d|||xF|D]>}|dsBtj|d|krBtj||} | | ||qBWdd|D|dd<qWdS)zDescends down a directory and refactor every Python file found. Python files are assumed to have a .py extension. Files and subdirectories starting with '.' are skipped. pyzDescending into %sr0rcSsg|]}|ds|qS)r0)r)r1Zdnrrrr4.sz0RefactoringTool.refactor_dir..N) rextsepwalkr~rrrsplitextrzr) rnZdir_namerrZpy_extdirpathZdirnames filenamesrfullnamerrrrs    zRefactoringTool.refactor_dirc Csyt|d}Wn0tk r>}z|d||dSd}~XYnXzt|jd}Wd|Xtj|d|dd}||fSQRXdS) zG Do our best to decode a Python source file correctly. rbzCan't open %s: %s)NNNrr"rr)encodingnewline) openOSErrorrrdetect_encodingrCcloserAread)rnrferrrrrr_read_python_source0s z#RefactoringTool._read_python_sourcecCs||\}}|dkrdS|d7}|rn|d||||}|jsL||kr`||||||q|d|nH|||}|js|r|jr|jt|dd|||dn |d|dS)zRefactors a file.N zRefactoring doctests in %szNo doctest changes in %srp)rrzNo changes in %s)rr~refactor_docstringrWprocessed_filerefactor_string was_changedstr)rnrrrinputroutputtreerrrr@s    zRefactoringTool.refactor_filec Cst|}d|krtj|j_zLy|j|}Wn6tk rb}z|d||jj |dSd}~XYnXWd|j|j_X||_ | d|| |||S)aFRefactor a given input string. Args: data: a string holding the code to be refactored. name: a human-readable name for use in error/log messages. Returns: An AST corresponding to the refactored input stream; None if there were errors during the parse. rVzCan't parse %s: %s: %sNzRefactoring %s) rRr r_rr`Z parse_stringr!r __class__rfuture_featuresr~ refactor_tree)rndatarrOrrrrrrWs     zRefactoringTool.refactor_stringcCstj}|rN|d||d}|js2||krB||d|q|dn:||d}|jsj|r~|jr~|t |d|n |ddS)NzRefactoring doctests in stdinzzNo doctest changes in stdinzNo changes in stdin) sysstdinrr~rrWrrrr)rnrrrrrrrrefactor_stdinrs     zRefactoringTool.refactor_stdinc Csx"t|j|jD]}|||qW||j|||j||j| }xtt | rΐx^|jj D]P}||krv||rv||j tjjdd|jr||j tjjdxt||D]}|||kr|||y t|Wntk rwYnX|jr&||jkr&q||}|r|||}|dk r||x,|D] }|jsng|_|j|q\W|j| }x2|D]*} | |krg|| <|| || qWqWqvWq\Wx$t|j|jD]}|||qW|jS)aRefactors a parse tree (modifying the tree in place). For compatible patterns the bottom matcher module is used. Otherwise the tree is traversed node-to-node for matches. Args: tree: a pytree.Node instance representing the root of the tree to be refactored. name: a human-readable name for this tree. Returns: True if the tree was modified, False otherwise. T)rtreverse)rtN)rrgrhZ start_tree traverse_byrlrmrkrunZleavesanyr*rZrrZBaseZdepthZkeep_line_orderZ get_linenor'remover ValueErrorZfixers_appliedmatch transformreplacerr,Z finish_treer) rnrrr.Z match_setnoderesultsnewZ new_matchesZfxrrrrrsJ       $zRefactoringTool.refactor_treecCs^|sdSxP|D]H}xB||jD]4}||}|r|||}|dk r|||}qWqWdS)aTraverse an AST, applying a set of fixers to each node. This is a helper method for refactor_tree(). Args: fixers: a list of fixer instances. traversal: a generator that yields AST nodes. Returns: None N)rrrr)rnrZZ traversalrr.rrrrrrs     zRefactoringTool.traverse_bycCs|j||dkr.||d}|dkr.dS||k}||||||r`|d||js`dS|rv|||||n |d|dS)zR Called when a file has been refactored and there may be changes. NrzNo changes to %szNot writing changes to %s)rirrrr~rW write_file)rnrrrrrrrrrrs  zRefactoringTool.processed_filec Csytj|d|dd}Wn0tk rF}z|d||dSd}~XYnX|Fy||Wn0tk r}z|d||Wdd}~XYnXWdQRX|d|d|_dS) zWrites a string to a file. It first shows a unified diff between the old text and the new text, and then rewrites the file; the latter is only done if the write option is set. wrr)rrzCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rArrrrr~re)rnrrrrfprrrrrs* zRefactoringTool.write_filez>>> z... c Csg}d}d}d}d}x|jddD]}|d7}||jr|dk r\|||||||}|g}||j} |d| }q"|dk r|||js|||jdkr| |q"|dk r||||||d}d}| |q"W|dk r||||||d |S)aRefactors a docstring, looking for doctests. This returns a modified version of the input string. It looks for doctests, which start with a ">>>" prompt, and may be continued with "..." prompts, as long as the "..." is indented the same as the ">>>". (Unfortunately we can't use the doctest module's parser, since, like most parsers, it is not geared towards preserving the original source.) NrT)keependsrrrr) splitlineslstriprPS1r,refactor_doctestfindPS2rstriprrz) rnrrresultblockZ block_linenoindentlinenolineirrrrs:          z"RefactoringTool.refactor_docstringc s*y||}Wnftk rx}zHjtjrRx|D]}d|dq8Wd|||j j ||Sd}~XYnX ||r&t |j dd}|d|d||dd} }| dg|dkst| |dds|dd7<j|d g}|r&|fd d |D7}|S) zRefactors one doctest. A doctest is given as a block of lines, the first of which starts with ">>>" (possibly indented), while the remaining lines start with "..." (identically indented). z Source: %srz+Can't parse docstring in %s line %s: %s: %sNT)rrrprcsg|]}j|qSr)r)r1r)rrnrrr4Zsz4RefactoringTool.refactor_doctest..) parse_blockr!rYZ isEnabledForrcDEBUGr~rrrrrrrAssertionErrorendswithrpop) rnrrrrrrrrZclippedr)rrnrr@s& "z RefactoringTool.refactor_doctestcCs|jr d}nd}|js$|d|n&|d|x|jD]}||q8W|jrt|dx|jD]}||qbW|jrt|jdkr|dn|dt|jx&|jD]\}}}|j|f||qWdS) Nwerez need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:rzThere was 1 error:zThere were %d errors:)rerir}rdrbrw)rnrfilemessagerrrrrr summarize]s$     zRefactoringTool.summarizecCs"|j||||}t|_|S)zParses a block into a tree. This is necessary to get correct line number / offset information in the parser diagnostics and embedded into the parse tree. )rZ parse_tokens wrap_toksrDr)rnrrrrrrrrtszRefactoringTool.parse_blockc csht|||j}xN|D]F\}}\}}\} } } ||d7}| |d7} ||||f| | f| fVqWdS)z;Wraps a tokenize stream to systematically modify start/end.rN)rr@ gen_lines__next__) rnrrrr+rrQZline0Zcol0Zline1Zcol1Z line_textrrrr~s   zRefactoringTool.wrap_toksccs||j}||j}|}xV|D]N}||r@|t|dVn(||dkrXdVntd||f|}qWx dVqrWdS)zGenerates lines as expected by tokenize from a list of lines. This strips the first len(indent + self.PS1) characters off each line. Nrzline=%r, prefix=%rrr)rrrrwrr)rnrrprefix1Zprefix2prefixrrrrrs    zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrr\ryrvrorfrr}r~rrrrrrrrrrrrrrrrrrrrrrrrUs: 4(   O  + rUc@s eZdZdS)MultiprocessingUnsupportedN)rrrrrrrrsrcsBeZdZfddZd fdd ZfddZfd d ZZS) MultiprocessRefactoringToolcs"tt|j||d|_d|_dS)N)superrroqueue output_lock)rnrkwargs)rrrrosz$MultiprocessRefactoringTool.__init__Frc s|dkrtt|||Sy ddlWntk r@tYnXjdk rTtd_ _ fddt |D}z.x|D] }| qWtt|||Wdj xt |D]}jdqWx|D]}|r| qWd_XdS)Nrrz already doing multiple processescsg|]}jjdqS))target)ZProcess_child)r1r)multiprocessingrnrrr4sz8MultiprocessRefactoringTool.refactor..)rrrr ImportErrorrr RuntimeErrorZ JoinableQueueZLockrrangestartrzputZis_alive)rnrrrZ num_processesZ processesr#r)r)rrnrrs2               z$MultiprocessRefactoringTool.refactorcsR|j}xB|dk rL|\}}ztt|j||Wd|jX|j}q WdS)N)rrarrrZ task_done)rnZtaskrr)rrrrs     z"MultiprocessRefactoringTool._childcs2|jdk r|j||fntt|j||SdS)N)rrrrr)rnrr)rrrrs  z)MultiprocessRefactoringTool.refactor_file)FFr)rrrrorrr __classcell__rr)rrrs   r)T)#rT __author__rArrrrcrr% itertoolsrZpgen2rrrZ fixer_utilrrrrr r rjrr!rrr/r5r7rRrSobjectrUrrrrrr s8    (