3 \"@sTdZddlZddlmZddlmZddlmZGdddeZ Gd d d e Z dS) z2Base class for fixers (optional, but recommended).N)PatternCompiler)pygram)does_tree_importc@seZdZdZdZdZdZdZdZe j dZ e Z dZdZdZdZdZdZejZddZd d Zd d Zd dZddZdddZddZdddZddZddZ ddZ!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. NrZpostFcCs||_||_|jdS)aInitializer. Subclass may override. Args: options: a 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)optionslogcompile_pattern)selfrr r "/usr/lib64/python3.6/fixer_base.py__init__/szBaseFix.__init__cCs,|jdk r(t}|j|jdd\|_|_dS)zCompiles self.PATTERN into self.pattern. Subclass may override if it doesn't want to use self.{pattern,PATTERN} in .match(). NT)Z with_tree)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_filenameFszBaseFix.set_filenamecCsd|i}|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)rmatch)r rresultsr r r rMs z BaseFix.matchcCs tdS)aReturns 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 transformYszBaseFix.transformxxx_todo_changemecCs6|}x ||jkr$|tt|j}qW|jj||S)zReturn a string suitable for use as an identifier The new name is guaranteed not to conflict with other identifiers. ) used_namesstrnextnumbersadd)r templatenamer r r new_nameis   zBaseFix.new_namecCs.|jrd|_|jjd|j|jj|dS)NFz### In file %s ###) first_logr appendr)r messager r r log_messagetszBaseFix.log_messagecCs>|j}|j}d|_d}|j|||f|r:|j|dS)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_linenoZcloneprefixr&)r rreasonlinenoZ for_outputmsgr r r cannot_convertzszBaseFix.cannot_convertcCs|j}|jd||fdS)zUsed 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 warningszBaseFix.warningcCs(|j|_|j|tjd|_d|_dS)zSome 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 itertoolscountrr#)r treerr r r start_trees  zBaseFix.start_treecCsdS)zSome 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 r1rr r r finish_treeszBaseFix.finish_tree)r)N)"__name__ __module__ __qualname____doc__rrrrrr/r0rsetrorderZexplicitZ run_orderZ _accept_typeZkeep_line_orderZ BM_compatiblerZpython_symbolsZsymsrr rrrr"r&r-r.r2r3r r r r rs4        rcs,eZdZdZdZfddZddZZS)ConditionalFixz@ Base class for fixers which not execute if an import is found. Ncstt|j|d|_dS)N)superr:r2 _should_skip)r args) __class__r r r2szConditionalFix.start_treecCsJ|jdk r|jS|jjd}|d}dj|dd}t||||_|jS)N.rr@)r<skip_onsplitjoinr)r rZpkgr!r r r should_skips  zConditionalFix.should_skip)r4r5r6r7rAr2rD __classcell__r r )r>r r:s r:) r7r/Zpatcomprr'rZ fixer_utilrobjectrr:r r r r s