Lines Matching +full:file +full:- +full:lines
5 lines, and joining lines with backslashes."""
11 """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
13 line-by-line syntax: strip comments (as long as "#" is your
14 comment character), skip blank lines, join adjacent lines by
21 spans multiple physical lines. Also provides 'unreadline()' for
22 implementing line-at-a-time lookahead.
26 TextFile (filename=None, file=None, **options)
28 It bombs (RuntimeError) if both 'filename' and 'file' are None;
29 'filename' should be a string, and 'file' a file object (or
32 can include it in warning messages. If 'file' is not supplied,
38 strip from "#" to end-of-line, as well as any whitespace
39 leading up to the "#" -- unless it is escaped by a backslash
46 skip lines that are empty *after* stripping comments and
48 then some lines may consist of solely whitespace: these will
51 if a backslash is the last non-newline character on a 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
57 strip leading whitespace from lines that are joined to their
60 error handler used to decode the file content
63 semantics of 'readline()' must differ from those of the builtin file
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
78 def __init__(self, filename=None, file=None, **options): argument
80 (a string) and 'file' (a file-like object) must be supplied.
83 if filename is None and file is None:
84 raise RuntimeError("you must supply either or both of 'filename' and 'file'")
86 # set values for all options -- either from client option hash
99 if file is None:
103 self.file = file
104 self.current_line = 0 # assuming that file is at BOF!
106 # 'linebuf' is a stack of lines that will be emptied before we
107 # actually read from the file; it's only populated by an
112 """Open a new file named 'filename'. This overrides both the
113 'filename' and 'file' arguments to the constructor."""
115 self.file = io.open(self.filename, 'r', errors=self.errors)
119 """Close the current file and forget everything we know about it
121 file = self.file
122 self.file = None
125 file.close()
133 outmsg.append("lines %d-%d: " % tuple(line))
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
148 range of physical lines, or an integer for a single physical
153 """Read and return a single logical line from the current file (or
154 from an internal buffer if lines have previously been "unread"
156 may involve reading multiple physical lines concatenated into a
159 line(s) just read. Returns None on end-of-file, since the empty
162 # If any "unread" lines waiting in 'linebuf', return the top
163 # one. (We don't actually buffer read-ahead data -- lines only
167 line = self.linebuf[-1]
168 del self.linebuf[-1]
175 line = self.file.readline()
183 # is not preceded by "\", then it starts a comment --
190 if pos == -1: # no "#" -- no comments
193 # It's definitely a comment -- either "#" is the first
195 elif pos == 0 or line[pos-1] != "\\":
197 # the job of a later step (rstrip_ws) to remove it --
202 eol = (line[-1] == '\n') and '\n' or ''
206 # *now*, before we try to join it to 'buildup_line' --
219 # oops: end of file
222 "end-of-file")
261 if line[-1] == '\\':
262 buildup_line = line[:-1]
265 if line[-2:] == '\\\n':
266 buildup_line = line[0:-2] + '\n'
273 """Read and return the list of all logical lines remaining in the
274 current file."""
275 lines = []
279 return lines
280 lines.append(line)
285 a parser with line-at-a-time lookahead."""