Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 1 | """text_file |
| 2 | |
| 3 | provides the TextFile class, which gives an interface to text files |
| 4 | that (optionally) takes care of stripping comments, ignoring blank |
| 5 | lines, and joining lines with backslashes.""" |
| 6 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 7 | __revision__ = "$Id$" |
| 8 | |
Guido van Rossum | 63236cf | 2007-05-25 18:39:29 +0000 | [diff] [blame] | 9 | import sys, os, io |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class TextFile: |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 13 | """Provides a file-like object that takes care of all the things you |
| 14 | commonly want to do when processing a text file that has some |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 15 | line-by-line syntax: strip comments (as long as "#" is your |
| 16 | comment character), skip blank lines, join adjacent lines by |
| 17 | escaping the newline (ie. backslash at end of line), strip |
| 18 | leading and/or trailing whitespace. All of these are optional |
| 19 | and independently controllable. |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 20 | |
| 21 | Provides a 'warn()' method so you can generate warning messages that |
| 22 | report physical line number, even if the logical line in question |
| 23 | spans multiple physical lines. Also provides 'unreadline()' for |
| 24 | implementing line-at-a-time lookahead. |
| 25 | |
| 26 | Constructor is called as: |
| 27 | |
| 28 | TextFile (filename=None, file=None, **options) |
| 29 | |
| 30 | It bombs (RuntimeError) if both 'filename' and 'file' are None; |
| 31 | 'filename' should be a string, and 'file' a file object (or |
| 32 | something that provides 'readline()' and 'close()' methods). It is |
| 33 | recommended that you supply at least 'filename', so that TextFile |
| 34 | can include it in warning messages. If 'file' is not supplied, |
Guido van Rossum | 63236cf | 2007-05-25 18:39:29 +0000 | [diff] [blame] | 35 | TextFile creates its own using 'io.open()'. |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 36 | |
| 37 | The options are all boolean, and affect the value returned by |
| 38 | 'readline()': |
| 39 | strip_comments [default: true] |
| 40 | strip from "#" to end-of-line, as well as any whitespace |
| 41 | leading up to the "#" -- unless it is escaped by a backslash |
| 42 | lstrip_ws [default: false] |
| 43 | strip leading whitespace from each line before returning it |
| 44 | rstrip_ws [default: true] |
| 45 | strip trailing whitespace (including line terminator!) from |
| 46 | each line before returning it |
| 47 | skip_blanks [default: true} |
| 48 | skip lines that are empty *after* stripping comments and |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 49 | whitespace. (If both lstrip_ws and rstrip_ws are false, |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 50 | then some lines may consist of solely whitespace: these will |
| 51 | *not* be skipped, even if 'skip_blanks' is true.) |
| 52 | join_lines [default: false] |
| 53 | if a backslash is the last non-newline character on a line |
| 54 | after stripping comments and whitespace, join the following line |
| 55 | to it to form one "logical line"; if N consecutive lines end |
| 56 | with a backslash, then N+1 physical lines will be joined to |
| 57 | form one logical line. |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 58 | collapse_join [default: false] |
| 59 | strip leading whitespace from lines that are joined to their |
| 60 | predecessor; only matters if (join_lines and not lstrip_ws) |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 61 | |
| 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 Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 69 | default_options = { 'strip_comments': 1, |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 70 | 'skip_blanks': 1, |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 71 | 'lstrip_ws': 0, |
| 72 | 'rstrip_ws': 1, |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 73 | 'join_lines': 0, |
| 74 | 'collapse_join': 0, |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 77 | def __init__(self, filename=None, file=None, **options): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 78 | """Construct a new TextFile object. At least one of 'filename' |
| 79 | (a string) and 'file' (a file-like object) must be supplied. |
| 80 | They keyword argument options are described above and affect |
| 81 | the values returned by 'readline()'.""" |
Greg Ward | 782cdfe | 1999-03-23 14:00:06 +0000 | [diff] [blame] | 82 | if filename is None and file is None: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 83 | raise RuntimeError("you must supply either or both of 'filename' and 'file'") |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 84 | |
| 85 | # set values for all options -- either from client option hash |
| 86 | # or fallback to default_options |
| 87 | for opt in self.default_options.keys(): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 88 | if opt in options: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 89 | setattr(self, opt, options[opt]) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 90 | else: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 91 | setattr(self, opt, self.default_options[opt]) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 92 | |
| 93 | # sanity check client option hash |
| 94 | for opt in options.keys(): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 95 | if opt not in self.default_options: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 96 | raise KeyError("invalid TextFile option '%s'" % opt) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 97 | |
Greg Ward | 782cdfe | 1999-03-23 14:00:06 +0000 | [diff] [blame] | 98 | if file is None: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 99 | self.open(filename) |
Greg Ward | 782cdfe | 1999-03-23 14:00:06 +0000 | [diff] [blame] | 100 | else: |
| 101 | self.filename = filename |
| 102 | self.file = file |
| 103 | self.current_line = 0 # assuming that file is at BOF! |
Greg Ward | 787451b | 1999-03-26 21:48:59 +0000 | [diff] [blame] | 104 | |
Greg Ward | 91c488c | 1999-03-29 18:01:49 +0000 | [diff] [blame] | 105 | # 'linebuf' is a stack of lines that will be emptied before we |
| 106 | # actually read from the file; it's only populated by an |
| 107 | # 'unreadline()' operation |
| 108 | self.linebuf = [] |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 109 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 110 | def open(self, filename): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 111 | """Open a new file named 'filename'. This overrides both the |
| 112 | 'filename' and 'file' arguments to the constructor.""" |
Greg Ward | 782cdfe | 1999-03-23 14:00:06 +0000 | [diff] [blame] | 113 | self.filename = filename |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 114 | self.file = io.open(self.filename, 'r') |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 115 | self.current_line = 0 |
| 116 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 117 | def close(self): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 118 | """Close the current file and forget everything we know about it |
| 119 | (filename, current line number).""" |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 120 | self.file.close() |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 121 | self.file = None |
| 122 | self.filename = None |
| 123 | self.current_line = None |
| 124 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 125 | def gen_error(self, msg, line=None): |
Greg Ward | f11296b | 2000-09-16 18:06:31 +0000 | [diff] [blame] | 126 | outmsg = [] |
| 127 | if line is None: |
| 128 | line = self.current_line |
| 129 | outmsg.append(self.filename + ", ") |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 130 | if isinstance(line, (list, tuple)): |
| 131 | outmsg.append("lines %d-%d: " % tuple(line)) |
Greg Ward | f11296b | 2000-09-16 18:06:31 +0000 | [diff] [blame] | 132 | else: |
| 133 | outmsg.append("line %d: " % line) |
| 134 | outmsg.append(str(msg)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 135 | return "".join(outmsg) |
Greg Ward | f11296b | 2000-09-16 18:06:31 +0000 | [diff] [blame] | 136 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 137 | def error(self, msg, line=None): |
| 138 | raise ValueError("error: " + self.gen_error(msg, line)) |
Greg Ward | f11296b | 2000-09-16 18:06:31 +0000 | [diff] [blame] | 139 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 140 | def warn(self, msg, line=None): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 141 | """Print (to stderr) a warning message tied to the current logical |
| 142 | line in the current file. If the current logical line in the |
| 143 | file spans multiple physical lines, the warning refers to the |
| 144 | whole range, eg. "lines 3-5". If 'line' supplied, it overrides |
| 145 | the current line number; it may be a list or tuple to indicate a |
| 146 | range of physical lines, or an integer for a single physical |
| 147 | line.""" |
Greg Ward | f11296b | 2000-09-16 18:06:31 +0000 | [diff] [blame] | 148 | sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n") |
Greg Ward | f6cdcd5 | 1999-01-18 17:08:16 +0000 | [diff] [blame] | 149 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 150 | def readline(self): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 151 | """Read and return a single logical line from the current file (or |
| 152 | from an internal buffer if lines have previously been "unread" |
| 153 | with 'unreadline()'). If the 'join_lines' option is true, this |
| 154 | may involve reading multiple physical lines concatenated into a |
| 155 | single string. Updates the current line number, so calling |
| 156 | 'warn()' after 'readline()' emits a warning about the physical |
| 157 | line(s) just read. Returns None on end-of-file, since the empty |
| 158 | string can occur if 'rstrip_ws' is true but 'strip_blanks' is |
| 159 | not.""" |
Greg Ward | 91c488c | 1999-03-29 18:01:49 +0000 | [diff] [blame] | 160 | # If any "unread" lines waiting in 'linebuf', return the top |
| 161 | # one. (We don't actually buffer read-ahead data -- lines only |
| 162 | # get put in 'linebuf' if the client explicitly does an |
| 163 | # 'unreadline()'. |
| 164 | if self.linebuf: |
| 165 | line = self.linebuf[-1] |
| 166 | del self.linebuf[-1] |
| 167 | return line |
| 168 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 169 | buildup_line = '' |
| 170 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 171 | while True: |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 172 | # read the line, make it None if EOF |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 173 | line = self.file.readline() |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 174 | if line == '': |
| 175 | line = None |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 176 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 177 | if self.strip_comments and line: |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 178 | |
| 179 | # Look for the first "#" in the line. If none, never |
| 180 | # mind. If we find one and it's the first character, or |
| 181 | # is not preceded by "\", then it starts a comment -- |
| 182 | # strip the comment, strip whitespace before it, and |
| 183 | # carry on. Otherwise, it's just an escaped "#", so |
| 184 | # unescape it (and any other escaped "#"'s that might be |
| 185 | # lurking in there) and otherwise leave the line alone. |
| 186 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 187 | pos = line.find("#") |
| 188 | if pos == -1: # no "#" -- no comments |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 189 | pass |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 190 | |
| 191 | # It's definitely a comment -- either "#" is the first |
| 192 | # character, or it's elsewhere and unescaped. |
| 193 | elif pos == 0 or line[pos-1] != "\\": |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 194 | # Have to preserve the trailing newline, because it's |
| 195 | # the job of a later step (rstrip_ws) to remove it -- |
| 196 | # and if rstrip_ws is false, we'd better preserve it! |
| 197 | # (NB. this means that if the final line is all comment |
| 198 | # and has no trailing newline, we will think that it's |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 199 | # EOF; I think that's OK.) |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 200 | eol = (line[-1] == '\n') and '\n' or '' |
| 201 | line = line[0:pos] + eol |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 202 | |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 203 | # If all that's left is whitespace, then skip line |
| 204 | # *now*, before we try to join it to 'buildup_line' -- |
| 205 | # that way constructs like |
| 206 | # hello \\ |
| 207 | # # comment that should be ignored |
| 208 | # there |
| 209 | # result in "hello there". |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 210 | if line.strip() == "": |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 211 | continue |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 212 | else: # it's an escaped "#" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 213 | line = line.replace("\\#", "#") |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 214 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 215 | # did previous line end with a backslash? then accumulate |
| 216 | if self.join_lines and buildup_line: |
| 217 | # oops: end of file |
Greg Ward | abc2f96 | 1999-08-10 20:09:38 +0000 | [diff] [blame] | 218 | if line is None: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 219 | self.warn("continuation line immediately precedes " |
| 220 | "end-of-file") |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 221 | return buildup_line |
| 222 | |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 223 | if self.collapse_join: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 224 | line = line.lstrip() |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 225 | line = buildup_line + line |
| 226 | |
| 227 | # careful: pay attention to line number when incrementing it |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 228 | if isinstance(self.current_line, list): |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 229 | self.current_line[1] = self.current_line[1] + 1 |
| 230 | else: |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 231 | self.current_line = [self.current_line, |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 232 | self.current_line + 1] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 233 | # just an ordinary line, read it as usual |
| 234 | else: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 235 | if line is None: # eof |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 236 | return None |
| 237 | |
| 238 | # still have to be careful about incrementing the line number! |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 239 | if isinstance(self.current_line, list): |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 240 | self.current_line = self.current_line[1] + 1 |
| 241 | else: |
| 242 | self.current_line = self.current_line + 1 |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 243 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 244 | # strip whitespace however the client wants (leading and |
| 245 | # trailing, or one or the other, or neither) |
| 246 | if self.lstrip_ws and self.rstrip_ws: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 247 | line = line.strip() |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 248 | elif self.lstrip_ws: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 249 | line = line.lstrip() |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 250 | elif self.rstrip_ws: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 251 | line = line.rstrip() |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 252 | |
| 253 | # blank line (whether we rstrip'ed or not)? skip to next line |
| 254 | # if appropriate |
Greg Ward | 3d05c16 | 2000-09-16 18:09:22 +0000 | [diff] [blame] | 255 | if (line == '' or line == '\n') and self.skip_blanks: |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 256 | continue |
| 257 | |
| 258 | if self.join_lines: |
| 259 | if line[-1] == '\\': |
| 260 | buildup_line = line[:-1] |
| 261 | continue |
| 262 | |
| 263 | if line[-2:] == '\\\n': |
| 264 | buildup_line = line[0:-2] + '\n' |
| 265 | continue |
| 266 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 267 | # well, I guess there's some actual content there: return it |
| 268 | return line |
| 269 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 270 | def readlines(self): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 271 | """Read and return the list of all logical lines remaining in the |
| 272 | current file.""" |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 273 | lines = [] |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 274 | while True: |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 275 | line = self.readline() |
| 276 | if line is None: |
| 277 | return lines |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 278 | lines.append(line) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 279 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 280 | def unreadline(self, line): |
Greg Ward | 274ad9d | 1999-09-29 13:03:32 +0000 | [diff] [blame] | 281 | """Push 'line' (a string) onto an internal buffer that will be |
| 282 | checked by future 'readline()' calls. Handy for implementing |
| 283 | a parser with line-at-a-time lookahead.""" |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 284 | self.linebuf.append(line) |
Greg Ward | 91c488c | 1999-03-29 18:01:49 +0000 | [diff] [blame] | 285 | |
| 286 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 287 | if __name__ == "__main__": |
| 288 | test_data = """# test file |
| 289 | |
| 290 | line 3 \\ |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 291 | # intervening comment |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 292 | continues on next line |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 293 | """ |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 294 | # result 1: no fancy options |
Amaury Forgeot d'Arc | 61cb087 | 2008-07-26 20:09:45 +0000 | [diff] [blame^] | 295 | result1 = [x + "\n" for x in test_data.split("\n")[:-1]] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 296 | |
| 297 | # result 2: just strip comments |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 298 | result2 = ["\n", |
| 299 | "line 3 \\\n", |
| 300 | " continues on next line\n"] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 301 | |
| 302 | # result 3: just strip blank lines |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 303 | result3 = ["# test file\n", |
| 304 | "line 3 \\\n", |
| 305 | "# intervening comment\n", |
| 306 | " continues on next line\n"] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 307 | |
| 308 | # result 4: default, strip comments, blank lines, and trailing whitespace |
Greg Ward | acff0b3 | 2000-09-16 18:33:36 +0000 | [diff] [blame] | 309 | result4 = ["line 3 \\", |
| 310 | " continues on next line"] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 311 | |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 312 | # result 5: strip comments and blanks, plus join lines (but don't |
| 313 | # "collapse" joined lines |
| 314 | result5 = ["line 3 continues on next line"] |
| 315 | |
| 316 | # result 6: strip comments and blanks, plus join lines (and |
| 317 | # "collapse" joined lines |
| 318 | result6 = ["line 3 continues on next line"] |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 319 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 320 | def test_input(count, description, file, expected_result): |
| 321 | result = file.readlines() |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 322 | if result == expected_result: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 323 | print("ok %d (%s)" % (count, description)) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 324 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 325 | print("not ok %d (%s):" % (count, description)) |
| 326 | print("** expected:") |
| 327 | print(expected_result) |
| 328 | print("** received:") |
| 329 | print(result) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 330 | |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 331 | |
| 332 | filename = "test.txt" |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 333 | out_file = open(filename, "w") |
| 334 | out_file.write(test_data) |
| 335 | out_file.close() |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 336 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 337 | in_file = TextFile(filename, strip_comments=0, skip_blanks=0, |
| 338 | lstrip_ws=0, rstrip_ws=0) |
| 339 | test_input(1, "no processing", in_file, result1) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 340 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 341 | in_file = TextFile(filename, strip_comments=1, skip_blanks=0, |
| 342 | lstrip_ws=0, rstrip_ws=0) |
| 343 | test_input(2, "strip comments", in_file, result2) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 344 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 345 | in_file = TextFile(filename, strip_comments=0, skip_blanks=1, |
| 346 | lstrip_ws=0, rstrip_ws=0) |
| 347 | test_input(3, "strip blanks", in_file, result3) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 348 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 349 | in_file = TextFile(filename) |
| 350 | test_input(4, "default processing", in_file, result4) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 351 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 352 | in_file = TextFile(filename, strip_comments=1, skip_blanks=1, |
| 353 | join_lines=1, rstrip_ws=1) |
| 354 | test_input(5, "join lines without collapsing", in_file, result5) |
Greg Ward | 60cd286 | 2000-09-16 18:04:55 +0000 | [diff] [blame] | 355 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 356 | in_file = TextFile(filename, strip_comments=1, skip_blanks=1, |
| 357 | join_lines=1, rstrip_ws=1, collapse_join=1) |
| 358 | test_input(6, "join lines with collapsing", in_file, result6) |
Greg Ward | d1dc475 | 1999-01-13 16:12:04 +0000 | [diff] [blame] | 359 | |
Amaury Forgeot d'Arc | 61cb087 | 2008-07-26 20:09:45 +0000 | [diff] [blame^] | 360 | del in_file |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 361 | os.remove(filename) |