f7c @sdZddlZddlZddddddd d d g Zdad"Zdd#ddddddZddZ ddZ ddZ ddZ ddZ ddZdd Zdd ZGdd d ZddZddZdd Zed!krendS($u5Helper class to quickly write a loop over all standard input files. Typical use is: import fileinput for line in fileinput.input(): process(line) This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-' it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the argument to input(). A single file name is also allowed. Functions filename(), lineno() return the filename and cumulative line number of the line that has just been read; filelineno() returns its line number in the current file; isfirstline() returns true iff the line just read is the first line of its file; isstdin() returns true iff the line was read from sys.stdin. Function nextfile() closes the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count; the filename is not changed until after the first line of the next file has been read. Function close() closes the sequence. Before any lines have been read, filename() returns None and both line numbers are zero; nextfile() has no effect. After all lines have been read, filename() and the line number functions return the values pertaining to the last line read; nextfile() has no effect. All files are opened in text mode by default, you can override this by setting the mode parameter to input() or FileInput.__init__(). If an I/O error occurs during opening or reading a file, the IOError exception is raised. If sys.stdin is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been explicitly reset (e.g. using sys.stdin.seek(0)). Empty files are opened and immediately closed; the only time their presence in the list of filenames is noticeable at all is when the last file opened is empty. It is possible that the last line of a file doesn't end in a newline character; otherwise lines are returned including the trailing newline. Class FileInput is the implementation; its methods filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() correspond to the functions in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly sequential order; sequence access and readline() cannot be mixed. Optional in-place filtering: if the keyword argument inplace=1 is passed to input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file. This makes it possible to write a filter that rewrites its input file in place. If the keyword argument backup="." is also given, it specifies the extension for the backup file, and the backup file remains around; by default, the extension is ".bak" and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read. XXX The current implementation does not work for MS-DOS 8+3 filesystems. Performance: this module is unfortunately one of the slower ways of processing large numbers of input lines. Nevertheless, a significant speed-up has been obtained by using readlines(bufsize) instead of readline(). A new keyword argument, bufsize=N, is present on the input() function and the FileInput() class to override the default buffer size. XXX Possible additions: - optional getopt argument processing - isatty() - read(), read(size), even readlines() iNuinputucloseunextfileufilenameulinenou filelinenou isfirstlineuisstdinu FileInputiiuurcCs=trtjrtdnt||||||atS(uReturn an instance of the FileInput class, which can be iterated. The parameters are passed to the constructor of the FileInput class. The returned instance, in addition to being an iterator, keeps global state for the functions of this module,. uinput() already active(u_stateu_fileu RuntimeErroru FileInput(ufilesuinplaceubackupubufsizeumodeuopenhook((u./opt/alt/python33/lib64/python3.3/fileinput.pyuinput[s cCs#t}da|r|jndS(uClose the sequence.N(u_stateuNoneuclose(ustate((u./opt/alt/python33/lib64/python3.3/fileinput.pyucloseiscCststdntjS(u Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect. uno active input()(u_stateu RuntimeErrorunextfile(((u./opt/alt/python33/lib64/python3.3/fileinput.pyunextfileqs cCststdntjS(ur Return the name of the file currently being read. Before the first line has been read, returns None. uno active input()(u_stateu RuntimeErrorufilename(((u./opt/alt/python33/lib64/python3.3/fileinput.pyufilenamescCststdntjS(u Return the cumulative line number of the line that has just been read. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line. uno active input()(u_stateu RuntimeErrorulineno(((u./opt/alt/python33/lib64/python3.3/fileinput.pyulinenoscCststdntjS(u Return the line number in the current file. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line within the file. uno active input()(u_stateu RuntimeErroru filelineno(((u./opt/alt/python33/lib64/python3.3/fileinput.pyu filelinenoscCststdntjS(ug Return the file number of the current file. When no file is currently opened, returns -1. uno active input()(u_stateu RuntimeErrorufileno(((u./opt/alt/python33/lib64/python3.3/fileinput.pyufilenosufilenocCststdntjS(ue Returns true the line just read is the first line of its file, otherwise returns false. uno active input()(u_stateu RuntimeErroru isfirstline(((u./opt/alt/python33/lib64/python3.3/fileinput.pyu isfirstlinescCststdntjS(u] Returns true if the last line was read from sys.stdin, otherwise returns false. uno active input()(u_stateu RuntimeErroruisstdin(((u./opt/alt/python33/lib64/python3.3/fileinput.pyuisstdinscBs|EeZdZdZd%d&dddd%ddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZddZddZddZdd Zd!d"Zd#d$Zd%S('u FileInputu?FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]]) Class FileInput is the implementation of the module; its methods filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), nextfile() and close() correspond to the functions of the same name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed. uiurcCsFt|tr|f}n=|dkr=tjdd}n|sLd }n t|}||_||_||_|p|t |_ d|_ d|_ d|_ d|_d|_d|_d |_d|_g|_d|_|d krtdn||_|r9|rtd nt|s9td q9n||_dS(Niu-iururUuUurbu=FileInput opening mode must be one of 'r', 'rU', 'U' and 'rb'u4FileInput cannot use an opening hook in inplace modeu#FileInput openhook must be callable(u-F(ururUuUurb(u isinstanceustruNoneusysuargvutupleu_filesu_inplaceu_backupuDEFAULT_BUFSIZEu_bufsizeu _savestdoutu_outputu _filenameu_linenou _filelinenou_fileuFalseu_isstdinu_backupfilenameu_bufferu _bufindexu ValueErroru_modeucallableu _openhook(uselfufilesuinplaceubackupubufsizeumodeuopenhook((u./opt/alt/python33/lib64/python3.3/fileinput.pyu__init__s<                    uFileInput.__init__cCs|jdS(N(uclose(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyu__del__suFileInput.__del__cCs|jf|_dS(N(unextfileu_files(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyucloses uFileInput.closecCs|S(N((uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyu __enter__suFileInput.__enter__cCs|jdS(N(uclose(uselfutypeuvalueu traceback((u./opt/alt/python33/lib64/python3.3/fileinput.pyu__exit__suFileInput.__exit__cCs|S(N((uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyu__iter__suFileInput.__iter__c Csyy|j|j}Wntk r(Yn2X|jd7_|jd7_|jd7_|S|j}|sutn|S(Ni(u_bufferu _bufindexu IndexErroru_linenou _filelinenoureadlineu StopIteration(uselfuline((u./opt/alt/python33/lib64/python3.3/fileinput.pyu__next__s   uFileInput.__next__c CsQ||jkrtdny|jSWntk rLtdYnXdS(Nuaccessing lines out of orderuend of input reached(u_linenou RuntimeErroru__next__u StopIterationu IndexError(uselfui((u./opt/alt/python33/lib64/python3.3/fileinput.pyu __getitem__s  uFileInput.__getitem__c Cs|j}d|_|r$|t_n|j}d|_|rI|jn|j}d|_|rx|j rx|jn|j}d|_|r|j ryt j |Wqt k rYqXnd|_g|_ d|_dS(NiF(u _savestdoutusysustdoutu_outputucloseu_fileu_isstdinu_backupfilenameu_backupuosuunlinkuOSErroruFalseu_bufferu _bufindex(uselfu savestdoutuoutputufileubackupfilename((u./opt/alt/python33/lib64/python3.3/fileinput.pyunextfiles,              uFileInput.nextfilec$Csy|j|j}Wntk r(Yn2X|jd7_|jd7_|jd7_|S|js|jspdS|jd|_|jdd|_d|_d|_d |_ d|_ |jdkrd|_t j |_d |_ q|jrk|j|jpd|_ ytj|j Wntjk r7YnXtj|j|j t|j |j|_ytj|jjj}Wn'tk rt|jd|_YnXtjtjBtjB}ttdr|tj O}ntj|j||}tj!|d|_y)ttd r:tj"|j|nWntk rOYnXt j#|_$|jt _#q|j%r|j%|j|j|_qt|j|j|_n|jj&|j'|_d|_|js|j(n|j)S( Niuiu-uu.bakuwuO_BINARYuchmodFT(*u_bufferu _bufindexu IndexErroru_linenou _filelinenou_fileu_filesu _filenameuNoneuFalseu_isstdinu_backupfilenameusysustdinuTrueu_inplaceu_backupuosuunlinkuerrorurenameuopenu_modeufstatufilenoust_modeuOSErroru_outputuO_CREATuO_WRONLYuO_TRUNCuhasattruO_BINARYufdopenuchmodustdoutu _savestdoutu _openhooku readlinesu_bufsizeunextfileureadline(uselfulineupermumodeufd((u./opt/alt/python33/lib64/python3.3/fileinput.pyureadline*sj                  uFileInput.readlinecCs|jS(N(u _filename(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyufilenameisuFileInput.filenamecCs|jS(N(u_lineno(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyulinenolsuFileInput.linenocCs|jS(N(u _filelineno(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyu filelinenoosuFileInput.filelinenoc Cs>|jr6y|jjSWq:tk r2dSYq:XndSdS(Niii(u_fileufilenou ValueError(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyufilenors    uFileInput.filenocCs |jdkS(Ni(u _filelineno(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyu isfirstline{suFileInput.isfirstlinecCs|jS(N(u_isstdin(uself((u./opt/alt/python33/lib64/python3.3/fileinput.pyuisstdin~suFileInput.isstdinNF(u__name__u __module__u __qualname__u__doc__uNoneuFalseu__init__u__del__ucloseu __enter__u__exit__u__iter__u__next__u __getitem__unextfileureadlineufilenameulinenou filelinenoufilenou isfirstlineuisstdin(u __locals__((u./opt/alt/python33/lib64/python3.3/fileinput.pyu FileInputs$  $         ?    cCswtjj|d}|dkr>ddl}|j||S|dkrfddl}|j||St||SdS(Niu.gziu.bz2(uosupathusplitextugzipuopenubz2uBZ2File(ufilenameumodeuextugzipubz2((u./opt/alt/python33/lib64/python3.3/fileinput.pyuhook_compresseds    uhook_compressedcsfdd}|S(Ncst||dS(Nuencoding(uopen(ufilenameumode(uencoding(u./opt/alt/python33/lib64/python3.3/fileinput.pyuopenhooksuhook_encoded..openhook((uencodinguopenhook((uencodingu./opt/alt/python33/lib64/python3.3/fileinput.pyu hook_encodedsu hook_encodedc CsLddl}d}d}|jtjddd\}}x>|D]6\}}|dkred}n|dkrD|}qDqDWxt|d|d|D]}|dddkr|dd}n|ddd kr|dd}ntd ttt t rd pd |fqWtd ttt fdS(Niiuib:u-iu-buinplaceubackupu u u%d: %s[%d]%s %su*uu %d: %s[%d]FTiiii( ugetoptuFalseusysuargvuTrueuinputuprintulinenoufilenameu filelinenou isfirstline(ugetoptuinplaceubackupuoptsuargsuouauline((u./opt/alt/python33/lib64/python3.3/fileinput.pyu_tests" %  !u_testu__main__i F(u__doc__usysuosu__all__uNoneu_stateuDEFAULT_BUFSIZEuFalseuinputucloseunextfileufilenameulinenou filelinenoufilenou isfirstlineuisstdinu FileInputuhook_compressedu hook_encodedu_testu__name__(((u./opt/alt/python33/lib64/python3.3/fileinput.pyuPs*