<s> """<STR_LIT>""" <EOL> import logging <EOL> import itertools <EOL> from . patcomp import PatternCompiler <EOL> from . import pygram <EOL> from . fixer_util import does_tree_import <EOL> class BaseFix ( object ) : <EOL> """<STR_LIT>""" <EOL> PATTERN = None <EOL> pattern = None <EOL> pattern_tree = None <EOL> options = None <EOL> filename = None <EOL> logger = None <EOL> numbers = itertools . count ( <NUM_LIT:1> ) <EOL> used_names = set ( ) <EOL> order = "<STR_LIT>" <EOL> explicit = False <EOL> run_order = <NUM_LIT:5> <EOL> _accept_type = None <EOL> keep_line_order = False <EOL> BM_compatible = False <EOL> syms = pygram . python_symbols <EOL> def __init__ ( self , options , log ) : <EOL> """<STR_LIT>""" <EOL> self . options = options <EOL> self . log = log <EOL> self . compile_pattern ( ) <EOL> def compile_pattern ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . PATTERN is not None : <EOL> PC = PatternCompiler ( ) <EOL> self . pattern , self . pattern_tree = PC . compile_pattern ( self . PATTERN , <EOL> with_tree = True ) <EOL> def set_filename ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . filename = filename <EOL> self . logger = logging . getLogger ( filename ) <EOL> def match ( self , node ) : <EOL> """<STR_LIT>""" <EOL> results = { "<STR_LIT>" : node } <EOL> return self . pattern . match ( node , results ) and results <EOL> def transform ( self , node , results ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def new_name ( self , template = u"<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> name = template <EOL> while name in self . used_names : <EOL> name = template + unicode ( self . numbers . next ( ) ) <EOL> self . used_names . add ( name ) <EOL> return name <EOL> def log_message ( self , message ) : <EOL> if self . first_log : <EOL> self . first_log = False <EOL> self . log . append ( "<STR_LIT>" % self . filename ) <EOL> self . log . append ( message ) <EOL> def cannot_convert ( self , node , reason = None ) : <EOL> """<STR_LIT>""" <EOL> lineno = node . get_lineno ( ) <EOL> for_output = node . clone ( ) <EOL> for_output . prefix = u"<STR_LIT>" <EOL> msg = "<STR_LIT>" <EOL> self . log_message ( msg % ( lineno , for_output ) ) <EOL> if reason : <EOL> self . log_message ( reason ) <EOL> def warning ( self , node , reason ) : <EOL> """<STR_LIT>""" <EOL> lineno = node . get_lineno ( ) <EOL> self . log_message ( "<STR_LIT>" % ( lineno , reason ) ) <EOL> def start_tree ( self , tree , filename ) : <EOL> """<STR_LIT>""" <EOL> self . used_names = tree . used_names <EOL> self . set_filename ( filename ) <EOL> self . numbers = itertools . count ( <NUM_LIT:1> ) <EOL> self . first_log = True <EOL> def finish_tree ( self , tree , filename ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ConditionalFix ( BaseFix ) : <EOL> """<STR_LIT>""" <EOL> skip_on = None <EOL> def start_tree ( self , * args ) : <EOL> super ( ConditionalFix , self ) . start_tree ( * args ) <EOL> self . _should_skip = None <EOL> def should_skip ( self , node ) : <EOL> if self . _should_skip is not None : <EOL> return self . _should_skip <EOL> pkg = self . skip_on . split ( "<STR_LIT:.>" ) <EOL> name = pkg [ - <NUM_LIT:1> ] <EOL> pkg = "<STR_LIT:.>" . join ( pkg [ : - <NUM_LIT:1> ] ) <EOL> self . _should_skip = does_tree_import ( pkg , name , node ) <EOL> return self . _should_skip </s>
