blob: 93abad38f434d7ecf8d208cdefc310bd73a5a673 [file] [log] [blame]
Greg Wardd1dc4751999-01-13 16:12:04 +00001"""text_file
2
3provides the TextFile class, which gives an interface to text files
4that (optionally) takes care of stripping comments, ignoring blank
5lines, and joining lines with backslashes."""
6
Serhiy Storchakaccd047e2016-04-25 00:12:32 +03007import sys, io
Tarek Ziadé36797272010-07-22 12:50:05 +00008
Greg Wardd1dc4751999-01-13 16:12:04 +00009
10class TextFile:
Greg Ward274ad9d1999-09-29 13:03:32 +000011 """Provides a file-like object that takes care of all the things you
12 commonly want to do when processing a text file that has some
Greg Ward60cd2862000-09-16 18:04:55 +000013 line-by-line syntax: strip comments (as long as "#" is your
14 comment character), skip blank lines, join adjacent lines by
15 escaping the newline (ie. backslash at end of line), strip
16 leading and/or trailing whitespace. All of these are optional
17 and independently controllable.
Greg Ward274ad9d1999-09-29 13:03:32 +000018
19 Provides a 'warn()' method so you can generate warning messages that
20 report physical line number, even if the logical line in question
21 spans multiple physical lines. Also provides 'unreadline()' for
22 implementing line-at-a-time lookahead.
23
24 Constructor is called as:
25
26 TextFile (filename=None, file=None, **options)
27
28 It bombs (RuntimeError) if both 'filename' and 'file' are None;
29 'filename' should be a string, and 'file' a file object (or
30 something that provides 'readline()' and 'close()' methods). It is
31 recommended that you supply at least 'filename', so that TextFile
32 can include it in warning messages. If 'file' is not supplied,
Guido van Rossum63236cf2007-05-25 18:39:29 +000033 TextFile creates its own using 'io.open()'.
Greg Ward274ad9d1999-09-29 13:03:32 +000034
35 The options are all boolean, and affect the value returned by
36 'readline()':
37 strip_comments [default: true]
38 strip from "#" to end-of-line, as well as any whitespace
39 leading up to the "#" -- unless it is escaped by a backslash
40 lstrip_ws [default: false]
41 strip leading whitespace from each line before returning it
42 rstrip_ws [default: true]
43 strip trailing whitespace (including line terminator!) from
44 each line before returning it
45 skip_blanks [default: true}
46 skip lines that are empty *after* stripping comments and
Greg Ward60cd2862000-09-16 18:04:55 +000047 whitespace. (If both lstrip_ws and rstrip_ws are false,
Greg Ward274ad9d1999-09-29 13:03:32 +000048 then some lines may consist of solely whitespace: these will
49 *not* be skipped, even if 'skip_blanks' is true.)
50 join_lines [default: false]
51 if a backslash is the last non-newline character on a line
52 after stripping comments and whitespace, join the following line
53 to it to form one "logical line"; if N consecutive lines end
54 with a backslash, then N+1 physical lines will be joined to
55 form one logical line.
Greg Ward60cd2862000-09-16 18:04:55 +000056 collapse_join [default: false]
57 strip leading whitespace from lines that are joined to their
58 predecessor; only matters if (join_lines and not lstrip_ws)
Victor Stinner75d8c5c2010-10-23 17:02:31 +000059 errors [default: 'strict']
60 error handler used to decode the file content
Greg Ward274ad9d1999-09-29 13:03:32 +000061
62 Note that since 'rstrip_ws' can strip the trailing newline, the
63 semantics of 'readline()' must differ from those of the builtin file
64 object's 'readline()' method! In particular, 'readline()' returns
65 None for end-of-file: an empty string might just be a blank line (or
66 an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
67 not."""
68
Greg Wardd1dc4751999-01-13 16:12:04 +000069 default_options = { 'strip_comments': 1,
Greg Wardd1dc4751999-01-13 16:12:04 +000070 'skip_blanks': 1,
Greg Wardd1dc4751999-01-13 16:12:04 +000071 'lstrip_ws': 0,
72 'rstrip_ws': 1,
Greg Ward60cd2862000-09-16 18:04:55 +000073 'join_lines': 0,
74 'collapse_join': 0,
Victor Stinner75d8c5c2010-10-23 17:02:31 +000075 'errors': 'strict',
Greg Wardd1dc4751999-01-13 16:12:04 +000076 }
77
Collin Winter5b7e9d72007-08-30 03:52:21 +000078 def __init__(self, filename=None, file=None, **options):
Greg Ward274ad9d1999-09-29 13:03:32 +000079 """Construct a new TextFile object. At least one of 'filename'
80 (a string) and 'file' (a file-like object) must be supplied.
81 They keyword argument options are described above and affect
82 the values returned by 'readline()'."""
Greg Ward782cdfe1999-03-23 14:00:06 +000083 if filename is None and file is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +000084 raise RuntimeError("you must supply either or both of 'filename' and 'file'")
Greg Wardd1dc4751999-01-13 16:12:04 +000085
86 # set values for all options -- either from client option hash
87 # or fallback to default_options
88 for opt in self.default_options.keys():
Guido van Rossume2b70bc2006-08-18 22:13:04 +000089 if opt in options:
Collin Winter5b7e9d72007-08-30 03:52:21 +000090 setattr(self, opt, options[opt])
Greg Wardd1dc4751999-01-13 16:12:04 +000091 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +000092 setattr(self, opt, self.default_options[opt])
Greg Wardd1dc4751999-01-13 16:12:04 +000093
94 # sanity check client option hash
95 for opt in options.keys():
Guido van Rossume2b70bc2006-08-18 22:13:04 +000096 if opt not in self.default_options:
Collin Winter5b7e9d72007-08-30 03:52:21 +000097 raise KeyError("invalid TextFile option '%s'" % opt)
Greg Wardd1dc4751999-01-13 16:12:04 +000098
Greg Ward782cdfe1999-03-23 14:00:06 +000099 if file is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000100 self.open(filename)
Greg Ward782cdfe1999-03-23 14:00:06 +0000101 else:
102 self.filename = filename
103 self.file = file
104 self.current_line = 0 # assuming that file is at BOF!
Greg Ward787451b1999-03-26 21:48:59 +0000105
Greg Ward91c488c1999-03-29 18:01:49 +0000106 # 'linebuf' is a stack of lines that will be emptied before we
107 # actually read from the file; it's only populated by an
108 # 'unreadline()' operation
109 self.linebuf = []
Fred Drakeb94b8492001-12-06 20:51:35 +0000110
Collin Winter5b7e9d72007-08-30 03:52:21 +0000111 def open(self, filename):
Greg Ward274ad9d1999-09-29 13:03:32 +0000112 """Open a new file named 'filename'. This overrides both the
113 'filename' and 'file' arguments to the constructor."""
Greg Ward782cdfe1999-03-23 14:00:06 +0000114 self.filename = filename
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000115 self.file = io.open(self.filename, 'r', errors=self.errors)
Greg Wardd1dc4751999-01-13 16:12:04 +0000116 self.current_line = 0
117
Collin Winter5b7e9d72007-08-30 03:52:21 +0000118 def close(self):
Greg Ward274ad9d1999-09-29 13:03:32 +0000119 """Close the current file and forget everything we know about it
120 (filename, current line number)."""
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300121 file = self.file
Greg Wardd1dc4751999-01-13 16:12:04 +0000122 self.file = None
123 self.filename = None
124 self.current_line = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300125 file.close()
Greg Wardd1dc4751999-01-13 16:12:04 +0000126
Collin Winter5b7e9d72007-08-30 03:52:21 +0000127 def gen_error(self, msg, line=None):
Greg Wardf11296b2000-09-16 18:06:31 +0000128 outmsg = []
129 if line is None:
130 line = self.current_line
131 outmsg.append(self.filename + ", ")
Collin Winter5b7e9d72007-08-30 03:52:21 +0000132 if isinstance(line, (list, tuple)):
133 outmsg.append("lines %d-%d: " % tuple(line))
Greg Wardf11296b2000-09-16 18:06:31 +0000134 else:
135 outmsg.append("line %d: " % line)
136 outmsg.append(str(msg))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000137 return "".join(outmsg)
Greg Wardf11296b2000-09-16 18:06:31 +0000138
Collin Winter5b7e9d72007-08-30 03:52:21 +0000139 def error(self, msg, line=None):
140 raise ValueError("error: " + self.gen_error(msg, line))
Greg Wardf11296b2000-09-16 18:06:31 +0000141
Collin Winter5b7e9d72007-08-30 03:52:21 +0000142 def warn(self, msg, line=None):
Greg Ward274ad9d1999-09-29 13:03:32 +0000143 """Print (to stderr) a warning message tied to the current logical
144 line in the current file. If the current logical line in the
145 file spans multiple physical lines, the warning refers to the
146 whole range, eg. "lines 3-5". If 'line' supplied, it overrides
147 the current line number; it may be a list or tuple to indicate a
148 range of physical lines, or an integer for a single physical
149 line."""
Greg Wardf11296b2000-09-16 18:06:31 +0000150 sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n")
Greg Wardf6cdcd51999-01-18 17:08:16 +0000151
Collin Winter5b7e9d72007-08-30 03:52:21 +0000152 def readline(self):
Greg Ward274ad9d1999-09-29 13:03:32 +0000153 """Read and return a single logical line from the current file (or
154 from an internal buffer if lines have previously been "unread"
155 with 'unreadline()'). If the 'join_lines' option is true, this
156 may involve reading multiple physical lines concatenated into a
157 single string. Updates the current line number, so calling
158 'warn()' after 'readline()' emits a warning about the physical
159 line(s) just read. Returns None on end-of-file, since the empty
160 string can occur if 'rstrip_ws' is true but 'strip_blanks' is
161 not."""
Greg Ward91c488c1999-03-29 18:01:49 +0000162 # If any "unread" lines waiting in 'linebuf', return the top
163 # one. (We don't actually buffer read-ahead data -- lines only
164 # get put in 'linebuf' if the client explicitly does an
165 # 'unreadline()'.
166 if self.linebuf:
167 line = self.linebuf[-1]
168 del self.linebuf[-1]
169 return line
170
Greg Wardd1dc4751999-01-13 16:12:04 +0000171 buildup_line = ''
172
Collin Winter5b7e9d72007-08-30 03:52:21 +0000173 while True:
Greg Wardabc2f961999-08-10 20:09:38 +0000174 # read the line, make it None if EOF
Greg Wardd1dc4751999-01-13 16:12:04 +0000175 line = self.file.readline()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000176 if line == '':
177 line = None
Greg Wardabc2f961999-08-10 20:09:38 +0000178
Greg Wardd1dc4751999-01-13 16:12:04 +0000179 if self.strip_comments and line:
Greg Wardabc2f961999-08-10 20:09:38 +0000180
181 # Look for the first "#" in the line. If none, never
182 # mind. If we find one and it's the first character, or
183 # is not preceded by "\", then it starts a comment --
184 # strip the comment, strip whitespace before it, and
185 # carry on. Otherwise, it's just an escaped "#", so
186 # unescape it (and any other escaped "#"'s that might be
187 # lurking in there) and otherwise leave the line alone.
188
Collin Winter5b7e9d72007-08-30 03:52:21 +0000189 pos = line.find("#")
190 if pos == -1: # no "#" -- no comments
Greg Wardabc2f961999-08-10 20:09:38 +0000191 pass
Greg Wardacff0b32000-09-16 18:33:36 +0000192
193 # It's definitely a comment -- either "#" is the first
194 # character, or it's elsewhere and unescaped.
195 elif pos == 0 or line[pos-1] != "\\":
Greg Ward274ad9d1999-09-29 13:03:32 +0000196 # Have to preserve the trailing newline, because it's
197 # the job of a later step (rstrip_ws) to remove it --
198 # and if rstrip_ws is false, we'd better preserve it!
199 # (NB. this means that if the final line is all comment
200 # and has no trailing newline, we will think that it's
Greg Wardabc2f961999-08-10 20:09:38 +0000201 # EOF; I think that's OK.)
Greg Ward274ad9d1999-09-29 13:03:32 +0000202 eol = (line[-1] == '\n') and '\n' or ''
203 line = line[0:pos] + eol
Fred Drakeb94b8492001-12-06 20:51:35 +0000204
Greg Wardacff0b32000-09-16 18:33:36 +0000205 # If all that's left is whitespace, then skip line
206 # *now*, before we try to join it to 'buildup_line' --
207 # that way constructs like
208 # hello \\
209 # # comment that should be ignored
210 # there
211 # result in "hello there".
Collin Winter5b7e9d72007-08-30 03:52:21 +0000212 if line.strip() == "":
Greg Wardacff0b32000-09-16 18:33:36 +0000213 continue
Collin Winter5b7e9d72007-08-30 03:52:21 +0000214 else: # it's an escaped "#"
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000215 line = line.replace("\\#", "#")
Fred Drakeb94b8492001-12-06 20:51:35 +0000216
Greg Wardd1dc4751999-01-13 16:12:04 +0000217 # did previous line end with a backslash? then accumulate
218 if self.join_lines and buildup_line:
219 # oops: end of file
Greg Wardabc2f961999-08-10 20:09:38 +0000220 if line is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000221 self.warn("continuation line immediately precedes "
222 "end-of-file")
Greg Wardd1dc4751999-01-13 16:12:04 +0000223 return buildup_line
224
Greg Ward60cd2862000-09-16 18:04:55 +0000225 if self.collapse_join:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000226 line = line.lstrip()
Greg Wardd1dc4751999-01-13 16:12:04 +0000227 line = buildup_line + line
228
229 # careful: pay attention to line number when incrementing it
Collin Winter5b7e9d72007-08-30 03:52:21 +0000230 if isinstance(self.current_line, list):
Greg Wardd1dc4751999-01-13 16:12:04 +0000231 self.current_line[1] = self.current_line[1] + 1
232 else:
Greg Wardacff0b32000-09-16 18:33:36 +0000233 self.current_line = [self.current_line,
Collin Winter5b7e9d72007-08-30 03:52:21 +0000234 self.current_line + 1]
Greg Wardd1dc4751999-01-13 16:12:04 +0000235 # just an ordinary line, read it as usual
236 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000237 if line is None: # eof
Greg Wardd1dc4751999-01-13 16:12:04 +0000238 return None
239
240 # still have to be careful about incrementing the line number!
Collin Winter5b7e9d72007-08-30 03:52:21 +0000241 if isinstance(self.current_line, list):
Greg Wardd1dc4751999-01-13 16:12:04 +0000242 self.current_line = self.current_line[1] + 1
243 else:
244 self.current_line = self.current_line + 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000245
Greg Wardd1dc4751999-01-13 16:12:04 +0000246 # strip whitespace however the client wants (leading and
247 # trailing, or one or the other, or neither)
248 if self.lstrip_ws and self.rstrip_ws:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000249 line = line.strip()
Greg Ward274ad9d1999-09-29 13:03:32 +0000250 elif self.lstrip_ws:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000251 line = line.lstrip()
Greg Ward274ad9d1999-09-29 13:03:32 +0000252 elif self.rstrip_ws:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000253 line = line.rstrip()
Greg Wardd1dc4751999-01-13 16:12:04 +0000254
255 # blank line (whether we rstrip'ed or not)? skip to next line
256 # if appropriate
Greg Ward3d05c162000-09-16 18:09:22 +0000257 if (line == '' or line == '\n') and self.skip_blanks:
Greg Wardd1dc4751999-01-13 16:12:04 +0000258 continue
259
260 if self.join_lines:
261 if line[-1] == '\\':
262 buildup_line = line[:-1]
263 continue
264
265 if line[-2:] == '\\\n':
266 buildup_line = line[0:-2] + '\n'
267 continue
268
Greg Wardd1dc4751999-01-13 16:12:04 +0000269 # well, I guess there's some actual content there: return it
270 return line
271
Collin Winter5b7e9d72007-08-30 03:52:21 +0000272 def readlines(self):
Greg Ward274ad9d1999-09-29 13:03:32 +0000273 """Read and return the list of all logical lines remaining in the
274 current file."""
Greg Wardd1dc4751999-01-13 16:12:04 +0000275 lines = []
Collin Winter5b7e9d72007-08-30 03:52:21 +0000276 while True:
Greg Wardd1dc4751999-01-13 16:12:04 +0000277 line = self.readline()
278 if line is None:
279 return lines
Collin Winter5b7e9d72007-08-30 03:52:21 +0000280 lines.append(line)
Greg Wardd1dc4751999-01-13 16:12:04 +0000281
Collin Winter5b7e9d72007-08-30 03:52:21 +0000282 def unreadline(self, line):
Greg Ward274ad9d1999-09-29 13:03:32 +0000283 """Push 'line' (a string) onto an internal buffer that will be
284 checked by future 'readline()' calls. Handy for implementing
285 a parser with line-at-a-time lookahead."""
Collin Winter5b7e9d72007-08-30 03:52:21 +0000286 self.linebuf.append(line)