Commit 3ad9dd55 authored by Guido van Rossum's avatar Guido van Rossum

Improved by Eric Raymond.

parent 950ff292
...@@ -18,9 +18,6 @@ ...@@ -18,9 +18,6 @@
# #
# The latter sequence may be used recursively at (A). # The latter sequence may be used recursively at (A).
# It is also allowed to use multiple push()...pop() sequences. # It is also allowed to use multiple push()...pop() sequences.
# Note that if a nested multipart message is terminated by a separator
# for an outer message, this is not reported, even though it is really
# illegal input.
# #
# If seekable is given as 0, the class code will not do the bookeeping # If seekable is given as 0, the class code will not do the bookeeping
# it normally attempts in order to make seeks relative to the beginning of the # it normally attempts in order to make seeks relative to the beginning of the
...@@ -30,13 +27,12 @@ ...@@ -30,13 +27,12 @@
import sys import sys
import string import string
err = sys.stderr.write
Error = 'multifile.Error' Error = 'multifile.Error'
class MultiFile: class MultiFile:
# #
seekable = 0 seekable = 0
#
def __init__(self, fp, seekable=1): def __init__(self, fp, seekable=1):
self.fp = fp self.fp = fp
self.stack = [] # Grows down self.stack = [] # Grows down
...@@ -70,37 +66,44 @@ class MultiFile: ...@@ -70,37 +66,44 @@ class MultiFile:
self.last = 0 self.last = 0
# #
def readline(self): def readline(self):
if self.level > 0: return '' if self.level > 0:
return ''
line = self.fp.readline() line = self.fp.readline()
# Real EOF?
if not line: if not line:
self.level = len(self.stack) self.level = len(self.stack)
self.last = (self.level > 0) self.last = (self.level > 0)
if self.last: if self.last:
err('*** Sudden EOF in MultiFile.readline()\n') raise Error, 'sudden EOF in MultiFile.readline()'
return '' return ''
if line[:2] <> '--': return line assert self.level == 0
n = len(line) # Fast check to see if this is just data
k = n if self.is_data(line):
while k > 0 and line[k-1] in string.whitespace: k = k-1 return line
mark = line[2:k] else:
if mark[-2:] == '--': mark1 = mark[:-2] # Ignore trailing whitespace on marker lines
else: mark1 = None 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)): for i in range(len(self.stack)):
sep = self.stack[i] sep = self.stack[i]
if sep == mark: if marker == self.section_divider(sep):
self.last = 0 self.last = 0
break break
elif mark1 <> None and sep == mark1: elif marker == self.end_marker(sep):
self.last = 1 self.last = 1
break break
else: else:
return line return line
# Get here after break out of loop # We only get here if we see a section divider or EOM line
if self.seekable: if self.seekable:
self.lastpos = self.tell() - len(line) self.lastpos = self.tell() - len(line)
self.level = i+1 self.level = i+1
if self.level > 1: if self.level > 1:
err('*** Missing endmarker in MultiFile.readline()\n') raise Error,'Missing endmarker in MultiFile.readline()'
return '' return ''
# #
def readlines(self): def readlines(self):
...@@ -147,3 +150,11 @@ class MultiFile: ...@@ -147,3 +150,11 @@ class MultiFile:
if self.level > 0: if self.level > 0:
self.lastpos = abslastpos - self.start 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 + "--"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment