f _"c@sTdZddlZddlmZddlmZddlmZGdddeZ Gd d d e Z dS) z2Base class for fixers (optional, but recommended).iN)PatternCompiler)pygram)does_tree_importc@seZdZdZdZdZdZdZdZe 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. NrZpostFicCs||_||_|dS)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)selfrrr +/usr/lib64/python3.10/lib2to3/fixer_base.py__init__/szBaseFix.__init__cCs,|jdur(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)PATTERNrrpattern pattern_tree)rZPCr r r r;s  zBaseFix.compile_patterncCs ||_dS)zOSet the filename. The main refactoring tool should call this. N)filename)rrr r r set_filenameFszBaseFix.set_filenamecCsd|i}|j||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 matchrrZresultsr 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)ZNotImplementedErrorrr r r transformYszBaseFix.transformxxx_todo_changemecCs2|}||jvr"|tt|j}q|j||S)zReturn a string suitable for use as an identifier The new name is guaranteed not to conflict with other identifiers. ) used_namesZstrZnextnumbersZadd)rZtemplatenamer r r new_nameis   zBaseFix.new_namecCs.|jrd|_|jd|j|j|dS)NFz### In file %s ###) first_logrZappendr)rZmessager r r log_messagetszBaseFix.log_messagecCs>|}|}d|_d}||||f|r:||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_linenoZcloneZprefixr)rrreasonlinenoZ for_outputZmsgr r r cannot_convertzszBaseFix.cannot_convertcCs|}|d||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)rr)rrrrr r r warningszBaseFix.warningcCs(|j|_||td|_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 itertoolscountrrrZtreerr 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$r r r finish_treeszBaseFix.finish_tree)rN)"__name__ __module__ __qualname____doc__r r rrrr"r#rZsetrZorderZexplicitZ run_orderZ _accept_typeZkeep_line_orderZ BM_compatiblerZpython_symbolsZsymsr rrrrrrr r!r%r&r r r r rs4        rcs,eZdZdZdZfddZddZZS)ConditionalFixz@ Base class for fixers which not execute if an import is found. Ncstt|j|d|_dSr')Zsuperr,r% _should_skip)rZargsZ __class__r r r%szConditionalFix.start_treecCsJ|jdur|jS|jd}|d}d|dd}t||||_|jS)NZ.i)r-skip_onZsplitZjoinr)rrZpkgrr r r should_skips  zConditionalFix.should_skip)r(r)r*r+r/r%r0Z __classcell__r r r.r r,s r,) r+r"ZpatcomprrrZ fixer_utilrZobjectrr,r r r r Zs