%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/lib/python3.8/lib2to3/__pycache__/
Upload File :
Create Path :
Current File : //usr/local/lib/python3.8/lib2to3/__pycache__/refactor.cpython-38.pyc

U

pak@sdZdZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZm
Z
mZddlmZddlmZmZdd	lmZd!dd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 <guido@python.org>N)chain)drivertokenizetoken)	find_root)pytreepygram)btm_matcherTcCsTt|ggdg}g}t|jD].\}}}|dr |rD|dd}||q |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_namesfindernameZispkgr,/usr/local/lib/python3.8/lib2to3/refactor.pyget_all_fix_namess
rc@seZdZdS)
_EveryNodeN__name__
__module____qualname__rrrrr+srcCst|tjtjfr(|jdkr t|jhSt|tjrH|jrDt|jStt|tj	rt
}|jD]}|D]}|t|qhq`|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)
isinstancerZNodePatternZLeafPatterntyperZNegatedPatternZcontent_get_head_typesZWildcardPatternsetupdate	Exception)patrpxrrrr/s


rc	Cstt}g}|D]x}|jrdzt|j}Wntk
rH||YqX|D]}|||qNq|jdk	r||j|q||qtt	j
jt	j
j
D]}|||qt|S)z^ Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  N)collectionsdefaultdictlistpatternrrrZ_accept_typerr	python_grammarZ
symbol2numbervaluestokensextenddict)Z
fixer_listZ
head_nodesZeveryfixerZheadsZ	node_typerrr_get_headnode_dictKs$

r1csfddtdDS)zN
    Return the fully qualified names for fixers in the package pkg_name.
    csg|]}d|qS.r).0fix_nameZpkg_namerr
<listcomp>hsz+get_fixers_from_package.<locals>.<listcomp>F)rr6rr6rget_fixers_from_packageds
r8cCs|SNr)objrrr	_identityksr;csXd}tt|jfdd}ttjtjtj	h}t
}z|\}}||krTq>q>|tjkrl|rfq6d}q>|tjkr6|dkr6|\}}|tjks|dkrq6|\}}|tjks|dkrq6|\}}|tj
kr|dkr|\}}|tjkr4|||\}}|tj
ks.|d	kr"q4|\}}qq>q6q>Wntk
rNYnXt|S)
NFcst}|d|dfS)Nrr)next)tokgenrradvancersz(_detect_future_features.<locals>.advanceTfromZ
__future__import(,)rgenerate_tokensioStringIOreadline	frozensetrNEWLINENLCOMMENTr STRINGNAMEOPadd
StopIteration)sourceZhave_docstringr@ignorefeaturestpvaluerr>r_detect_future_featuresosB








rWc@seZdZdZdS)
FixerErrorzA fixer could not be loaded.N)rrr__doc__rrrrrXsrXc@seZdZdddZdZdZd4ddZdd	Zd
dZdd
Z	ddZ
ddZd5ddZd6ddZ
ddZd7ddZddZd8ddZddZd d!Zd9d"d#Zd:d$d%Zd&Zd'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3ZdS);RefactoringToolF)print_functionwrite_unchanged_filesZFixrNcCs.||_|pg|_|j|_|dk	r0|j||jdrDtj|_ntj	|_|j
d|_g|_t
d|_g|_d|_tj|jtj|jd|_|\|_|_g|_t|_g|_g|_t|j|jD]F}|j r|j!|q||jkr|j"|q||jkr|j"|qt#|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.
        Nr[r\rZF)convertlogger)&fixersexplicit_default_optionscopyoptionsr!r	!python_grammar_no_print_statementgrammarr+getr\errorslogging	getLoggerr^	fixer_logwroterZDriverrr]
get_fixers	pre_order
post_orderfilesbmZ
BottomMatcherBMZ
bmi_pre_orderZbmi_post_orderrZ
BM_compatibleZ	add_fixerrr1bmi_pre_order_headsbmi_post_order_heads)selfZfixer_namesrcr`r0rrr__init__s>





