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