<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import os <EOL> import sys <EOL> import getopt <EOL> import tokenize <EOL> if not hasattr ( tokenize , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> verbose = <NUM_LIT:0> <EOL> filename_only = <NUM_LIT:0> <EOL> def errprint ( * args ) : <EOL> sep = "<STR_LIT>" <EOL> for arg in args : <EOL> sys . stderr . write ( sep + str ( arg ) ) <EOL> sep = "<STR_LIT:U+0020>" <EOL> sys . stderr . write ( "<STR_LIT:\n>" ) <EOL> def main ( ) : <EOL> global verbose , filename_only <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" ) <EOL> except getopt . error , msg : <EOL> errprint ( msg ) <EOL> return <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> filename_only = filename_only + <NUM_LIT:1> <EOL> if o == '<STR_LIT>' : <EOL> verbose = verbose + <NUM_LIT:1> <EOL> if not args : <EOL> errprint ( "<STR_LIT>" , sys . argv [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> return <EOL> for arg in args : <EOL> check ( arg ) <EOL> class NannyNag ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lineno , msg , line ) : <EOL> self . lineno , self . msg , self . line = lineno , msg , line <EOL> def get_lineno ( self ) : <EOL> return self . lineno <EOL> def get_msg ( self ) : <EOL> return self . msg <EOL> def get_line ( self ) : <EOL> return self . line <EOL> def check ( file ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isdir ( file ) and not os . path . islink ( file ) : <EOL> if verbose : <EOL> print "<STR_LIT>" % ( file , ) <EOL> names = os . listdir ( file ) <EOL> for name in names : <EOL> fullname = os . path . join ( file , name ) <EOL> if ( os . path . isdir ( fullname ) and <EOL> not os . path . islink ( fullname ) or <EOL> os . path . normcase ( name [ - <NUM_LIT:3> : ] ) == "<STR_LIT>" ) : <EOL> check ( fullname ) <EOL> return <EOL> try : <EOL> f = open ( file ) <EOL> except IOError , msg : <EOL> errprint ( "<STR_LIT>" % ( file , msg ) ) <EOL> return <EOL> if verbose > <NUM_LIT:1> : <EOL> print "<STR_LIT>" % file <EOL> try : <EOL> process_tokens ( tokenize . generate_tokens ( f . readline ) ) <EOL> except tokenize . TokenError , msg : <EOL> errprint ( "<STR_LIT>" % ( file , msg ) ) <EOL> return <EOL> except IndentationError , msg : <EOL> errprint ( "<STR_LIT>" % ( file , msg ) ) <EOL> return <EOL> except NannyNag , nag : <EOL> badline = nag . get_lineno ( ) <EOL> line = nag . get_line ( ) <EOL> if verbose : <EOL> print "<STR_LIT>" % ( file , badline ) <EOL> print "<STR_LIT>" % ( line , ) <EOL> print nag . get_msg ( ) <EOL> else : <EOL> if '<STR_LIT:U+0020>' in file : file = '<STR_LIT:">' + file + '<STR_LIT:">' <EOL> if filename_only : print file <EOL> else : print file , badline , repr ( line ) <EOL> return <EOL> if verbose : <EOL> print "<STR_LIT>" % ( file , ) <EOL> class Whitespace : <EOL> S , T = '<STR_LIT>' <EOL> def __init__ ( self , ws ) : <EOL> self . raw = ws <EOL> S , T = Whitespace . S , Whitespace . T <EOL> count = [ ] <EOL> b = n = nt = <NUM_LIT:0> <EOL> for ch in self . raw : <EOL> if ch == S : <EOL> n = n + <NUM_LIT:1> <EOL> b = b + <NUM_LIT:1> <EOL> elif ch == T : <EOL> n = n + <NUM_LIT:1> <EOL> nt = nt + <NUM_LIT:1> <EOL> if b >= len ( count ) : <EOL> count = count + [ <NUM_LIT:0> ] * ( b - len ( count ) + <NUM_LIT:1> ) <EOL> count [ b ] = count [ b ] + <NUM_LIT:1> <EOL> b = <NUM_LIT:0> <EOL> else : <EOL> break <EOL> self . n = n <EOL> self . nt = nt <EOL> self . norm = tuple ( count ) , b <EOL> self . is_simple = len ( count ) <= <NUM_LIT:1> <EOL> def longest_run_of_spaces ( self ) : <EOL> count , trailing = self . norm <EOL> return max ( len ( count ) - <NUM_LIT:1> , trailing ) <EOL> def indent_level ( self , tabsize ) : <EOL> count , trailing = self . norm <EOL> il = <NUM_LIT:0> <EOL> for i in range ( tabsize , len ( count ) ) : <EOL> il = il + i / tabsize * count [ i ] <EOL> return trailing + tabsize * ( il + self . nt ) <EOL> def equal ( self , other ) : <EOL> return self . norm == other . norm <EOL> def not_equal_witness ( self , other ) : <EOL> n = max ( self . longest_run_of_spaces ( ) , <EOL> other . longest_run_of_spaces ( ) ) + <NUM_LIT:1> <EOL> a = [ ] <EOL> for ts in range ( <NUM_LIT:1> , n + <NUM_LIT:1> ) : <EOL> if self . indent_level ( ts ) != other . indent_level ( ts ) : <EOL> a . append ( ( ts , <EOL> self . indent_level ( ts ) , <EOL> other . indent_level ( ts ) ) ) <EOL> return a <EOL> def less ( self , other ) : <EOL> if self . n >= other . n : <EOL> return False <EOL> if self . is_simple and other . is_simple : <EOL> return self . nt <= other . nt <EOL> n = max ( self . longest_run_of_spaces ( ) , <EOL> other . longest_run_of_spaces ( ) ) + <NUM_LIT:1> <EOL> for ts in range ( <NUM_LIT:2> , n + <NUM_LIT:1> ) : <EOL> if self . indent_level ( ts ) >= other . indent_level ( ts ) : <EOL> return False <EOL> return True <EOL> def not_less_witness ( self , other ) : <EOL> n = max ( self . longest_run_of_spaces ( ) , <EOL> other . longest_run_of_spaces ( ) ) + <NUM_LIT:1> <EOL> a = [ ] <EOL> for ts in range ( <NUM_LIT:1> , n + <NUM_LIT:1> ) : <EOL> if self . indent_level ( ts ) >= other . indent_level ( ts ) : <EOL> a . append ( ( ts , <EOL> self . indent_level ( ts ) , <EOL> other . indent_level ( ts ) ) ) <EOL> return a <EOL> def format_witnesses ( w ) : <EOL> firsts = map ( lambda tup : str ( tup [ <NUM_LIT:0> ] ) , w ) <EOL> prefix = "<STR_LIT>" <EOL> if len ( w ) > <NUM_LIT:1> : <EOL> prefix = prefix + "<STR_LIT:s>" <EOL> return prefix + "<STR_LIT:U+0020>" + '<STR_LIT:U+002CU+0020>' . join ( firsts ) <EOL> def process_tokens ( tokens ) : <EOL> INDENT = tokenize . INDENT <EOL> DEDENT = tokenize . DEDENT <EOL> NEWLINE = tokenize . NEWLINE <EOL> JUNK = tokenize . COMMENT , tokenize . NL <EOL> indents = [ Whitespace ( "<STR_LIT>" ) ] <EOL> check_equal = <NUM_LIT:0> <EOL> for ( type , token , start , end , line ) in tokens : <EOL> if type == NEWLINE : <EOL> check_equal = <NUM_LIT:1> <EOL> elif type == INDENT : <EOL> check_equal = <NUM_LIT:0> <EOL> thisguy = Whitespace ( token ) <EOL> if not indents [ - <NUM_LIT:1> ] . less ( thisguy ) : <EOL> witness = indents [ - <NUM_LIT:1> ] . not_less_witness ( thisguy ) <EOL> msg = "<STR_LIT>" + format_witnesses ( witness ) <EOL> raise NannyNag ( start [ <NUM_LIT:0> ] , msg , line ) <EOL> indents . append ( thisguy ) <EOL> elif type == DEDENT : <EOL> check_equal = <NUM_LIT:1> <EOL> del indents [ - <NUM_LIT:1> ] <EOL> elif check_equal and type not in JUNK : <EOL> check_equal = <NUM_LIT:0> <EOL> thisguy = Whitespace ( line ) <EOL> if not indents [ - <NUM_LIT:1> ] . equal ( thisguy ) : <EOL> witness = indents [ - <NUM_LIT:1> ] . not_equal_witness ( thisguy ) <EOL> msg = "<STR_LIT>" + format_witnesses ( witness ) <EOL> raise NannyNag ( start [ <NUM_LIT:0> ] , msg , line ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
