<s> """<STR_LIT>""" <EOL> import sys , os <EOL> __all__ = [ "<STR_LIT:input>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:filename>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _state = None <EOL> DEFAULT_BUFSIZE = <NUM_LIT:8> * <NUM_LIT> <EOL> def input ( files = None , inplace = <NUM_LIT:0> , backup = "<STR_LIT>" , bufsize = <NUM_LIT:0> , <EOL> mode = "<STR_LIT:r>" , openhook = None ) : <EOL> """<STR_LIT>""" <EOL> global _state <EOL> if _state and _state . _file : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> _state = FileInput ( files , inplace , backup , bufsize , mode , openhook ) <EOL> return _state <EOL> def close ( ) : <EOL> """<STR_LIT>""" <EOL> global _state <EOL> state = _state <EOL> _state = None <EOL> if state : <EOL> state . close ( ) <EOL> def nextfile ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . nextfile ( ) <EOL> def filename ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . filename ( ) <EOL> def lineno ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . lineno ( ) <EOL> def filelineno ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . filelineno ( ) <EOL> def fileno ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . fileno ( ) <EOL> def isfirstline ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . isfirstline ( ) <EOL> def isstdin ( ) : <EOL> """<STR_LIT>""" <EOL> if not _state : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return _state . isstdin ( ) <EOL> class FileInput : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , files = None , inplace = <NUM_LIT:0> , backup = "<STR_LIT>" , bufsize = <NUM_LIT:0> , <EOL> mode = "<STR_LIT:r>" , openhook = None ) : <EOL> if isinstance ( files , basestring ) : <EOL> files = ( files , ) <EOL> else : <EOL> if files is None : <EOL> files = sys . argv [ <NUM_LIT:1> : ] <EOL> if not files : <EOL> files = ( '<STR_LIT:->' , ) <EOL> else : <EOL> files = tuple ( files ) <EOL> self . _files = files <EOL> self . _inplace = inplace <EOL> self . _backup = backup <EOL> self . _bufsize = bufsize or DEFAULT_BUFSIZE <EOL> self . _savestdout = None <EOL> self . _output = None <EOL> self . _filename = None <EOL> self . _lineno = <NUM_LIT:0> <EOL> self . _filelineno = <NUM_LIT:0> <EOL> self . _file = None <EOL> self . _isstdin = False <EOL> self . _backupfilename = None <EOL> self . _buffer = [ ] <EOL> self . _bufindex = <NUM_LIT:0> <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:rb>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _mode = mode <EOL> if inplace and openhook : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif openhook and not hasattr ( openhook , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _openhook = openhook <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> self . nextfile ( ) <EOL> self . _files = ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> try : <EOL> line = self . _buffer [ self . _bufindex ] <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> self . _bufindex += <NUM_LIT:1> <EOL> self . _lineno += <NUM_LIT:1> <EOL> self . _filelineno += <NUM_LIT:1> <EOL> return line <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise StopIteration <EOL> return line <EOL> def __getitem__ ( self , i ) : <EOL> if i != self . _lineno : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> try : <EOL> return self . next ( ) <EOL> except StopIteration : <EOL> raise IndexError , "<STR_LIT>" <EOL> def nextfile ( self ) : <EOL> savestdout = self . _savestdout <EOL> self . _savestdout = <NUM_LIT:0> <EOL> if savestdout : <EOL> sys . stdout = savestdout <EOL> output = self . _output <EOL> self . _output = <NUM_LIT:0> <EOL> if output : <EOL> output . close ( ) <EOL> file = self . _file <EOL> self . _file = <NUM_LIT:0> <EOL> if file and not self . _isstdin : <EOL> file . close ( ) <EOL> backupfilename = self . _backupfilename <EOL> self . _backupfilename = <NUM_LIT:0> <EOL> if backupfilename and not self . _backup : <EOL> try : os . unlink ( backupfilename ) <EOL> except OSError : pass <EOL> self . _isstdin = False <EOL> self . _buffer = [ ] <EOL> self . _bufindex = <NUM_LIT:0> <EOL> def readline ( self ) : <EOL> try : <EOL> line = self . _buffer [ self . _bufindex ] <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> self . _bufindex += <NUM_LIT:1> <EOL> self . _lineno += <NUM_LIT:1> <EOL> self . _filelineno += <NUM_LIT:1> <EOL> return line <EOL> if not self . _file : <EOL> if not self . _files : <EOL> return "<STR_LIT>" <EOL> self . _filename = self . _files [ <NUM_LIT:0> ] <EOL> self . _files = self . _files [ <NUM_LIT:1> : ] <EOL> self . _filelineno = <NUM_LIT:0> <EOL> self . _file = None <EOL> self . _isstdin = False <EOL> self . _backupfilename = <NUM_LIT:0> <EOL> if self . _filename == '<STR_LIT:->' : <EOL> self . _filename = '<STR_LIT>' <EOL> self . _file = sys . stdin <EOL> self . _isstdin = True <EOL> else : <EOL> if self . _inplace : <EOL> self . _backupfilename = ( <EOL> self . _filename + ( self . _backup or os . extsep + "<STR_LIT>" ) ) <EOL> try : os . unlink ( self . _backupfilename ) <EOL> except os . error : pass <EOL> os . rename ( self . _filename , self . _backupfilename ) <EOL> self . _file = open ( self . _backupfilename , self . _mode ) <EOL> try : <EOL> perm = os . fstat ( self . _file . fileno ( ) ) . st_mode <EOL> except OSError : <EOL> self . _output = open ( self . _filename , "<STR_LIT:w>" ) <EOL> else : <EOL> fd = os . open ( self . _filename , <EOL> os . O_CREAT | os . O_WRONLY | os . O_TRUNC , <EOL> perm ) <EOL> self . _output = os . fdopen ( fd , "<STR_LIT:w>" ) <EOL> try : <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> os . chmod ( self . _filename , perm ) <EOL> except OSError : <EOL> pass <EOL> self . _savestdout = sys . stdout <EOL> sys . stdout = self . _output <EOL> else : <EOL> if self . _openhook : <EOL> self . _file = self . _openhook ( self . _filename , self . _mode ) <EOL> else : <EOL> self . _file = open ( self . _filename , self . _mode ) <EOL> self . _buffer = self . _file . readlines ( self . _bufsize ) <EOL> self . _bufindex = <NUM_LIT:0> <EOL> if not self . _buffer : <EOL> self . nextfile ( ) <EOL> return self . readline ( ) <EOL> def filename ( self ) : <EOL> return self . _filename <EOL> def lineno ( self ) : <EOL> return self . _lineno <EOL> def filelineno ( self ) : <EOL> return self . _filelineno <EOL> def fileno ( self ) : <EOL> if self . _file : <EOL> try : <EOL> return self . _file . fileno ( ) <EOL> except ValueError : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def isfirstline ( self ) : <EOL> return self . _filelineno == <NUM_LIT:1> <EOL> def isstdin ( self ) : <EOL> return self . _isstdin <EOL> def hook_compressed ( filename , mode ) : <EOL> ext = os . path . splitext ( filename ) [ <NUM_LIT:1> ] <EOL> if ext == '<STR_LIT>' : <EOL> import gzip <EOL> return gzip . open ( filename , mode ) <EOL> elif ext == '<STR_LIT>' : <EOL> import bz2 <EOL> return bz2 . BZ2File ( filename , mode ) <EOL> else : <EOL> return open ( filename , mode ) <EOL> def hook_encoded ( encoding ) : <EOL> import io <EOL> def openhook ( filename , mode ) : <EOL> mode = mode . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT:b>' , '<STR_LIT>' ) or '<STR_LIT:r>' <EOL> return io . open ( filename , mode , encoding = encoding , newline = '<STR_LIT>' ) <EOL> return openhook <EOL> def _test ( ) : <EOL> import getopt <EOL> inplace = <NUM_LIT:0> <EOL> backup = <NUM_LIT:0> <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" ) <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : inplace = <NUM_LIT:1> <EOL> if o == '<STR_LIT>' : backup = a <EOL> for line in input ( args , inplace = inplace , backup = backup ) : <EOL> if line [ - <NUM_LIT:1> : ] == '<STR_LIT:\n>' : line = line [ : - <NUM_LIT:1> ] <EOL> if line [ - <NUM_LIT:1> : ] == '<STR_LIT:\r>' : line = line [ : - <NUM_LIT:1> ] <EOL> print "<STR_LIT>" % ( lineno ( ) , filename ( ) , filelineno ( ) , <EOL> isfirstline ( ) and "<STR_LIT:*>" or "<STR_LIT>" , line ) <EOL> print "<STR_LIT>" % ( lineno ( ) , filename ( ) , filelineno ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
