î j f2ã@s~dZddlZddlZddlmZddlmZddlmZGdd„de ƒZ Gd d „d e ƒZ dS) z2Base class for fixers (optional, but recommended).éNé)ÚPatternCompiler)Úpygram)Údoes_tree_importc@seZdZdZdZdZdZdZdZe j dƒZ e ƒZ dZdZdZdZdZdZejZdd„Zd d „Zd d „Zd d„Zdd„Zddd„Zdd„Zddd„Zdd„Zdd„Z dd„Z!dS)ÚBaseFixaOptional base class for fixers. The subclass name must be FixFooBar where FooBar is the result of removing underscores and capitalizing the words of the fix name. For example, the class name for a fixer named 'has_key' should be FixHasKey. NrZpostFécCs ||_||_|jƒdS)aInitializer. Subclass may override. Args: options: an dict containing the options passed to RefactoringTool that could be used to customize the fixer through the command line. log: a list to append warnings and other messages to. N)ÚoptionsÚlogÚcompile_pattern)Úselfrr ©r ú7/opt/alt/python34/lib64/python3.4/lib2to3/fixer_base.pyÚ__init__0s  zBaseFix.__init__cCsC|jdk r?tƒ}|j|jddƒ\|_|_ndS)z–Compiles self.PATTERN into self.pattern. Subclass may override if it doesn't want to use self.{pattern,PATTERN} in .match(). NZ with_treeT)ÚPATTERNrr ÚpatternÚ pattern_tree)r ÚPCr r r r <s zBaseFix.compile_patterncCs ||_dS)zOSet the filename. The main refactoring tool should call this. N)Úfilename)r rr r r Ú set_filenameGszBaseFix.set_filenamecCs&i|d6}|jj||ƒo%|S)aReturns match for a given parse tree node. Should return a true or false object (not necessarily a bool). It may return a non-empty dict of matching sub-nodes as returned by a matching pattern. Subclass may override. Únode)rÚmatch)r rÚresultsr r r rNs z BaseFix.matchcCs tƒ‚dS)a¶Returns the transformation for a given parse tree node. Args: node: the root of the parse tree that matched the fixer. results: a dict mapping symbolic names to part of the match. Returns: None, or a node that is a modified copy of the argument node. The node argument may also be modified in-place to effect the same change. Subclass *must* override. N)ÚNotImplementedError)r rrr r r Ú transformZszBaseFix.transformZxxx_todo_changemecCsI|}x,||jkr4|tt|jƒƒ}q W|jj|ƒ|S)z†Return a string suitable for use as an identifier The new name is guaranteed not to conflict with other identifiers. )Ú used_namesÚstrÚnextÚnumbersÚadd)r ÚtemplateÚnamer r r Únew_namejs zBaseFix.new_namecCs@|jr,d|_|jjd|jƒn|jj|ƒdS)NFz### In file %s ###)Ú first_logr Úappendr)r Úmessager r r Ú log_messageus  zBaseFix.log_messagecCsX|jƒ}|jƒ}d|_d}|j|||fƒ|rT|j|ƒndS)aWarn the user that a given chunk of code is not valid Python 3, but that it cannot be converted automatically. First argument is the top-level node for the code in question. Optional second argument is why it can't be converted. ÚzLine %d: could not convert: %sN)Ú get_linenoZcloneÚprefixr%)r rÚreasonÚlinenoZ for_outputÚmsgr r r Úcannot_convert{s   zBaseFix.cannot_convertcCs'|jƒ}|jd||fƒdS)zàUsed for warning the user about possible uncertainty in the translation. First argument is the top-level node for the code in question. Optional second argument is why it can't be converted. z Line %d: %sN)r'r%)r rr)r*r r r ÚwarningŠs zBaseFix.warningcCs8|j|_|j|ƒtjdƒ|_d|_dS)zîSome fixers need to maintain tree-wide state. This method is called once, at the start of tree fix-up. tree - the root node of the tree to be processed. filename - the name of the file the tree came from. rTN)rrÚ itertoolsÚcountrr")r Útreerr r r Ú start_tree”s  zBaseFix.start_treecCsdS)zóSome fixers need to maintain tree-wide state. This method is called once, at the conclusion of tree fix-up. tree - the root node of the tree to be processed. filename - the name of the file the tree came from. Nr )r r0rr r r Ú finish_tree szBaseFix.finish_tree)"Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrr.r/rÚsetrZorderZexplicitZ run_orderZ _accept_typeZkeep_line_orderZ BM_compatiblerZpython_symbolsZsymsrr rrrr!r%r,r-r1r2r r r r rs4       rcs:eZdZdZdZ‡fdd†Zdd„Z‡S)ÚConditionalFixz@ Base class for fixers which not execute if an import is found. Ncs#tt|ƒj|Œd|_dS)N)Úsuperr8r1Ú _should_skip)r Úargs)Ú __class__r r r1°szConditionalFix.start_treecCsg|jdk r|jS|jjdƒ}|d}dj|dd…ƒ}t|||ƒ|_|jS)NÚ.réÿÿÿÿr>)r:Úskip_onÚsplitÚjoinr)r rZpkgr r r r Ú should_skip´s zConditionalFix.should_skip)r3r4r5r6r?r1rBr r )r<r r8ªs r8) r6Zloggingr.Zpatcomprr&rZ fixer_utilrÚobjectrr8r r r r Ús  ›