<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import argparse <EOL> import collections <EOL> expected_pragma = b'<STR_LIT>' <EOL> def has_coding ( line ) : <EOL> if not line . strip ( ) : <EOL> return False <EOL> return ( <EOL> line . lstrip ( ) [ <NUM_LIT:0> : <NUM_LIT:1> ] == b'<STR_LIT:#>' and ( <EOL> b'<STR_LIT>' in line or <EOL> b'<STR_LIT>' in line or <EOL> b'<STR_LIT>' in line or <EOL> b'<STR_LIT>' in line <EOL> ) <EOL> ) <EOL> class ExpectedContents ( collections . namedtuple ( <EOL> '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> @ property <EOL> def has_any_pragma ( self ) : <EOL> return self . pragma_status is not False <EOL> def is_expected_pragma ( self , remove ) : <EOL> expected_pragma_status = not remove <EOL> return self . pragma_status is expected_pragma_status <EOL> def _get_expected_contents ( first_line , second_line , rest ) : <EOL> if first_line . startswith ( b'<STR_LIT>' ) : <EOL> shebang = first_line <EOL> potential_coding = second_line <EOL> else : <EOL> shebang = b'<STR_LIT>' <EOL> potential_coding = first_line <EOL> rest = second_line + rest <EOL> if potential_coding == expected_pragma : <EOL> pragma_status = True <EOL> elif has_coding ( potential_coding ) : <EOL> pragma_status = None <EOL> else : <EOL> pragma_status = False <EOL> rest = potential_coding + rest <EOL> return ExpectedContents ( <EOL> shebang = shebang , rest = rest , pragma_status = pragma_status , <EOL> ) <EOL> def fix_encoding_pragma ( f , remove = False ) : <EOL> expected = _get_expected_contents ( f . readline ( ) , f . readline ( ) , f . read ( ) ) <EOL> if not expected . rest . strip ( ) : <EOL> if expected . has_any_pragma or expected . shebang : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> f . truncate ( ) <EOL> f . write ( b'<STR_LIT>' ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> if expected . is_expected_pragma ( remove ) : <EOL> return <NUM_LIT:0> <EOL> f . seek ( <NUM_LIT:0> ) <EOL> f . truncate ( ) <EOL> f . write ( expected . shebang ) <EOL> if not remove : <EOL> f . write ( expected_pragma ) <EOL> f . write ( expected . rest ) <EOL> return <NUM_LIT:1> <EOL> def main ( argv = None ) : <EOL> parser = argparse . ArgumentParser ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> args = parser . parse_args ( argv ) <EOL> retv = <NUM_LIT:0> <EOL> if args . remove : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> for filename in args . filenames : <EOL> with open ( filename , '<STR_LIT>' ) as f : <EOL> file_ret = fix_encoding_pragma ( f , remove = args . remove ) <EOL> retv |= file_ret <EOL> if file_ret : <EOL> print ( fmt . format ( pragma = expected_pragma , filename = filename ) ) <EOL> return retv <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> exit ( main ( ) ) </s>
