annotate Lib/fileinput.py @ 12357:5b59ea41db4e legacy-trunk

Install the docs (with fewer rules). Add descriptions for the system variables.
author Guido van Rossum <guido@python.org>
date Mon, 10 Apr 2000 19:36:27 +0000
parents e2620aab9f2d
children 4dbbf322a9df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Helper class to quickly write a loop over all standard input files.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Typical use is:
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import fileinput
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 for line in fileinput.input():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 process(line)
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 This iterates over the lines of all files listed in sys.argv[1:],
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 defaulting to sys.stdin if the list is empty. If a filename is '-' it
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 is also replaced by sys.stdin. To specify an alternative list of
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 filenames, pass it as the argument to input(). A single file name is
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 also allowed.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 Functions filename(), lineno() return the filename and cumulative line
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 number of the line that has just been read; filelineno() returns its
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 line number in the current file; isfirstline() returns true iff the
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 line just read is the first line of its file; isstdin() returns true
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 iff the line was read from sys.stdin. Function nextfile() closes the
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 current file so that the next iteration will read the first line from
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 the next file (if any); lines not read from the file will not count
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 towards the cumulative line count; the filename is not changed until
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 after the first line of the next file has been read. Function close()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 closes the sequence.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 Before any lines have been read, filename() returns None and both line
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 numbers are zero; nextfile() has no effect. After all lines have been
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 read, filename() and the line number functions return the values
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 pertaining to the last line read; nextfile() has no effect.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 All files are opened in text mode. If an I/O error occurs during
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 opening or reading a file, the IOError exception is raised.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 If sys.stdin is used more than once, the second and further use will
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 return no lines, except perhaps for interactive use, or if it has been
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 explicitly reset (e.g. using sys.stdin.seek(0)).
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 Empty files are opened and immediately closed; the only time their
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 presence in the list of filenames is noticeable at all is when the
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 last file opened is empty.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 It is possible that the last line of a file doesn't end in a newline
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 character; otherwise lines are returned including the trailing
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 newline.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Class FileInput is the implementation; its methods filename(),
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 correspond to the functions in the module. In addition it has a
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 readline() method which returns the next input line, and a
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 __getitem__() method which implements the sequence behavior. The
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 sequence must be accessed in strictly sequential order; sequence
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 access and readline() cannot be mixed.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 Optional in-place filtering: if the keyword argument inplace=1 is
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 passed to input() or to the FileInput constructor, the file is moved
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 to a backup file and standard output is directed to the input file.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 This makes it possible to write a filter that rewrites its input file
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 in place. If the keyword argument backup=".<some extension>" is also
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 given, it specifies the extension for the backup file, and the backup
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 file remains around; by default, the extension is ".bak" and it is
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 deleted when the output file is closed. In-place filtering is
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 disabled when standard input is read. XXX The current implementation
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 does not work for MS-DOS 8+3 filesystems.
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 XXX Possible additions:
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 - optional getopt argument processing
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 - specify open mode ('r' or 'rb')
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 - specify buffer size
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 - fileno()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 - isatty()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 - read(), read(size), even readlines()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 """
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
11387
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
76 import sys, os, stat
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 _state = None
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
12340
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
80 def input(files=None, inplace=0, backup=""):
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 global _state
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if _state and _state._file:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
83 raise RuntimeError, "input() already active"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 _state = FileInput(files, inplace, backup)
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return _state
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 def close():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 global _state
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 state = _state
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 _state = None
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 if state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
92 state.close()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 def nextfile():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
96 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return _state.nextfile()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def filename():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
101 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 return _state.filename()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 def lineno():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
106 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return _state.lineno()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def filelineno():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
111 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 return _state.filelineno()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def isfirstline():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
116 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return _state.isfirstline()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 def isstdin():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 if not _state:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
121 raise RuntimeError, "no active input()"
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return _state.isstdin()
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 class FileInput:
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
12340
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
126 def __init__(self, files=None, inplace=0, backup=""):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
127 if type(files) == type(''):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
128 files = (files,)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
129 else:
12340
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
130 if files is None:
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
131 files = sys.argv[1:]
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
132 if not files:
12340
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
133 files = ('-',)
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
134 else:
e2620aab9f2d Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents: 11387
diff changeset
135 files = tuple(files)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
136 self._files = files
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
137 self._inplace = inplace
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
138 self._backup = backup
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
139 self._savestdout = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
140 self._output = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
141 self._filename = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
142 self._lineno = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
143 self._filelineno = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
144 self._file = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
145 self._isstdin = 0
8687
fd49ed65cd20 Add missing initializer for self._backupfilename. Due to Ralph Butler.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
146 self._backupfilename = None
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 def __del__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
149 self.close()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 def close(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
152 self.nextfile()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
153 self._files = ()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 def __getitem__(self, i):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
156 if i != self._lineno:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
157 raise RuntimeError, "accessing lines out of order"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
158 line = self.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
159 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
160 raise IndexError, "end of input reached"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
161 return line
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def nextfile(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
164 savestdout = self._savestdout
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
165 self._savestdout = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
166 if savestdout:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
167 sys.stdout = savestdout
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
169 output = self._output
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
170 self._output = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
171 if output:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
172 output.close()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
174 file = self._file
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
175 self._file = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
176 if file and not self._isstdin:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
177 file.close()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
179 backupfilename = self._backupfilename
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
180 self._backupfilename = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
181 if backupfilename and not self._backup:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
182 try: os.unlink(backupfilename)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
183 except: pass
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
185 self._isstdin = 0
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 def readline(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
188 if not self._file:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
189 if not self._files:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
190 return ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
191 self._filename = self._files[0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
192 self._files = self._files[1:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
193 self._filelineno = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
194 self._file = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
195 self._isstdin = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
196 self._backupfilename = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
197 if self._filename == '-':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
198 self._filename = '<stdin>'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
199 self._file = sys.stdin
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
200 self._isstdin = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
201 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
202 if self._inplace:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
203 self._backupfilename = (
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
204 self._filename + (self._backup or ".bak"))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
205 try: os.unlink(self._backupfilename)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
206 except os.error: pass
11387
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
207 # The next few lines may raise IOError
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
208 os.rename(self._filename, self._backupfilename)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
209 self._file = open(self._backupfilename, "r")
11387
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
210 try:
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
211 perm = os.fstat(self._file.fileno())[stat.ST_MODE]
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
212 except:
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
213 self._output = open(self._filename, "w")
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
214 else:
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
215 fd = os.open(self._filename,
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
216 os.O_CREAT | os.O_WRONLY | os.O_TRUNC,
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
217 perm)
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
218 self._output = os.fdopen(fd, "w")
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
219 try:
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
220 os.chmod(self._filename, perm)
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
221 except:
019cd21c8c9f Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents: 8687
diff changeset
222 pass
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
223 self._savestdout = sys.stdout
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
224 sys.stdout = self._output
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
225 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
226 # This may raise IOError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
227 self._file = open(self._filename, "r")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
228 line = self._file.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
229 if line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
230 self._lineno = self._lineno + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
231 self._filelineno = self._filelineno + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
232 return line
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
233 self.nextfile()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
234 # Recursive call
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
235 return self.readline()
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 def filename(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
238 return self._filename
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 def lineno(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
241 return self._lineno
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 def filelineno(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
244 return self._filelineno
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
245
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 def isfirstline(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
247 return self._filelineno == 1
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 def isstdin(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
250 return self._isstdin
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 def _test():
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 import getopt
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 inplace = 0
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 backup = 0
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 opts, args = getopt.getopt(sys.argv[1:], "ib:")
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 for o, a in opts:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
258 if o == '-i': inplace = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
259 if o == '-b': backup = a
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 for line in input(args, inplace=inplace, backup=backup):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
261 if line[-1:] == '\n': line = line[:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
262 if line[-1:] == '\r': line = line[:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
263 print "%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6620
diff changeset
264 isfirstline() and "*" or "", line)
6620
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 print "%d: %s[%d]" % (lineno(), filename(), filelineno())
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 if __name__ == '__main__':
00b1b77c4177 A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 _test()