Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 1 | """Helper class to quickly write a loop over all standard input files. |
| 2 | |
| 3 | Typical use is: |
| 4 | |
| 5 | import fileinput |
| 6 | for line in fileinput.input(): |
| 7 | process(line) |
| 8 | |
| 9 | This iterates over the lines of all files listed in sys.argv[1:], |
| 10 | defaulting to sys.stdin if the list is empty. If a filename is '-' it |
| 11 | is also replaced by sys.stdin. To specify an alternative list of |
| 12 | filenames, pass it as the argument to input(). A single file name is |
| 13 | also allowed. |
| 14 | |
| 15 | Functions filename(), lineno() return the filename and cumulative line |
| 16 | number of the line that has just been read; filelineno() returns its |
| 17 | line number in the current file; isfirstline() returns true iff the |
| 18 | line just read is the first line of its file; isstdin() returns true |
| 19 | iff the line was read from sys.stdin. Function nextfile() closes the |
| 20 | current file so that the next iteration will read the first line from |
| 21 | the next file (if any); lines not read from the file will not count |
| 22 | towards the cumulative line count; the filename is not changed until |
| 23 | after the first line of the next file has been read. Function close() |
| 24 | closes the sequence. |
| 25 | |
| 26 | Before any lines have been read, filename() returns None and both line |
| 27 | numbers are zero; nextfile() has no effect. After all lines have been |
| 28 | read, filename() and the line number functions return the values |
| 29 | pertaining to the last line read; nextfile() has no effect. |
| 30 | |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 31 | All files are opened in text mode by default, you can override this by |
| 32 | setting the mode parameter to input() or FileInput.__init__(). |
| 33 | If an I/O error occurs during opening or reading a file, the IOError |
| 34 | exception is raised. |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 35 | |
| 36 | If sys.stdin is used more than once, the second and further use will |
| 37 | return no lines, except perhaps for interactive use, or if it has been |
| 38 | explicitly reset (e.g. using sys.stdin.seek(0)). |
| 39 | |
| 40 | Empty files are opened and immediately closed; the only time their |
| 41 | presence in the list of filenames is noticeable at all is when the |
| 42 | last file opened is empty. |
| 43 | |
| 44 | It is possible that the last line of a file doesn't end in a newline |
| 45 | character; otherwise lines are returned including the trailing |
| 46 | newline. |
| 47 | |
| 48 | Class FileInput is the implementation; its methods filename(), |
| 49 | lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() |
| 50 | correspond to the functions in the module. In addition it has a |
| 51 | readline() method which returns the next input line, and a |
| 52 | __getitem__() method which implements the sequence behavior. The |
| 53 | sequence must be accessed in strictly sequential order; sequence |
| 54 | access and readline() cannot be mixed. |
| 55 | |
| 56 | Optional in-place filtering: if the keyword argument inplace=1 is |
| 57 | passed to input() or to the FileInput constructor, the file is moved |
| 58 | to a backup file and standard output is directed to the input file. |
| 59 | This makes it possible to write a filter that rewrites its input file |
| 60 | in place. If the keyword argument backup=".<some extension>" is also |
| 61 | given, it specifies the extension for the backup file, and the backup |
| 62 | file remains around; by default, the extension is ".bak" and it is |
| 63 | deleted when the output file is closed. In-place filtering is |
| 64 | disabled when standard input is read. XXX The current implementation |
| 65 | does not work for MS-DOS 8+3 filesystems. |
| 66 | |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 67 | Performance: this module is unfortunately one of the slower ways of |
| 68 | processing large numbers of input lines. Nevertheless, a significant |
| 69 | speed-up has been obtained by using readlines(bufsize) instead of |
| 70 | readline(). A new keyword argument, bufsize=N, is present on the |
| 71 | input() function and the FileInput() class to override the default |
| 72 | buffer size. |
| 73 | |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 74 | XXX Possible additions: |
| 75 | |
| 76 | - optional getopt argument processing |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 77 | - isatty() |
| 78 | - read(), read(size), even readlines() |
| 79 | |
| 80 | """ |
| 81 | |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 82 | import sys, os |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 83 | |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 84 | __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno", |
| 85 | "isfirstline", "isstdin", "FileInput"] |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 87 | _state = None |
| 88 | |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 89 | DEFAULT_BUFSIZE = 8*1024 |
| 90 | |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 91 | def input(files=None, inplace=False, backup="", bufsize=0, |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 92 | mode="r", openhook=None): |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 93 | """input(files=None, inplace=False, backup="", bufsize=0, \ |
| 94 | mode="r", openhook=None) |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 95 | |
| 96 | Create an instance of the FileInput class. The instance will be used |
| 97 | as global state for the functions of this module, and is also returned |
| 98 | to use during iteration. The parameters to this function will be passed |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 99 | along to the constructor of the FileInput class. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 100 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 101 | global _state |
| 102 | if _state and _state._file: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 103 | raise RuntimeError("input() already active") |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 104 | _state = FileInput(files, inplace, backup, bufsize, mode, openhook) |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 105 | return _state |
| 106 | |
| 107 | def close(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 108 | """Close the sequence.""" |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 109 | global _state |
| 110 | state = _state |
| 111 | _state = None |
| 112 | if state: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | state.close() |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 114 | |
| 115 | def nextfile(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 116 | """ |
| 117 | Close the current file so that the next iteration will read the first |
| 118 | line from the next file (if any); lines not read from the file will |
| 119 | not count towards the cumulative line count. The filename is not |
| 120 | changed until after the first line of the next file has been read. |
| 121 | Before the first line has been read, this function has no effect; |
| 122 | it cannot be used to skip the first file. After the last line of the |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 123 | last file has been read, this function has no effect. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 124 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 125 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 126 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 127 | return _state.nextfile() |
| 128 | |
| 129 | def filename(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 130 | """ |
| 131 | Return the name of the file currently being read. |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 132 | Before the first line has been read, returns None. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 133 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 134 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 135 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 136 | return _state.filename() |
| 137 | |
| 138 | def lineno(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 139 | """ |
| 140 | Return the cumulative line number of the line that has just been read. |
| 141 | Before the first line has been read, returns 0. After the last line |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 142 | of the last file has been read, returns the line number of that line. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 143 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 144 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 145 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 146 | return _state.lineno() |
| 147 | |
| 148 | def filelineno(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 149 | """ |
| 150 | Return the line number in the current file. Before the first line |
| 151 | has been read, returns 0. After the last line of the last file has |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 152 | been read, returns the line number of that line within the file. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 153 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 154 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 155 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 156 | return _state.filelineno() |
| 157 | |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 158 | def fileno(): |
| 159 | """ |
| 160 | Return the file number of the current file. When no file is currently |
| 161 | opened, returns -1. |
| 162 | """ |
| 163 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 164 | raise RuntimeError("no active input()") |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 165 | return _state.fileno() |
| 166 | |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 167 | def isfirstline(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 168 | """ |
| 169 | Returns true the line just read is the first line of its file, |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 170 | otherwise returns false. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 171 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 172 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 173 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 174 | return _state.isfirstline() |
| 175 | |
| 176 | def isstdin(): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 177 | """ |
| 178 | Returns true if the last line was read from sys.stdin, |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 179 | otherwise returns false. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 180 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 181 | if not _state: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 182 | raise RuntimeError("no active input()") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 183 | return _state.isstdin() |
| 184 | |
| 185 | class FileInput: |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 186 | """class FileInput([files[, inplace[, backup[, mode[, openhook]]]]]) |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 187 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 188 | Class FileInput is the implementation of the module; its methods |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 189 | filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), |
| 190 | nextfile() and close() correspond to the functions of the same name |
| 191 | in the module. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 192 | In addition it has a readline() method which returns the next |
| 193 | input line, and a __getitem__() method which implements the |
| 194 | sequence behavior. The sequence must be accessed in strictly |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 195 | sequential order; random access and readline() cannot be mixed. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 196 | """ |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 197 | |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 198 | def __init__(self, files=None, inplace=False, backup="", bufsize=0, |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 199 | mode="r", openhook=None): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 200 | if isinstance(files, str): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | files = (files,) |
| 202 | else: |
Guido van Rossum | 2516b39 | 2000-04-10 17:16:12 +0000 | [diff] [blame] | 203 | if files is None: |
| 204 | files = sys.argv[1:] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 205 | if not files: |
Guido van Rossum | 2516b39 | 2000-04-10 17:16:12 +0000 | [diff] [blame] | 206 | files = ('-',) |
| 207 | else: |
| 208 | files = tuple(files) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 209 | self._files = files |
| 210 | self._inplace = inplace |
| 211 | self._backup = backup |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 212 | self._bufsize = bufsize or DEFAULT_BUFSIZE |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 213 | self._savestdout = None |
| 214 | self._output = None |
| 215 | self._filename = None |
| 216 | self._lineno = 0 |
| 217 | self._filelineno = 0 |
| 218 | self._file = None |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 219 | self._isstdin = False |
Guido van Rossum | 0aec9fb | 1998-07-20 15:49:28 +0000 | [diff] [blame] | 220 | self._backupfilename = None |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 221 | self._buffer = [] |
| 222 | self._bufindex = 0 |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 223 | # restrict mode argument to reading modes |
| 224 | if mode not in ('r', 'rU', 'U', 'rb'): |
| 225 | raise ValueError("FileInput opening mode must be one of " |
| 226 | "'r', 'rU', 'U' and 'rb'") |
| 227 | self._mode = mode |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 228 | if inplace and openhook: |
| 229 | raise ValueError("FileInput cannot use an opening hook in inplace mode") |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 230 | elif openhook and not hasattr(openhook, '__call__'): |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 231 | raise ValueError("FileInput openhook must be callable") |
| 232 | self._openhook = openhook |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 233 | |
| 234 | def __del__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 235 | self.close() |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 236 | |
| 237 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 238 | self.nextfile() |
| 239 | self._files = () |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 240 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame^] | 241 | def __enter__(self): |
| 242 | return self |
| 243 | |
| 244 | def __exit__(self, type, value, traceback): |
| 245 | self.close() |
| 246 | |
Neil Schemenauer | 908632a | 2002-03-26 20:28:40 +0000 | [diff] [blame] | 247 | def __iter__(self): |
| 248 | return self |
| 249 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 250 | def __next__(self): |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 251 | try: |
| 252 | line = self._buffer[self._bufindex] |
| 253 | except IndexError: |
| 254 | pass |
| 255 | else: |
| 256 | self._bufindex += 1 |
| 257 | self._lineno += 1 |
| 258 | self._filelineno += 1 |
| 259 | return line |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 260 | line = self.readline() |
| 261 | if not line: |
Neil Schemenauer | 908632a | 2002-03-26 20:28:40 +0000 | [diff] [blame] | 262 | raise StopIteration |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 263 | return line |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 264 | |
Neil Schemenauer | 908632a | 2002-03-26 20:28:40 +0000 | [diff] [blame] | 265 | def __getitem__(self, i): |
| 266 | if i != self._lineno: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 267 | raise RuntimeError("accessing lines out of order") |
Neil Schemenauer | 908632a | 2002-03-26 20:28:40 +0000 | [diff] [blame] | 268 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 269 | return self.__next__() |
Neil Schemenauer | 908632a | 2002-03-26 20:28:40 +0000 | [diff] [blame] | 270 | except StopIteration: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 271 | raise IndexError("end of input reached") |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 272 | |
| 273 | def nextfile(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 274 | savestdout = self._savestdout |
| 275 | self._savestdout = 0 |
| 276 | if savestdout: |
| 277 | sys.stdout = savestdout |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 279 | output = self._output |
| 280 | self._output = 0 |
| 281 | if output: |
| 282 | output.close() |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 284 | file = self._file |
| 285 | self._file = 0 |
| 286 | if file and not self._isstdin: |
| 287 | file.close() |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 289 | backupfilename = self._backupfilename |
| 290 | self._backupfilename = 0 |
| 291 | if backupfilename and not self._backup: |
| 292 | try: os.unlink(backupfilename) |
Skip Montanaro | cffac66 | 2002-08-14 02:58:16 +0000 | [diff] [blame] | 293 | except OSError: pass |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 295 | self._isstdin = False |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 296 | self._buffer = [] |
| 297 | self._bufindex = 0 |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 298 | |
| 299 | def readline(self): |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 300 | try: |
| 301 | line = self._buffer[self._bufindex] |
| 302 | except IndexError: |
| 303 | pass |
| 304 | else: |
| 305 | self._bufindex += 1 |
| 306 | self._lineno += 1 |
| 307 | self._filelineno += 1 |
| 308 | return line |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 309 | if not self._file: |
| 310 | if not self._files: |
| 311 | return "" |
| 312 | self._filename = self._files[0] |
| 313 | self._files = self._files[1:] |
| 314 | self._filelineno = 0 |
| 315 | self._file = None |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 316 | self._isstdin = False |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 317 | self._backupfilename = 0 |
| 318 | if self._filename == '-': |
| 319 | self._filename = '<stdin>' |
| 320 | self._file = sys.stdin |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 321 | self._isstdin = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 322 | else: |
| 323 | if self._inplace: |
| 324 | self._backupfilename = ( |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 325 | self._filename + (self._backup or ".bak")) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 326 | try: os.unlink(self._backupfilename) |
| 327 | except os.error: pass |
Guido van Rossum | dcb8583 | 1999-10-18 21:41:43 +0000 | [diff] [blame] | 328 | # The next few lines may raise IOError |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 329 | os.rename(self._filename, self._backupfilename) |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 330 | self._file = open(self._backupfilename, self._mode) |
Guido van Rossum | dcb8583 | 1999-10-18 21:41:43 +0000 | [diff] [blame] | 331 | try: |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 332 | perm = os.fstat(self._file.fileno()).st_mode |
Skip Montanaro | cffac66 | 2002-08-14 02:58:16 +0000 | [diff] [blame] | 333 | except OSError: |
Guido van Rossum | dcb8583 | 1999-10-18 21:41:43 +0000 | [diff] [blame] | 334 | self._output = open(self._filename, "w") |
| 335 | else: |
Guido van Rossum | 6203d8f | 2007-10-29 17:39:59 +0000 | [diff] [blame] | 336 | mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC |
| 337 | if hasattr(os, 'O_BINARY'): |
| 338 | mode |= os.O_BINARY |
| 339 | |
| 340 | fd = os.open(self._filename, mode, perm) |
Guido van Rossum | dcb8583 | 1999-10-18 21:41:43 +0000 | [diff] [blame] | 341 | self._output = os.fdopen(fd, "w") |
| 342 | try: |
Jack Jansen | 52941a8 | 2003-01-08 16:33:16 +0000 | [diff] [blame] | 343 | if hasattr(os, 'chmod'): |
| 344 | os.chmod(self._filename, perm) |
Skip Montanaro | cffac66 | 2002-08-14 02:58:16 +0000 | [diff] [blame] | 345 | except OSError: |
Guido van Rossum | dcb8583 | 1999-10-18 21:41:43 +0000 | [diff] [blame] | 346 | pass |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 347 | self._savestdout = sys.stdout |
| 348 | sys.stdout = self._output |
| 349 | else: |
| 350 | # This may raise IOError |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 351 | if self._openhook: |
| 352 | self._file = self._openhook(self._filename, self._mode) |
| 353 | else: |
| 354 | self._file = open(self._filename, self._mode) |
Guido van Rossum | 4795524 | 2001-01-05 14:44:39 +0000 | [diff] [blame] | 355 | self._buffer = self._file.readlines(self._bufsize) |
| 356 | self._bufindex = 0 |
| 357 | if not self._buffer: |
| 358 | self.nextfile() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 359 | # Recursive call |
| 360 | return self.readline() |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 361 | |
| 362 | def filename(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 363 | return self._filename |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 364 | |
| 365 | def lineno(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 366 | return self._lineno |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 367 | |
| 368 | def filelineno(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 369 | return self._filelineno |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 370 | |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 371 | def fileno(self): |
| 372 | if self._file: |
| 373 | try: |
| 374 | return self._file.fileno() |
| 375 | except ValueError: |
| 376 | return -1 |
| 377 | else: |
| 378 | return -1 |
| 379 | |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 380 | def isfirstline(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 381 | return self._filelineno == 1 |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 382 | |
| 383 | def isstdin(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 384 | return self._isstdin |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 385 | |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 386 | |
| 387 | def hook_compressed(filename, mode): |
| 388 | ext = os.path.splitext(filename)[1] |
| 389 | if ext == '.gz': |
| 390 | import gzip |
| 391 | return gzip.open(filename, mode) |
| 392 | elif ext == '.bz2': |
| 393 | import bz2 |
| 394 | return bz2.BZ2File(filename, mode) |
| 395 | else: |
| 396 | return open(filename, mode) |
| 397 | |
| 398 | |
| 399 | def hook_encoded(encoding): |
| 400 | import codecs |
| 401 | def openhook(filename, mode): |
| 402 | return codecs.open(filename, mode, encoding) |
| 403 | return openhook |
| 404 | |
| 405 | |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 406 | def _test(): |
| 407 | import getopt |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 408 | inplace = False |
| 409 | backup = False |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 410 | opts, args = getopt.getopt(sys.argv[1:], "ib:") |
| 411 | for o, a in opts: |
Georg Brandl | ef0a865 | 2009-05-17 12:22:57 +0000 | [diff] [blame] | 412 | if o == '-i': inplace = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 413 | if o == '-b': backup = a |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 414 | for line in input(args, inplace=inplace, backup=backup): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 415 | if line[-1:] == '\n': line = line[:-1] |
| 416 | if line[-1:] == '\r': line = line[:-1] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 417 | print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), |
| 418 | isfirstline() and "*" or "", line)) |
| 419 | print("%d: %s[%d]" % (lineno(), filename(), filelineno())) |
Guido van Rossum | 7d5b99d | 1997-11-21 17:12:59 +0000 | [diff] [blame] | 420 | |
| 421 | if __name__ == '__main__': |
| 422 | _test() |