zRefactoringTool.__init__c	CsXg}g}|jD]}t|iidg}|ddd}||jrR|t|jd}|d}|jddd	|D}zt	||}Wn&t
k
rtd
||fdYnX||j|j
}	|	jr|jdk	r||jkr|d|q|d
||	jdkr||	q|	jdkr||	qtd|	jqtd}
|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.
        rr3rN_cSsg|]}|qSr)title)r4r%rrrr7sz.RefactoringTool.get_fixers.<locals>.<listcomp>zCan't find %s.%sTzSkipping optional fixer: %szAdding transformation: %spreZpostzIllegal fixer order: %rZ	run_orderkey)r_rrsplitrFILE_PREFIXlensplitCLASS_PREFIXjoingetattrAttributeErrorrXrcrjr`log_message	log_debugorderroperator
attrgettersort)rtZpre_order_fixersZpost_order_fixersZfix_mod_pathmodr5parts
class_nameZ	fix_classr0Zkey_funcrrrrls:

zRefactoringTool.get_fixerscOsdS)zCalled when an error occurs.Nr)rtmsgargskwdsrrr	log_errorszRefactoringTool.log_errorcGs|r||}|j|dS)zHook to log a message.N)r^infortrrrrrrszRefactoringTool.log_messagecGs|r||}|j|dSr9)r^debugrrrrr	szRefactoringTool.log_debugcCsdS)zTCalled with the old version, new version, and filename of a
        refactored file.Nr)rtold_textnew_textfilenameequalrrrprint_outputszRefactoringTool.print_outputcCs8|D].}tj|r$||||q||||qdS)z)Refactor a list of files and directories.N)ospathisdirrefactor_dir
refactor_file)rtitemswrite
doctests_onlyZdir_or_filerrrrefactorszRefactoringTool.refactorc
Cstjd}t|D]\}}}|d||||D]>}|ds>tj|d|kr>tj||}	|	|	||q>dd|D|dd<qdS)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 %sr3rcSsg|]}|ds|qSr2)r)r4Zdnrrrr7.s
z0RefactoringTool.refactor_dir.<locals>.<listcomp>N)
rextsepwalkrrrrsplitextrr)
rtZdir_namerrZpy_extdirpathdirnames	filenamesrfullnamerrrrs

zRefactoringTool.refactor_dirc
Cszt|d}Wn6tk
rD}z|d||WYdSd}~XYnXzt|jd}W5|Xtj|d|dd}||fW5QRSQRXdS)	zG
        Do our best to decode a Python source file correctly.
        rbzCan't open %s: %s)NNNrr$rxencodingnewline)	openOSErrorrcloserdetect_encodingrHrFread)rtrferrrrrr_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 %srv)rrzNo changes in %s)rrrefactor_docstringr\processed_filerefactor_stringwas_changedstr)rtrrrinputroutputtreerrrr@s"zRefactoringTool.refactor_filec
Cst|}d|krtj|j_zVz|j|}Wn@tk
rl}z"|d||jj	|WYWdSd}~XYnXW5|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.
        r[zCan't parse %s: %s: %sNzRefactoring %s)
rWr	rdrreZparse_stringr"r	__class__rfuture_featuresr
refactor_tree)rtdatarrTrrrrrrWs"
 zRefactoringTool.refactor_stringcCstj}|rN|d||d}|js2||krB||d|q|dn:||d}|jsj|r~|jr~|t	|d|n
|ddS)NzRefactoring doctests in stdinz<stdin>zNo doctest changes in stdinzNo changes in stdin)
sysstdinrrrr\rrrr)rtrrrrrrrrefactor_stdinrs

zRefactoringTool.refactor_stdinc

Cst|j|jD]}|||q||j|||j||j|	}t
|r|jjD]D}||krj||rj||j
tjjdd|jr||j
tjjdt||D]}|||kr|||zt|Wntk
rYqYnX|jr||jkrq||}|r|||}|dk	r|||D] }|js^g|_|j|qL|j|	}|D]*}	|	|krg||	<||	||	qqqjqTt|j|jD]}|||q|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)r|reverser{N)rrmrnZ
start_treetraverse_byrrrsrqrunZleavesanyr,r_rrZBasedepthZkeep_line_orderZ
get_linenor)remover
ValueErrorZfixers_appliedmatch	transformreplacerr.Zfinish_treer)
rtrrr0Z	match_setnoderesultsnewZnew_matchesZfxrrrrrsJ



