��Qc@�s�dZddlmZdZddlZddlZddlZddlZddlZddl Z ddl
mZddlm
Z
mZmZddlmZdd lmZmZdd
lmZddlmZed�Zd
efd��YZd�Zd�Zd�Zd�Z ej!ddfkr[ddl"Z"e"j#Z$d�Z%d�Z&ne#Z$e Z%e Z&d�Z'defd��YZ(de)fd��YZ*defd��YZ+de*fd��YZ,dS( s�Refactoring 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.
i�(twith_statements#Guido van Rossum <[email protected]>N(tchaini(tdriverttokenizettoken(t find_root(tpytreetpygram(t btm_utils(tbtm_matchercC�s�t|ggdg�}tjj|j�}g}xbttj|��D]K}|jd�rI|jd�rI|r�|d}n|j |d �qIqIW|S(sEReturn a sorted list of all available fix names in the given package.t*tfix_s.pyii�(
t
__import__tostpathtdirnamet__file__tsortedtlistdirt
startswithtendswithtappend(t fixer_pkgt
remove_prefixtpkgt fixer_dirt fix_namestname((s&/sys/lib/python2.7/lib2to3/refactor.pytget_all_fix_names!s
t
_EveryNodecB�seZRS((t__name__t
__module__(((s&/sys/lib/python2.7/lib2to3/refactor.pyR.scC�s�t|tjtjf�rC|jdkr3t�nt|jg�St|tj�rt|j rkt
|j �St�nt|tj�r�t�}x5|j D]*}x!|D]}|jt
|��q�Wq�W|St
d|��dS(sf Accepts a pytree Pattern Node and returns a set
of the pattern types which will match first. s$Oh no! I don't understand pattern %sN(t
isinstanceRtNodePatterntLeafPatternttypetNoneRtsettNegatedPatterntcontentt_get_head_typestWildcardPatterntupdatet Exception(tpattrtptx((s&/sys/lib/python2.7/lib2to3/refactor.pyR(2s
cC�stjt�}g}x�|D]�}|jr�yt|j�}Wntk
r^|j|�q�XxU|D]}||j|�qfWq|jdk r�||jj|�q|j|�qWx:t t
jjj
�t
jj�D]}||j|�q�Wt|�S(s^ Accepts a list of fixers and returns a dictionary
of head node type --> fixer list. N(tcollectionstdefaultdicttlisttpatternR(RRt_accept_typeR$RRtpython_grammart
symbol2numbert
itervaluesttokenstextendtdict(t
fixer_listt
head_nodesteverytfixertheadst node_type((s&/sys/lib/python2.7/lib2to3/refactor.pyt_get_headnode_dictNs"
cC�s(gt|t�D]}|d|^qS(sN
Return the fully qualified names for fixers in the package pkg_name.
t.(RtFalse(tpkg_nametfix_name((s&/sys/lib/python2.7/lib2to3/refactor.pytget_fixers_from_packagegscC�s|S(N((tobj((s&/sys/lib/python2.7/lib2to3/refactor.pyt _identitynsiicC�s|jdd�S(Nu
u
(treplace(tinput((s&/sys/lib/python2.7/lib2to3/refactor.pyt_from_system_newlinesuscC�s*tjdkr"|jdtj�S|SdS(Ns
u
(R
tlinesepRI(RJ((s&/sys/lib/python2.7/lib2to3/refactor.pyt_to_system_newlineswsc�s�t}tjtj|�j���fd�}ttjtjtj f�}t
�}ykxdtr�|�\}}||kr�q]q]|tjkr�|r�Pnt}q]|tj
kr�|dkr�|�\}}|tj
ks�dkr�n|�\}}|tj
ks|dkrPn|�\}}|tjkrY|dkrY|�\}}nxa|tj
kr�|j|�|�\}}|tjks�|dkr�Pn|�\}}q\Wq]Pq]WWntk
r�nXt|�S(Nc�s�j�}|d|dfS(Nii(tnext(ttok(tgen(s&/sys/lib/python2.7/lib2to3/refactor.pytadvance�sufromu
__future__uimportu(u,(RCRtgenerate_tokenstStringIOtreadlinet frozensetRtNEWLINEtNLtCOMMENTR%tTruetSTRINGtNAMEtOPtaddt
StopIteration(tsourcethave_docstringRQtignoretfeaturesttptvalue((RPs&/sys/lib/python2.7/lib2to3/refactor.pyt_detect_future_features�sD
t
FixerErrorcB�seZdZRS(sA fixer could not be loaded.(RRt__doc__(((s&/sys/lib/python2.7/lib2to3/refactor.pyRf�stRefactoringToolcB�s!eZied6ed6ZdZdZddd�Zd�Zd�Z d�Z
d�Zd �Zeed
�Z
eed�Zd�Zeed
�Zd�Zed�Zd�Zd�Zdedd�Zdd�ZdZdZd�Zd�Zd�Zd�Zd�Zd�ZRS(tprint_functiontwrite_unchanged_filestFixRcC�s�||_|pg|_|jj�|_|dk rI|jj|�n|jdretj|_ ntj
|_ |jjd�|_g|_
tjd�|_g|_t|_tj|j dtjd|j�|_|j�\|_|_g|_tj�|_g|_g|_ x}t!|j|j�D]f}|j"rT|jj#|�q2||jkrv|jj$|�q2||jkr2|j j$|�q2q2Wt%|j�|_&t%|j �|_'dS(s�Initializer.
Args:
fixer_names: a list of fixers to import
options: an dict with configuration.
explicit: a list of fixers to run even if they are explicit.
RiRjRhtconverttloggerN((tfixerstexplicitt_default_optionstcopytoptionsR$R*Rt!python_grammar_no_print_statementtgrammarR5tgetRjterrorstloggingt getLoggerRmt fixer_logRCtwroteRtDriverRRlt
get_fixerst pre_ordert
post_ordertfilestbmt
BottomMatchertBMt
bmi_pre_ordertbmi_post_orderRt
BM_compatiblet add_fixerRRAtbmi_pre_order_headstbmi_post_order_heads(tselftfixer_namesRrRoR>((s&/sys/lib/python2.7/lib2to3/refactor.pyt__init__�s<
cC�s�g}g}x�|jD]�}t|iidg�}|jdd�d}|j|j�rr|t|j�}n|jd�}|jdjg|D]}|j �^q��}yt
||�} Wn'tk
r�td||f��nX| |j
|j�}
|
jr?|jtk r?||jkr?|jd|�qn|jd |�|
jd
krn|j|
�q|
jdkr�|j|
�qtd|
j��qWtjd
�}|jd|�|jd|�||fS(sInspects 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
RBii�t_tsCan't find %s.%ssSkipping implicit fixer: %ssAdding transformation: %stpretpostsIllegal fixer order: %rt run_ordertkey(RnRtrsplitRtFILE_PREFIXtlentsplittCLASS_PREFIXtjointtitletgetattrtAttributeErrorRfRrRyRoRYtlog_messaget log_debugtorderRtoperatort
attrgettertsort(R�tpre_order_fixerstpost_order_fixerstfix_mod_pathtmodREtpartsR.t
class_namet fix_classR>tkey_func((s&/sys/lib/python2.7/lib2to3/refactor.pyR|�s8/
cO�s�dS(sCalled when an error occurs.N((R�tmsgtargstkwds((s&/sys/lib/python2.7/lib2to3/refactor.pyt log_errorscG�s'|r||}n|jj|�dS(sHook to log a message.N(Rmtinfo(R�R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR�s
cG�s'|r||}n|jj|�dS(N(Rmtdebug(R�R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR�s
cC�sdS(sTCalled with the old version, new version, and filename of a
refactored file.N((R�told_texttnew_texttfilenametequal((s&/sys/lib/python2.7/lib2to3/refactor.pytprint_output!scC�sPxI|D]A}tjj|�r5|j|||�q|j|||�qWdS(s)Refactor a list of files and directories.N(R
Rtisdirtrefactor_dirt
refactor_file(R�titemstwritet
doctests_onlytdir_or_file((s&/sys/lib/python2.7/lib2to3/refactor.pytrefactor&s
cC�s�tjd}x�tj|�D]�\}}}|jd|�|j�|j�xe|D]]}|jd�rWtjj|�d|krWtjj||�} |j | ||�qWqWWg|D]}
|
jd�s�|
^q�|(qWdS(s�Descends 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.
tpysDescending into %sRBiN(
R
textseptwalkR�R�RRtsplitextR�R�(R�tdir_nameR�R�tpy_exttdirpathtdirnamest filenamesRtfullnametdn((s&/sys/lib/python2.7/lib2to3/refactor.pyR�/s
cC�s�yt|d�}Wn'tk
r<}|jd||�dSXztj|j�d}Wd|j�Xt|dd|��}t |j
��|fSWdQXdS(sG
Do our best to decode a Python source file correctly.
trbsCan't open %s: %siNR-tencoding(NN(topentIOErrorR�R$Rtdetect_encodingRTtcloset_open_with_encodingRKtread(R�R�tfterrR�((s&/sys/lib/python2.7/lib2to3/refactor.pyt_read_python_sourceCsc C�s|j|�\}}|dkr%dS|d7}|r�|jd|�|j||�}|jsl||kr�|j|||||�q�jd|�nc|j||�}|js�|r�jr�jt|�d |d|d|�n|jd|�dS( sRefactors a file.Nu
sRefactoring doctests in %ssNo doctest changes in %si�R�R�sNo changes in %s( R�R$R�trefactor_docstringRjtprocessed_filetrefactor_stringtwas_changedtunicode(R�R�R�R�RJR�toutputttree((s&/sys/lib/python2.7/lib2to3/refactor.pyR�Ss
cC�s�t|�}d|kr*tj|j_nzMy|jj|�}Wn0tk
ru}|jd||jj |�dSXWd|j|j_X||_
|jd|�|j||�|S(sFRefactor 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.
RisCan't parse %s: %s: %sNsRefactoring %s(
ReRRsRRttparse_stringR+R�t __class__Rtfuture_featuresR�t
refactor_tree(R�tdataRRbR�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR�js cC�s�tjj�}|ro|jd�|j|d�}|jsI||kr_|j|d|�q�|jd�nS|j|d�}|js�|r�|jr�|jt |�d|�n
|jd�dS(NsRefactoring doctests in stdins<stdin>sNo doctest changes in stdinsNo changes in stdin(
tsyststdinR�R�R�RjR�R�R�R�(R�R�RJR�R�((s&/sys/lib/python2.7/lib2to3/refactor.pytrefactor_stdin�s
c
C�s�x-t|j|j�D]}|j||�qW|j|j|j��|j|j|j��|jj|j ��}x�
|j��rcx�|jjD]�}||kr�||r�||j
dtjjdt�|jr�|j
dtjj�nx[t||�D]F}|||kr9||j|�nyt|�Wntk
r]qnX|jr|||jkr|qn|j|�}|r|j||�}|dk rU|j|�x9|j�D]+}|js�|_n|jj|�q�W|jj|j ��}x?|D]4} | |kr6g|| <n|| j|| �qWqUqqWq�q�Wq}Wx-t|j|j�D]}|j||�qzW|j S(s�Refactors 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.
R�treverseN(!RR}R~t
start_treettraverse_byR�R�R�truntleavestanytvaluesRnR�RtBasetdepthRYtkeep_line_ordert
get_linenoR2tremoveRt
ValueErrortfixers_appliedtmatcht transformR$RIRR9tfinish_treeR�(
R�R�RR>t match_settnodetresultstnewtnew_matchestfxr((s&/sys/lib/python2.7/lib2to3/refactor.pyR��sJ
.cC�s�|s
dSxr|D]j}xa||jD]R}|j|�}|r%|j||�}|dk rw|j|�|}qwq%q%WqWdS(sTraverse 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(R#R�R�R$RI(R�Rnt traversalR�R>R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR��s
cC�s�|jj|�|dkrB|j|�d}|dkrBdSn||k}|j||||�|r�|jd|�|js�dSn|r�|j||||�n|jd|�dS(sR
Called when a file has been refactored and there may be changes.
iNsNo changes to %ssNot writing changes to %s(RRR$R�R�R�Rjt
write_file(R�R�R�R�R�R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR��s cC�s�yt|dd|�}Wn*tjk
rE}|jd||�dSXzGy|jt|��Wn)tjk
r�}|jd||�nXWd|j�X|jd|�t|_ dS(s�Writes 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.
twR�sCan't create %s: %sNsCan't write %s: %ssWrote changes to %s(
R�R
terrorR�R�RMR�R�RYRz(R�R�R�R�R�R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyR�ss>>> s... c
C�s�g}d}d}d}d}x+|jt�D]}|d7}|j�j|j�r�|dk r�|j|j||||��n|}|g}|j|j�} || }q.|dk r|j||j �s�||j j
�dkr|j|�q.|dk r/|j|j||||��nd}d}|j|�q.W|dk rz|j|j||||��ndj|�S(s�Refactors 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.)
iiu
uN(
R$t
splitlinesRYtlstripRtPS1R9trefactor_doctesttfindtPS2trstripRR�(
R�RJR�tresulttblocktblock_linenotindenttlinenotlineti((s&/sys/lib/python2.7/lib2to3/refactor.pyR�(s:
c
C�ssy|j|||�}Wnutk
r�}|jjtj�rmx*|D]}|jd|jd��qGWn|jd|||j j
|�|SX|j||�rot|�j
t�}||d ||d} }| dg|dks�| ��|djd�s|dcd7<n||j|jd�g}|ro|g|D]}||j|^qK7}qon|S(s�Refactors 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).
s
Source: %su
s+Can't parse docstring in %s line %s: %s: %sii�i(tparse_blockR+RmtisEnabledForRwtDEBUGR�RR�R�RR�R�R�RYtAssertionErrorRR�tpopR(
R�RRRR�R�R�RR�tclipped((s&/sys/lib/python2.7/lib2to3/refactor.pyR�Ss&
#.cC�s|jrd}nd}|js4|jd|�n1|jd|�x|jD]}|j|�qNW|jr�|jd�x!|jD]}|j|�q�Wn|jrt|j�dkr�|jd�n|jdt|j��x0|jD]"\}}}|j|||�q�dS( Ntweres
need to besNo files %s modified.sFiles that %s modified:s$Warnings/messages while refactoring:isThere was 1 error:sThere were %d errors:(RzRR�RyRvR�(R�RtfiletmessageR�R�R�((s&/sys/lib/python2.7/lib2to3/refactor.pyt summarizeps$
cC�s1|jj|j|||��}t�|_|S(s�Parses 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.
(Rtparse_tokenst wrap_toksRUR�(R�RRRR�((s&/sys/lib/python2.7/lib2to3/refactor.pyR
�s!cc�s�tj|j||�j�}xe|D]]\}}\}}\} }
}||d7}| |d7} ||||f| |
f|fVq%WdS(s;Wraps a tokenize stream to systematically modify start/end.iN(RRRt gen_linesRN(R�RRRR8R#Rdtline0tcol0tline1tcol1t line_text((s&/sys/lib/python2.7/lib2to3/refactor.pyR�s
(cc�s�||j}||j}|}xi|D]a}|j|�rN|t|�Vn4||j�dkrldVntd||f��|}q'Wxtr�dVq�WdS(s�Generates lines as expected by tokenize from a list of lines.
This strips the first len(indent + self.PS1) characters off each line.
u
sline=%r, prefix=%rR�N(R�RRR�RR
RY(R�RRtprefix1tprefix2tprefixR((s&/sys/lib/python2.7/lib2to3/refactor.pyR�s
N(RRRCRpR�R�R$R�R|R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�RR�R�RR
RR(((s&/sys/lib/python2.7/lib2to3/refactor.pyRh�s:
4 ( O +
tMultiprocessingUnsupportedcB�seZRS((RR(((s&/sys/lib/python2.7/lib2to3/refactor.pyR�stMultiprocessRefactoringToolcB�s5eZd�Zeedd�Zd�Zd�ZRS(cO�s/tt|�j||�d|_d|_dS(N(tsuperR R�R$tqueuetoutput_lock(R�R�tkwargs((s&/sys/lib/python2.7/lib2to3/refactor.pyR��s ic C�sf|dkr(tt|�j|||�Syddl}Wntk
rQt�nX|jdk rptd��n|j �|_|j
�|_gt|�D]}|j
d|j�^q�}z;x|D]}|j�q�Wtt|�j|||�Wd|jj�x$t|�D]}|jjd�qWx'|D]}|j�r5|j�q5q5Wd|_XdS(Nii�s already doing multiple processesttarget(R!R R�tmultiprocessingtImportErrorRR"R$tRuntimeErrort
JoinableQueuetLockR#txrangetProcesst_childtstartR�tputtis_alive( R�R�R�R�t
num_processesR&R t processesR.((s&/sys/lib/python2.7/lib2to3/refactor.pyR��s2
+
cC�so|jj�}xY|dk rj|\}}ztt|�j||�Wd|jj�X|jj�}qWdS(N(R"RuR$R!R R�t task_done(R�ttaskR�R$((s&/sys/lib/python2.7/lib2to3/refactor.pyR-�scO�sE|jdk r(|jj||f�ntt|�j||�SdS(N(R"R$R/R!R R�(R�R�R$((s&/sys/lib/python2.7/lib2to3/refactor.pyR��s(RRR�RCR�R-R�(((s&/sys/lib/python2.7/lib2to3/refactor.pyR �s
(-Rgt
__future__Rt
__author__R
R�RwR�R0RSt itertoolsRtpgen2RRRt
fixer_utilRR�RRRtbuR R�RYRR+RR(RARFRHtversion_infotcodecsR�R�RKRMReRftobjectRhRR (((s&/sys/lib/python2.7/lib2to3/refactor.pyt<module> sF
(�
|