diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-01-14 23:47:14 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-01-14 23:47:14 (GMT) |
commit | 07e99cb77406e1bc84606f49b743e41b0de8a6d5 (patch) | |
tree | b922cda3a970bffa797269fd550f8d8b032afe5b /Lib/multifile.py | |
parent | 88869f9787cd4ceb2298e4b13980beb057687824 (diff) | |
download | cpython-07e99cb77406e1bc84606f49b743e41b0de8a6d5.zip cpython-07e99cb77406e1bc84606f49b743e41b0de8a6d5.tar.gz cpython-07e99cb77406e1bc84606f49b743e41b0de8a6d5.tar.bz2 |
Whitespace normalization.
Diffstat (limited to 'Lib/multifile.py')
-rw-r--r-- | Lib/multifile.py | 260 |
1 files changed, 130 insertions, 130 deletions
diff --git a/Lib/multifile.py b/Lib/multifile.py index 0b51b55..38a5a46 100644 --- a/Lib/multifile.py +++ b/Lib/multifile.py @@ -13,8 +13,8 @@ fp = MultiFile(real_fp) "read some lines from fp" fp.push(separator) while 1: - "read lines from fp until it returns an empty string" (A) - if not fp.next(): break + "read lines from fp until it returns an empty string" (A) + if not fp.next(): break fp.pop() "read remaining lines from fp until it returns an empty string" @@ -31,134 +31,134 @@ import sys import string class Error(Exception): - pass + pass class MultiFile: - seekable = 0 - - def __init__(self, fp, seekable=1): - self.fp = fp - self.stack = [] # Grows down - self.level = 0 - self.last = 0 - if seekable: - self.seekable = 1 - self.start = self.fp.tell() - self.posstack = [] # Grows down - - def tell(self): - if self.level > 0: - return self.lastpos - return self.fp.tell() - self.start - - def seek(self, pos, whence=0): - here = self.tell() - if whence: - if whence == 1: - pos = pos + here - elif whence == 2: - if self.level > 0: - pos = pos + self.lastpos - else: - raise Error, "can't use whence=2 yet" - if not 0 <= pos <= here or \ - self.level > 0 and pos > self.lastpos: - raise Error, 'bad MultiFile.seek() call' - self.fp.seek(pos + self.start) - self.level = 0 - self.last = 0 - - def readline(self): - if self.level > 0: - return '' - line = self.fp.readline() - # Real EOF? - if not line: - self.level = len(self.stack) - self.last = (self.level > 0) - if self.last: - raise Error, 'sudden EOF in MultiFile.readline()' - return '' - assert self.level == 0 - # Fast check to see if this is just data - if self.is_data(line): - return line - else: - # Ignore trailing whitespace on marker lines - k = len(line) - 1 - while line[k] in string.whitespace: - k = k - 1 - marker = line[:k+1] - # No? OK, try to match a boundary. - # Return the line (unstripped) if we don't. - for i in range(len(self.stack)): - sep = self.stack[i] - if marker == self.section_divider(sep): - self.last = 0 - break - elif marker == self.end_marker(sep): - self.last = 1 - break - else: - return line - # We only get here if we see a section divider or EOM line - if self.seekable: - self.lastpos = self.tell() - len(line) - self.level = i+1 - if self.level > 1: - raise Error,'Missing endmarker in MultiFile.readline()' - return '' - - def readlines(self): - list = [] - while 1: - line = self.readline() - if not line: break - list.append(line) - return list - - def read(self): # Note: no size argument -- read until EOF only! - return string.joinfields(self.readlines(), '') - - def next(self): - while self.readline(): pass - if self.level > 1 or self.last: - return 0 - self.level = 0 - self.last = 0 - if self.seekable: - self.start = self.fp.tell() - return 1 - - def push(self, sep): - if self.level > 0: - raise Error, 'bad MultiFile.push() call' - self.stack.insert(0, sep) - if self.seekable: - self.posstack.insert(0, self.start) - self.start = self.fp.tell() - - def pop(self): - if self.stack == []: - raise Error, 'bad MultiFile.pop() call' - if self.level <= 1: - self.last = 0 - else: - abslastpos = self.lastpos + self.start - self.level = max(0, self.level - 1) - del self.stack[0] - if self.seekable: - self.start = self.posstack[0] - del self.posstack[0] - if self.level > 0: - self.lastpos = abslastpos - self.start - - def is_data(self, line): - return line[:2] != '--' - - def section_divider(self, str): - return "--" + str - - def end_marker(self, str): - return "--" + str + "--" + seekable = 0 + + def __init__(self, fp, seekable=1): + self.fp = fp + self.stack = [] # Grows down + self.level = 0 + self.last = 0 + if seekable: + self.seekable = 1 + self.start = self.fp.tell() + self.posstack = [] # Grows down + + def tell(self): + if self.level > 0: + return self.lastpos + return self.fp.tell() - self.start + + def seek(self, pos, whence=0): + here = self.tell() + if whence: + if whence == 1: + pos = pos + here + elif whence == 2: + if self.level > 0: + pos = pos + self.lastpos + else: + raise Error, "can't use whence=2 yet" + if not 0 <= pos <= here or \ + self.level > 0 and pos > self.lastpos: + raise Error, 'bad MultiFile.seek() call' + self.fp.seek(pos + self.start) + self.level = 0 + self.last = 0 + + def readline(self): + if self.level > 0: + return '' + line = self.fp.readline() + # Real EOF? + if not line: + self.level = len(self.stack) + self.last = (self.level > 0) + if self.last: + raise Error, 'sudden EOF in MultiFile.readline()' + return '' + assert self.level == 0 + # Fast check to see if this is just data + if self.is_data(line): + return line + else: + # Ignore trailing whitespace on marker lines + k = len(line) - 1 + while line[k] in string.whitespace: + k = k - 1 + marker = line[:k+1] + # No? OK, try to match a boundary. + # Return the line (unstripped) if we don't. + for i in range(len(self.stack)): + sep = self.stack[i] + if marker == self.section_divider(sep): + self.last = 0 + break + elif marker == self.end_marker(sep): + self.last = 1 + break + else: + return line + # We only get here if we see a section divider or EOM line + if self.seekable: + self.lastpos = self.tell() - len(line) + self.level = i+1 + if self.level > 1: + raise Error,'Missing endmarker in MultiFile.readline()' + return '' + + def readlines(self): + list = [] + while 1: + line = self.readline() + if not line: break + list.append(line) + return list + + def read(self): # Note: no size argument -- read until EOF only! + return string.joinfields(self.readlines(), '') + + def next(self): + while self.readline(): pass + if self.level > 1 or self.last: + return 0 + self.level = 0 + self.last = 0 + if self.seekable: + self.start = self.fp.tell() + return 1 + + def push(self, sep): + if self.level > 0: + raise Error, 'bad MultiFile.push() call' + self.stack.insert(0, sep) + if self.seekable: + self.posstack.insert(0, self.start) + self.start = self.fp.tell() + + def pop(self): + if self.stack == []: + raise Error, 'bad MultiFile.pop() call' + if self.level <= 1: + self.last = 0 + else: + abslastpos = self.lastpos + self.start + self.level = max(0, self.level - 1) + del self.stack[0] + if self.seekable: + self.start = self.posstack[0] + del self.posstack[0] + if self.level > 0: + self.lastpos = abslastpos - self.start + + def is_data(self, line): + return line[:2] != '--' + + def section_divider(self, str): + return "--" + str + + def end_marker(self, str): + return "--" + str + "--" |