zRefactoringTool.refactor_treecCsV|sdS|D]D}||jD]4}||}|r|||}|dk	r|||}qqdS)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)rtr_Z	traversalrr0rrrrrrs

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)rorrrrr\
write_file)rtrrrrrrrrrrszRefactoringTool.processed_filecCsztj|d|dd}Wn6tk
rL}z|d||WYdSd}~XYnX|Fz||Wn0tk
r}z|d||W5d}~XYnXW5QRX|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.
        wrxrzCan't create %s: %sNzCan't write %s: %szWrote changes to %sT)rFrrrrrrk)rtrrrrfprrrrrs*zRefactoringTool.write_filez>>> z... c
	Csg}d}d}d}d}|jddD]}|d7}||jr~|dk	rZ|||||||}|g}||j}	|d|	}q |dk	r|||js|||jdkr|	|q |dk	r||||||d}d}|	|q |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.)
        NrTkeependsrrrx)
splitlineslstriprPS1r.refactor_doctestfindPS2rstriprr)
rtrrresultblockZblock_linenoindentlinenolineirrrrsJ
z"RefactoringTool.refactor_docstringc

s.z||}Wnjtk
r|}zLjtjrN|D]}d|dq6d|||j	j
||WYSd}~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: %sNTrrrvrcsg|]}j|qSr)r)r4rrrtrrr7Zsz4RefactoringTool.refactor_doctest.<locals>.<listcomp>)parse_blockr"r^isEnabledForrhDEBUGrrrrrrrrAssertionErrorendswithrpop)
rtrrrrrrrrZclippedrrrr@s."z RefactoringTool.refactor_doctestcCs|jrd}nd}|js$|d|n"|d||jD]}||q6|jrl|d|jD]}||q\|jrt|jdkr|dn|dt|j|jD]\}}}|j|f||qdS)	Nwerez
need to bezNo files %s modified.zFiles that %s modified:z$Warnings/messages while refactoring:rzThere was 1 error:zThere were %d errors:)rkrorrjrgr)rtrfilemessagerrrrrr	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_toksrIr)rtrrrrrrrrtszRefactoringTool.parse_blockccsdt|||j}|D]F\}}\}}\}	}
}||d7}|	|d7}	||||f|	|
f|fVqdS)z;Wraps a tokenize stream to systematically modify start/end.rN)rrE	gen_lines__next__)rtrrrr-rrVZline0Zcol0Zline1Zcol1Z	line_textrrrr~s
zRefactoringTool.wrap_toksccsx||j}||j}|}|D]N}||r>|t|dVn(||dkrVdVntd||f|}qdVqldS)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=%rrx)rrrrrr)rtrrprefix1Zprefix2prefixrrrrrs


zRefactoringTool.gen_lines)NN)FF)FF)FF)F)NFN)N)rrrrarr~rurlrrrrrrrrrrrrrrrrrrrrrrrrrrrZs>
4(
	


O

+
rZc@seZdZdS)MultiprocessingUnsupportedNrrrrrrsrcsBeZdZfddZdfdd	ZfddZfd	d
ZZS)MultiprocessRefactoringToolcs"tt|j||d|_d|_dSr9)superrruqueueoutput_lockrtrkwargsrrrrusz$MultiprocessRefactoringTool.__init__Frc
s|dkrtt|||SzddlWntk
r@tYnXjdk	rTtd_	_
fddt|D}z*|D]}|qtt|||W5jt|D]}j
dq|D]}|r|qd_XdS)Nrrz already doing multiple processescsg|]}jjdqS))target)Process_child)r4rmultiprocessingrtrrr7sz8MultiprocessRefactoringTool.refactor.<locals>.<listcomp>)rrrrImportErrorrrRuntimeError
JoinableQueueLockrrangerputis_alivestart)rtrrrZ
num_processes	processesrr%rrrrs<







z$MultiprocessRefactoringTool.refactorcsN|j}|dk	rJ|\}}ztt|j||W5|jX|j}q
dSr9)rrf	task_donerrr)rtZtaskrrrrrrs

z"MultiprocessRefactoringTool._childcs2|jdk	r|j||fntt|j||SdSr9)rrrrrrrrrrs

z)MultiprocessRefactoringTool.refactor_file)FFr)rrrrurrr
__classcell__rrrrrsr)T)#rY
__author__rFrrrrhrr'	itertoolsrZpgen2rrrZ
fixer_utilrrxrr	r
rprr"rrr1r8r;rWrXobjectrZrrrrrr<module>s8
(	

Zerion Mini Shell 1.0