annotate Lib/distutils/text_file.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents b3515ed28135
children 3e2b7965ada9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
1 """text_file
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
2
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
3 provides the TextFile class, which gives an interface to text files
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
4 that (optionally) takes care of stripping comments, ignoring blank
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
5 lines, and joining lines with backslashes."""
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
6
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
7 __revision__ = "$Id$"
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
8
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
9 from types import *
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
10 import sys, os, string
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
11
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
12
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
13 class TextFile:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
14
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
15 """Provides a file-like object that takes care of all the things you
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
16 commonly want to do when processing a text file that has some
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
17 line-by-line syntax: strip comments (as long as "#" is your
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
18 comment character), skip blank lines, join adjacent lines by
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
19 escaping the newline (ie. backslash at end of line), strip
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
20 leading and/or trailing whitespace. All of these are optional
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
21 and independently controllable.
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
22
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
23 Provides a 'warn()' method so you can generate warning messages that
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
24 report physical line number, even if the logical line in question
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
25 spans multiple physical lines. Also provides 'unreadline()' for
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
26 implementing line-at-a-time lookahead.
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
27
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
28 Constructor is called as:
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
29
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
30 TextFile (filename=None, file=None, **options)
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
31
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
32 It bombs (RuntimeError) if both 'filename' and 'file' are None;
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
33 'filename' should be a string, and 'file' a file object (or
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
34 something that provides 'readline()' and 'close()' methods). It is
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
35 recommended that you supply at least 'filename', so that TextFile
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
36 can include it in warning messages. If 'file' is not supplied,
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
37 TextFile creates its own using the 'open()' builtin.
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
38
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
39 The options are all boolean, and affect the value returned by
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
40 'readline()':
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
41 strip_comments [default: true]
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
42 strip from "#" to end-of-line, as well as any whitespace
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
43 leading up to the "#" -- unless it is escaped by a backslash
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
44 lstrip_ws [default: false]
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
45 strip leading whitespace from each line before returning it
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
46 rstrip_ws [default: true]
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
47 strip trailing whitespace (including line terminator!) from
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
48 each line before returning it
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
49 skip_blanks [default: true}
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
50 skip lines that are empty *after* stripping comments and
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
51 whitespace. (If both lstrip_ws and rstrip_ws are false,
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
52 then some lines may consist of solely whitespace: these will
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
53 *not* be skipped, even if 'skip_blanks' is true.)
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
54 join_lines [default: false]
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
55 if a backslash is the last non-newline character on a line
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
56 after stripping comments and whitespace, join the following line
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
57 to it to form one "logical line"; if N consecutive lines end
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
58 with a backslash, then N+1 physical lines will be joined to
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
59 form one logical line.
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
60 collapse_join [default: false]
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
61 strip leading whitespace from lines that are joined to their
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
62 predecessor; only matters if (join_lines and not lstrip_ws)
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
63
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
64 Note that since 'rstrip_ws' can strip the trailing newline, the
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
65 semantics of 'readline()' must differ from those of the builtin file
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
66 object's 'readline()' method! In particular, 'readline()' returns
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
67 None for end-of-file: an empty string might just be a blank line (or
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
68 an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
69 not."""
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
70
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
71 default_options = { 'strip_comments': 1,
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
72 'skip_blanks': 1,
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
73 'lstrip_ws': 0,
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
74 'rstrip_ws': 1,
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
75 'join_lines': 0,
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
76 'collapse_join': 0,
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
77 }
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
78
10447
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
79 def __init__ (self, filename=None, file=None, **options):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
80 """Construct a new TextFile object. At least one of 'filename'
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
81 (a string) and 'file' (a file-like object) must be supplied.
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
82 They keyword argument options are described above and affect
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
83 the values returned by 'readline()'."""
10447
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
84
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
85 if filename is None and file is None:
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
86 raise RuntimeError, \
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14643
diff changeset
87 "you must supply either or both of 'filename' and 'file'"
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
88
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
89 # set values for all options -- either from client option hash
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
90 # or fallback to default_options
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
91 for opt in self.default_options.keys():
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 26210
diff changeset
92 if opt in options:
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
93 setattr (self, opt, options[opt])
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
94
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
95 else:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
96 setattr (self, opt, self.default_options[opt])
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
97
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
98 # sanity check client option hash
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
99 for opt in options.keys():
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 26210
diff changeset
100 if opt not in self.default_options:
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
101 raise KeyError, "invalid TextFile option '%s'" % opt
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
102
10447
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
103 if file is None:
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
104 self.open (filename)
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
105 else:
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
106 self.filename = filename
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
107 self.file = file
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
108 self.current_line = 0 # assuming that file is at BOF!
10483
7474ad7e0a44 Added 'linestart' array and 'unreadline()' method (makes parsing a lot easier).
Greg Ward <gward@python.net>
parents: 10447
diff changeset
109
10492
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
110 # 'linebuf' is a stack of lines that will be emptied before we
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
111 # actually read from the file; it's only populated by an
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
112 # 'unreadline()' operation
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
113 self.linebuf = []
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14643
diff changeset
114
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
115
10447
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
116 def open (self, filename):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
117 """Open a new file named 'filename'. This overrides both the
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
118 'filename' and 'file' arguments to the constructor."""
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
119
10447
d3762933bff8 Changes to allow passing an open file to the constructor (to support
Greg Ward <gward@python.net>
parents: 10360
diff changeset
120 self.filename = filename
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
121 self.file = open (self.filename, 'r')
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
122 self.current_line = 0
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
123
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
124
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
125 def close (self):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
126 """Close the current file and forget everything we know about it
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
127 (filename, current line number)."""
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
128
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
129 self.file.close ()
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
130 self.file = None
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
131 self.filename = None
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
132 self.current_line = None
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
133
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
134
14641
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
135 def gen_error (self, msg, line=None):
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
136 outmsg = []
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
137 if line is None:
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
138 line = self.current_line
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
139 outmsg.append(self.filename + ", ")
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
140 if type (line) in (ListType, TupleType):
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
141 outmsg.append("lines %d-%d: " % tuple (line))
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
142 else:
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
143 outmsg.append("line %d: " % line)
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
144 outmsg.append(str(msg))
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
145 return string.join(outmsg, "")
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
146
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
147
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
148 def error (self, msg, line=None):
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
149 raise ValueError, "error: " + self.gen_error(msg, line)
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
150
10610
3f1e72c71b3a 'warn()' method now takes an optional line number.
Greg Ward <gward@python.net>
parents: 10492
diff changeset
151 def warn (self, msg, line=None):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
152 """Print (to stderr) a warning message tied to the current logical
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
153 line in the current file. If the current logical line in the
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
154 file spans multiple physical lines, the warning refers to the
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
155 whole range, eg. "lines 3-5". If 'line' supplied, it overrides
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
156 the current line number; it may be a list or tuple to indicate a
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
157 range of physical lines, or an integer for a single physical
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
158 line."""
14641
8a7d47d07981 [change from 2000/08/11, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 14640
diff changeset
159 sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n")
9904
d95afe87db4d Added 'warn' method.
Greg Ward <gward@python.net>
parents: 9862
diff changeset
160
d95afe87db4d Added 'warn' method.
Greg Ward <gward@python.net>
parents: 9862
diff changeset
161
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
162 def readline (self):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
163 """Read and return a single logical line from the current file (or
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
164 from an internal buffer if lines have previously been "unread"
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
165 with 'unreadline()'). If the 'join_lines' option is true, this
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
166 may involve reading multiple physical lines concatenated into a
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
167 single string. Updates the current line number, so calling
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
168 'warn()' after 'readline()' emits a warning about the physical
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
169 line(s) just read. Returns None on end-of-file, since the empty
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
170 string can occur if 'rstrip_ws' is true but 'strip_blanks' is
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
171 not."""
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
172
10492
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
173 # If any "unread" lines waiting in 'linebuf', return the top
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
174 # one. (We don't actually buffer read-ahead data -- lines only
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
175 # get put in 'linebuf' if the client explicitly does an
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
176 # 'unreadline()'.
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
177 if self.linebuf:
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
178 line = self.linebuf[-1]
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
179 del self.linebuf[-1]
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
180 return line
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
181
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
182 buildup_line = ''
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
183
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
184 while 1:
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
185 # read the line, make it None if EOF
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
186 line = self.file.readline()
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
187 if line == '': line = None
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
188
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
189 if self.strip_comments and line:
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
190
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
191 # Look for the first "#" in the line. If none, never
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
192 # mind. If we find one and it's the first character, or
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
193 # is not preceded by "\", then it starts a comment --
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
194 # strip the comment, strip whitespace before it, and
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
195 # carry on. Otherwise, it's just an escaped "#", so
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
196 # unescape it (and any other escaped "#"'s that might be
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
197 # lurking in there) and otherwise leave the line alone.
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
198
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
199 pos = string.find (line, "#")
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
200 if pos == -1: # no "#" -- no comments
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
201 pass
14643
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
202
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
203 # It's definitely a comment -- either "#" is the first
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
204 # character, or it's elsewhere and unescaped.
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
205 elif pos == 0 or line[pos-1] != "\\":
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
206 # Have to preserve the trailing newline, because it's
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
207 # the job of a later step (rstrip_ws) to remove it --
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
208 # and if rstrip_ws is false, we'd better preserve it!
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
209 # (NB. this means that if the final line is all comment
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
210 # and has no trailing newline, we will think that it's
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
211 # EOF; I think that's OK.)
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
212 eol = (line[-1] == '\n') and '\n' or ''
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
213 line = line[0:pos] + eol
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14643
diff changeset
214
14643
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
215 # If all that's left is whitespace, then skip line
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
216 # *now*, before we try to join it to 'buildup_line' --
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
217 # that way constructs like
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
218 # hello \\
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
219 # # comment that should be ignored
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
220 # there
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
221 # result in "hello there".
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
222 if string.strip(line) == "":
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
223 continue
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
224
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
225 else: # it's an escaped "#"
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
226 line = string.replace (line, "\\#", "#")
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14643
diff changeset
227
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
228
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
229 # did previous line end with a backslash? then accumulate
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
230 if self.join_lines and buildup_line:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
231 # oops: end of file
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
232 if line is None:
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
233 self.warn ("continuation line immediately precedes "
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
234 "end-of-file")
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
235 return buildup_line
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
236
14640
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
237 if self.collapse_join:
3fefeafa3f4a [change from 2000/04/17, propagating now to distutils copy]
Greg Ward <gward@python.net>
parents: 11342
diff changeset
238 line = string.lstrip (line)
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
239 line = buildup_line + line
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
240
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
241 # careful: pay attention to line number when incrementing it
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
242 if type (self.current_line) is ListType:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
243 self.current_line[1] = self.current_line[1] + 1
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
244 else:
14643
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
245 self.current_line = [self.current_line,
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
246 self.current_line+1]
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
247 # just an ordinary line, read it as usual
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
248 else:
11185
dbb8083f0ce7 Allow comment characters (#) to be escaped:
Greg Ward <gward@python.net>
parents: 10610
diff changeset
249 if line is None: # eof
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
250 return None
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
251
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
252 # still have to be careful about incrementing the line number!
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
253 if type (self.current_line) is ListType:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
254 self.current_line = self.current_line[1] + 1
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
255 else:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
256 self.current_line = self.current_line + 1
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14643
diff changeset
257
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
258
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
259 # strip whitespace however the client wants (leading and
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
260 # trailing, or one or the other, or neither)
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
261 if self.lstrip_ws and self.rstrip_ws:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
262 line = string.strip (line)
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
263 elif self.lstrip_ws:
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
264 line = string.lstrip (line)
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
265 elif self.rstrip_ws:
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
266 line = string.rstrip (line)
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
267
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
268 # blank line (whether we rstrip'ed or not)? skip to next line
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
269 # if appropriate
14642
08e1c3c41e60 Andrew Kuchling:
Greg Ward <gward@python.net>
parents: 14641
diff changeset
270 if (line == '' or line == '\n') and self.skip_blanks:
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
271 continue
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
272
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
273 if self.join_lines:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
274 if line[-1] == '\\':
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
275 buildup_line = line[:-1]
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
276 continue
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
277
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
278 if line[-2:] == '\\\n':
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
279 buildup_line = line[0:-2] + '\n'
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
280 continue
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
281
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
282 # well, I guess there's some actual content there: return it
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
283 return line
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
284
14643
b954c33f0b4b Changed so lines that are all comment (or just whitespace + comment)
Greg Ward <gward@python.net>
parents: 14642
diff changeset
285 # readline ()
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
286
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
287
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
288 def readlines (self):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
289 """Read and return the list of all logical lines remaining in the
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
290 current file."""
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
291
9862
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
292 lines = []
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
293 while 1:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
294 line = self.readline()
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
295 if line is None:
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
296 return lines
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
297 lines.append (line)
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
298
a7d1cf95dadc Added: mems.lib.text_file: provides TextFile class for parsing text
Greg Ward <gward@python.net>
parents:
diff changeset
299
10492
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
300 def unreadline (self, line):
11342
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
301 """Push 'line' (a string) onto an internal buffer that will be
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
302 checked by future 'readline()' calls. Handy for implementing
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
303 a parser with line-at-a-time lookahead."""
d83fa1dd7b79 Added all documentation.
Greg Ward <gward@python.net>
parents: 11185
diff changeset
304
10492
43052d5b9808 Replaced the last attempt at an "unreadline" with one that actually
Greg Ward <gward@python.net>
parents: 10483
diff changeset
305 self.linebuf.append (line)