Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../email
File: feedparser.py
# Copyright (C) 2004-2006 Python Software Foundation
[0] Fix | Delete
# Authors: Baxter, Wouters and Warsaw
[1] Fix | Delete
# Contact: email-sig@python.org
[2] Fix | Delete
[3] Fix | Delete
"""FeedParser - An email feed parser.
[4] Fix | Delete
[5] Fix | Delete
The feed parser implements an interface for incrementally parsing an email
[6] Fix | Delete
message, line by line. This has advantages for certain applications, such as
[7] Fix | Delete
those reading email messages off a socket.
[8] Fix | Delete
[9] Fix | Delete
FeedParser.feed() is the primary interface for pushing new data into the
[10] Fix | Delete
parser. It returns when there's nothing more it can do with the available
[11] Fix | Delete
data. When you have no more data to push into the parser, call .close().
[12] Fix | Delete
This completes the parsing and returns the root message object.
[13] Fix | Delete
[14] Fix | Delete
The other advantage of this parser is that it will never raise a parsing
[15] Fix | Delete
exception. Instead, when it finds something unexpected, it adds a 'defect' to
[16] Fix | Delete
the current message. Defects are just instances that live on the message
[17] Fix | Delete
object's .defects attribute.
[18] Fix | Delete
"""
[19] Fix | Delete
[20] Fix | Delete
__all__ = ['FeedParser']
[21] Fix | Delete
[22] Fix | Delete
import re
[23] Fix | Delete
[24] Fix | Delete
from email import errors
[25] Fix | Delete
from email import message
[26] Fix | Delete
[27] Fix | Delete
NLCRE = re.compile('\r\n|\r|\n')
[28] Fix | Delete
NLCRE_bol = re.compile('(\r\n|\r|\n)')
[29] Fix | Delete
NLCRE_eol = re.compile('(\r\n|\r|\n)\Z')
[30] Fix | Delete
NLCRE_crack = re.compile('(\r\n|\r|\n)')
[31] Fix | Delete
# RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character
[32] Fix | Delete
# except controls, SP, and ":".
[33] Fix | Delete
headerRE = re.compile(r'^(From |[\041-\071\073-\176]{1,}:|[\t ])')
[34] Fix | Delete
EMPTYSTRING = ''
[35] Fix | Delete
NL = '\n'
[36] Fix | Delete
[37] Fix | Delete
NeedMoreData = object()
[38] Fix | Delete
[39] Fix | Delete
[40] Fix | Delete
[41] Fix | Delete
class BufferedSubFile(object):
[42] Fix | Delete
"""A file-ish object that can have new data loaded into it.
[43] Fix | Delete
[44] Fix | Delete
You can also push and pop line-matching predicates onto a stack. When the
[45] Fix | Delete
current predicate matches the current line, a false EOF response
[46] Fix | Delete
(i.e. empty string) is returned instead. This lets the parser adhere to a
[47] Fix | Delete
simple abstraction -- it parses until EOF closes the current message.
[48] Fix | Delete
"""
[49] Fix | Delete
def __init__(self):
[50] Fix | Delete
# Chunks of the last partial line pushed into this object.
[51] Fix | Delete
self._partial = []
[52] Fix | Delete
# The list of full, pushed lines, in reverse order
[53] Fix | Delete
self._lines = []
[54] Fix | Delete
# The stack of false-EOF checking predicates.
[55] Fix | Delete
self._eofstack = []
[56] Fix | Delete
# A flag indicating whether the file has been closed or not.
[57] Fix | Delete
self._closed = False
[58] Fix | Delete
[59] Fix | Delete
def push_eof_matcher(self, pred):
[60] Fix | Delete
self._eofstack.append(pred)
[61] Fix | Delete
[62] Fix | Delete
def pop_eof_matcher(self):
[63] Fix | Delete
return self._eofstack.pop()
[64] Fix | Delete
[65] Fix | Delete
def close(self):
[66] Fix | Delete
# Don't forget any trailing partial line.
[67] Fix | Delete
self.pushlines(''.join(self._partial).splitlines(True))
[68] Fix | Delete
self._partial = []
[69] Fix | Delete
self._closed = True
[70] Fix | Delete
[71] Fix | Delete
def readline(self):
[72] Fix | Delete
if not self._lines:
[73] Fix | Delete
if self._closed:
[74] Fix | Delete
return ''
[75] Fix | Delete
return NeedMoreData
[76] Fix | Delete
# Pop the line off the stack and see if it matches the current
[77] Fix | Delete
# false-EOF predicate.
[78] Fix | Delete
line = self._lines.pop()
[79] Fix | Delete
# RFC 2046, section 5.1.2 requires us to recognize outer level
[80] Fix | Delete
# boundaries at any level of inner nesting. Do this, but be sure it's
[81] Fix | Delete
# in the order of most to least nested.
[82] Fix | Delete
for ateof in self._eofstack[::-1]:
[83] Fix | Delete
if ateof(line):
[84] Fix | Delete
# We're at the false EOF. But push the last line back first.
[85] Fix | Delete
self._lines.append(line)
[86] Fix | Delete
return ''
[87] Fix | Delete
return line
[88] Fix | Delete
[89] Fix | Delete
def unreadline(self, line):
[90] Fix | Delete
# Let the consumer push a line back into the buffer.
[91] Fix | Delete
assert line is not NeedMoreData
[92] Fix | Delete
self._lines.append(line)
[93] Fix | Delete
[94] Fix | Delete
def push(self, data):
[95] Fix | Delete
"""Push some new data into this object."""
[96] Fix | Delete
# Crack into lines, but preserve the linesep characters on the end of each
[97] Fix | Delete
parts = data.splitlines(True)
[98] Fix | Delete
[99] Fix | Delete
if not parts or not parts[0].endswith(('\n', '\r')):
[100] Fix | Delete
# No new complete lines, so just accumulate partials
[101] Fix | Delete
self._partial += parts
[102] Fix | Delete
return
[103] Fix | Delete
[104] Fix | Delete
if self._partial:
[105] Fix | Delete
# If there are previous leftovers, complete them now
[106] Fix | Delete
self._partial.append(parts[0])
[107] Fix | Delete
parts[0:1] = ''.join(self._partial).splitlines(True)
[108] Fix | Delete
del self._partial[:]
[109] Fix | Delete
[110] Fix | Delete
# If the last element of the list does not end in a newline, then treat
[111] Fix | Delete
# it as a partial line. We only check for '\n' here because a line
[112] Fix | Delete
# ending with '\r' might be a line that was split in the middle of a
[113] Fix | Delete
# '\r\n' sequence (see bugs 1555570 and 1721862).
[114] Fix | Delete
if not parts[-1].endswith('\n'):
[115] Fix | Delete
self._partial = [parts.pop()]
[116] Fix | Delete
self.pushlines(parts)
[117] Fix | Delete
[118] Fix | Delete
def pushlines(self, lines):
[119] Fix | Delete
# Reverse and insert at the front of the lines.
[120] Fix | Delete
self._lines[:0] = lines[::-1]
[121] Fix | Delete
[122] Fix | Delete
def is_closed(self):
[123] Fix | Delete
return self._closed
[124] Fix | Delete
[125] Fix | Delete
def __iter__(self):
[126] Fix | Delete
return self
[127] Fix | Delete
[128] Fix | Delete
def next(self):
[129] Fix | Delete
line = self.readline()
[130] Fix | Delete
if line == '':
[131] Fix | Delete
raise StopIteration
[132] Fix | Delete
return line
[133] Fix | Delete
[134] Fix | Delete
[135] Fix | Delete
[136] Fix | Delete
class FeedParser:
[137] Fix | Delete
"""A feed-style parser of email."""
[138] Fix | Delete
[139] Fix | Delete
def __init__(self, _factory=message.Message):
[140] Fix | Delete
"""_factory is called with no arguments to create a new message obj"""
[141] Fix | Delete
self._factory = _factory
[142] Fix | Delete
self._input = BufferedSubFile()
[143] Fix | Delete
self._msgstack = []
[144] Fix | Delete
self._parse = self._parsegen().next
[145] Fix | Delete
self._cur = None
[146] Fix | Delete
self._last = None
[147] Fix | Delete
self._headersonly = False
[148] Fix | Delete
[149] Fix | Delete
# Non-public interface for supporting Parser's headersonly flag
[150] Fix | Delete
def _set_headersonly(self):
[151] Fix | Delete
self._headersonly = True
[152] Fix | Delete
[153] Fix | Delete
def feed(self, data):
[154] Fix | Delete
"""Push more data into the parser."""
[155] Fix | Delete
self._input.push(data)
[156] Fix | Delete
self._call_parse()
[157] Fix | Delete
[158] Fix | Delete
def _call_parse(self):
[159] Fix | Delete
try:
[160] Fix | Delete
self._parse()
[161] Fix | Delete
except StopIteration:
[162] Fix | Delete
pass
[163] Fix | Delete
[164] Fix | Delete
def close(self):
[165] Fix | Delete
"""Parse all remaining data and return the root message object."""
[166] Fix | Delete
self._input.close()
[167] Fix | Delete
self._call_parse()
[168] Fix | Delete
root = self._pop_message()
[169] Fix | Delete
assert not self._msgstack
[170] Fix | Delete
# Look for final set of defects
[171] Fix | Delete
if root.get_content_maintype() == 'multipart' \
[172] Fix | Delete
and not root.is_multipart():
[173] Fix | Delete
root.defects.append(errors.MultipartInvariantViolationDefect())
[174] Fix | Delete
return root
[175] Fix | Delete
[176] Fix | Delete
def _new_message(self):
[177] Fix | Delete
msg = self._factory()
[178] Fix | Delete
if self._cur and self._cur.get_content_type() == 'multipart/digest':
[179] Fix | Delete
msg.set_default_type('message/rfc822')
[180] Fix | Delete
if self._msgstack:
[181] Fix | Delete
self._msgstack[-1].attach(msg)
[182] Fix | Delete
self._msgstack.append(msg)
[183] Fix | Delete
self._cur = msg
[184] Fix | Delete
self._last = msg
[185] Fix | Delete
[186] Fix | Delete
def _pop_message(self):
[187] Fix | Delete
retval = self._msgstack.pop()
[188] Fix | Delete
if self._msgstack:
[189] Fix | Delete
self._cur = self._msgstack[-1]
[190] Fix | Delete
else:
[191] Fix | Delete
self._cur = None
[192] Fix | Delete
return retval
[193] Fix | Delete
[194] Fix | Delete
def _parsegen(self):
[195] Fix | Delete
# Create a new message and start by parsing headers.
[196] Fix | Delete
self._new_message()
[197] Fix | Delete
headers = []
[198] Fix | Delete
# Collect the headers, searching for a line that doesn't match the RFC
[199] Fix | Delete
# 2822 header or continuation pattern (including an empty line).
[200] Fix | Delete
for line in self._input:
[201] Fix | Delete
if line is NeedMoreData:
[202] Fix | Delete
yield NeedMoreData
[203] Fix | Delete
continue
[204] Fix | Delete
if not headerRE.match(line):
[205] Fix | Delete
# If we saw the RFC defined header/body separator
[206] Fix | Delete
# (i.e. newline), just throw it away. Otherwise the line is
[207] Fix | Delete
# part of the body so push it back.
[208] Fix | Delete
if not NLCRE.match(line):
[209] Fix | Delete
self._input.unreadline(line)
[210] Fix | Delete
break
[211] Fix | Delete
headers.append(line)
[212] Fix | Delete
# Done with the headers, so parse them and figure out what we're
[213] Fix | Delete
# supposed to see in the body of the message.
[214] Fix | Delete
self._parse_headers(headers)
[215] Fix | Delete
# Headers-only parsing is a backwards compatibility hack, which was
[216] Fix | Delete
# necessary in the older parser, which could raise errors. All
[217] Fix | Delete
# remaining lines in the input are thrown into the message body.
[218] Fix | Delete
if self._headersonly:
[219] Fix | Delete
lines = []
[220] Fix | Delete
while True:
[221] Fix | Delete
line = self._input.readline()
[222] Fix | Delete
if line is NeedMoreData:
[223] Fix | Delete
yield NeedMoreData
[224] Fix | Delete
continue
[225] Fix | Delete
if line == '':
[226] Fix | Delete
break
[227] Fix | Delete
lines.append(line)
[228] Fix | Delete
self._cur.set_payload(EMPTYSTRING.join(lines))
[229] Fix | Delete
return
[230] Fix | Delete
if self._cur.get_content_type() == 'message/delivery-status':
[231] Fix | Delete
# message/delivery-status contains blocks of headers separated by
[232] Fix | Delete
# a blank line. We'll represent each header block as a separate
[233] Fix | Delete
# nested message object, but the processing is a bit different
[234] Fix | Delete
# than standard message/* types because there is no body for the
[235] Fix | Delete
# nested messages. A blank line separates the subparts.
[236] Fix | Delete
while True:
[237] Fix | Delete
self._input.push_eof_matcher(NLCRE.match)
[238] Fix | Delete
for retval in self._parsegen():
[239] Fix | Delete
if retval is NeedMoreData:
[240] Fix | Delete
yield NeedMoreData
[241] Fix | Delete
continue
[242] Fix | Delete
break
[243] Fix | Delete
msg = self._pop_message()
[244] Fix | Delete
# We need to pop the EOF matcher in order to tell if we're at
[245] Fix | Delete
# the end of the current file, not the end of the last block
[246] Fix | Delete
# of message headers.
[247] Fix | Delete
self._input.pop_eof_matcher()
[248] Fix | Delete
# The input stream must be sitting at the newline or at the
[249] Fix | Delete
# EOF. We want to see if we're at the end of this subpart, so
[250] Fix | Delete
# first consume the blank line, then test the next line to see
[251] Fix | Delete
# if we're at this subpart's EOF.
[252] Fix | Delete
while True:
[253] Fix | Delete
line = self._input.readline()
[254] Fix | Delete
if line is NeedMoreData:
[255] Fix | Delete
yield NeedMoreData
[256] Fix | Delete
continue
[257] Fix | Delete
break
[258] Fix | Delete
while True:
[259] Fix | Delete
line = self._input.readline()
[260] Fix | Delete
if line is NeedMoreData:
[261] Fix | Delete
yield NeedMoreData
[262] Fix | Delete
continue
[263] Fix | Delete
break
[264] Fix | Delete
if line == '':
[265] Fix | Delete
break
[266] Fix | Delete
# Not at EOF so this is a line we're going to need.
[267] Fix | Delete
self._input.unreadline(line)
[268] Fix | Delete
return
[269] Fix | Delete
if self._cur.get_content_maintype() == 'message':
[270] Fix | Delete
# The message claims to be a message/* type, then what follows is
[271] Fix | Delete
# another RFC 2822 message.
[272] Fix | Delete
for retval in self._parsegen():
[273] Fix | Delete
if retval is NeedMoreData:
[274] Fix | Delete
yield NeedMoreData
[275] Fix | Delete
continue
[276] Fix | Delete
break
[277] Fix | Delete
self._pop_message()
[278] Fix | Delete
return
[279] Fix | Delete
if self._cur.get_content_maintype() == 'multipart':
[280] Fix | Delete
boundary = self._cur.get_boundary()
[281] Fix | Delete
if boundary is None:
[282] Fix | Delete
# The message /claims/ to be a multipart but it has not
[283] Fix | Delete
# defined a boundary. That's a problem which we'll handle by
[284] Fix | Delete
# reading everything until the EOF and marking the message as
[285] Fix | Delete
# defective.
[286] Fix | Delete
self._cur.defects.append(errors.NoBoundaryInMultipartDefect())
[287] Fix | Delete
lines = []
[288] Fix | Delete
for line in self._input:
[289] Fix | Delete
if line is NeedMoreData:
[290] Fix | Delete
yield NeedMoreData
[291] Fix | Delete
continue
[292] Fix | Delete
lines.append(line)
[293] Fix | Delete
self._cur.set_payload(EMPTYSTRING.join(lines))
[294] Fix | Delete
return
[295] Fix | Delete
# Create a line match predicate which matches the inter-part
[296] Fix | Delete
# boundary as well as the end-of-multipart boundary. Don't push
[297] Fix | Delete
# this onto the input stream until we've scanned past the
[298] Fix | Delete
# preamble.
[299] Fix | Delete
separator = '--' + boundary
[300] Fix | Delete
boundaryre = re.compile(
[301] Fix | Delete
'(?P<sep>' + re.escape(separator) +
[302] Fix | Delete
r')(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$')
[303] Fix | Delete
capturing_preamble = True
[304] Fix | Delete
preamble = []
[305] Fix | Delete
linesep = False
[306] Fix | Delete
while True:
[307] Fix | Delete
line = self._input.readline()
[308] Fix | Delete
if line is NeedMoreData:
[309] Fix | Delete
yield NeedMoreData
[310] Fix | Delete
continue
[311] Fix | Delete
if line == '':
[312] Fix | Delete
break
[313] Fix | Delete
mo = boundaryre.match(line)
[314] Fix | Delete
if mo:
[315] Fix | Delete
# If we're looking at the end boundary, we're done with
[316] Fix | Delete
# this multipart. If there was a newline at the end of
[317] Fix | Delete
# the closing boundary, then we need to initialize the
[318] Fix | Delete
# epilogue with the empty string (see below).
[319] Fix | Delete
if mo.group('end'):
[320] Fix | Delete
linesep = mo.group('linesep')
[321] Fix | Delete
break
[322] Fix | Delete
# We saw an inter-part boundary. Were we in the preamble?
[323] Fix | Delete
if capturing_preamble:
[324] Fix | Delete
if preamble:
[325] Fix | Delete
# According to RFC 2046, the last newline belongs
[326] Fix | Delete
# to the boundary.
[327] Fix | Delete
lastline = preamble[-1]
[328] Fix | Delete
eolmo = NLCRE_eol.search(lastline)
[329] Fix | Delete
if eolmo:
[330] Fix | Delete
preamble[-1] = lastline[:-len(eolmo.group(0))]
[331] Fix | Delete
self._cur.preamble = EMPTYSTRING.join(preamble)
[332] Fix | Delete
capturing_preamble = False
[333] Fix | Delete
self._input.unreadline(line)
[334] Fix | Delete
continue
[335] Fix | Delete
# We saw a boundary separating two parts. Consume any
[336] Fix | Delete
# multiple boundary lines that may be following. Our
[337] Fix | Delete
# interpretation of RFC 2046 BNF grammar does not produce
[338] Fix | Delete
# body parts within such double boundaries.
[339] Fix | Delete
while True:
[340] Fix | Delete
line = self._input.readline()
[341] Fix | Delete
if line is NeedMoreData:
[342] Fix | Delete
yield NeedMoreData
[343] Fix | Delete
continue
[344] Fix | Delete
mo = boundaryre.match(line)
[345] Fix | Delete
if not mo:
[346] Fix | Delete
self._input.unreadline(line)
[347] Fix | Delete
break
[348] Fix | Delete
# Recurse to parse this subpart; the input stream points
[349] Fix | Delete
# at the subpart's first line.
[350] Fix | Delete
self._input.push_eof_matcher(boundaryre.match)
[351] Fix | Delete
for retval in self._parsegen():
[352] Fix | Delete
if retval is NeedMoreData:
[353] Fix | Delete
yield NeedMoreData
[354] Fix | Delete
continue
[355] Fix | Delete
break
[356] Fix | Delete
# Because of RFC 2046, the newline preceding the boundary
[357] Fix | Delete
# separator actually belongs to the boundary, not the
[358] Fix | Delete
# previous subpart's payload (or epilogue if the previous
[359] Fix | Delete
# part is a multipart).
[360] Fix | Delete
if self._last.get_content_maintype() == 'multipart':
[361] Fix | Delete
epilogue = self._last.epilogue
[362] Fix | Delete
if epilogue == '':
[363] Fix | Delete
self._last.epilogue = None
[364] Fix | Delete
elif epilogue is not None:
[365] Fix | Delete
mo = NLCRE_eol.search(epilogue)
[366] Fix | Delete
if mo:
[367] Fix | Delete
end = len(mo.group(0))
[368] Fix | Delete
self._last.epilogue = epilogue[:-end]
[369] Fix | Delete
else:
[370] Fix | Delete
payload = self._last.get_payload()
[371] Fix | Delete
if isinstance(payload, basestring):
[372] Fix | Delete
mo = NLCRE_eol.search(payload)
[373] Fix | Delete
if mo:
[374] Fix | Delete
payload = payload[:-len(mo.group(0))]
[375] Fix | Delete
self._last.set_payload(payload)
[376] Fix | Delete
self._input.pop_eof_matcher()
[377] Fix | Delete
self._pop_message()
[378] Fix | Delete
# Set the multipart up for newline cleansing, which will
[379] Fix | Delete
# happen if we're in a nested multipart.
[380] Fix | Delete
self._last = self._cur
[381] Fix | Delete
else:
[382] Fix | Delete
# I think we must be in the preamble
[383] Fix | Delete
assert capturing_preamble
[384] Fix | Delete
preamble.append(line)
[385] Fix | Delete
# We've seen either the EOF or the end boundary. If we're still
[386] Fix | Delete
# capturing the preamble, we never saw the start boundary. Note
[387] Fix | Delete
# that as a defect and store the captured text as the payload.
[388] Fix | Delete
# Everything from here to the EOF is epilogue.
[389] Fix | Delete
if capturing_preamble:
[390] Fix | Delete
self._cur.defects.append(errors.StartBoundaryNotFoundDefect())
[391] Fix | Delete
self._cur.set_payload(EMPTYSTRING.join(preamble))
[392] Fix | Delete
epilogue = []
[393] Fix | Delete
for line in self._input:
[394] Fix | Delete
if line is NeedMoreData:
[395] Fix | Delete
yield NeedMoreData
[396] Fix | Delete
continue
[397] Fix | Delete
self._cur.epilogue = EMPTYSTRING.join(epilogue)
[398] Fix | Delete
return
[399] Fix | Delete
# If the end boundary ended in a newline, we'll need to make sure
[400] Fix | Delete
# the epilogue isn't None
[401] Fix | Delete
if linesep:
[402] Fix | Delete
epilogue = ['']
[403] Fix | Delete
else:
[404] Fix | Delete
epilogue = []
[405] Fix | Delete
for line in self._input:
[406] Fix | Delete
if line is NeedMoreData:
[407] Fix | Delete
yield NeedMoreData
[408] Fix | Delete
continue
[409] Fix | Delete
epilogue.append(line)
[410] Fix | Delete
# Any CRLF at the front of the epilogue is not technically part of
[411] Fix | Delete
# the epilogue. Also, watch out for an empty string epilogue,
[412] Fix | Delete
# which means a single newline.
[413] Fix | Delete
if epilogue:
[414] Fix | Delete
firstline = epilogue[0]
[415] Fix | Delete
bolmo = NLCRE_bol.match(firstline)
[416] Fix | Delete
if bolmo:
[417] Fix | Delete
epilogue[0] = firstline[len(bolmo.group(0)):]
[418] Fix | Delete
self._cur.epilogue = EMPTYSTRING.join(epilogue)
[419] Fix | Delete
return
[420] Fix | Delete
# Otherwise, it's some non-multipart type, so the entire rest of the
[421] Fix | Delete
# file contents becomes the payload.
[422] Fix | Delete
lines = []
[423] Fix | Delete
for line in self._input:
[424] Fix | Delete
if line is NeedMoreData:
[425] Fix | Delete
yield NeedMoreData
[426] Fix | Delete
continue
[427] Fix | Delete
lines.append(line)
[428] Fix | Delete
self._cur.set_payload(EMPTYSTRING.join(lines))
[429] Fix | Delete
[430] Fix | Delete
def _parse_headers(self, lines):
[431] Fix | Delete
# Passed a list of lines that make up the headers for the current msg
[432] Fix | Delete
lastheader = ''
[433] Fix | Delete
lastvalue = []
[434] Fix | Delete
for lineno, line in enumerate(lines):
[435] Fix | Delete
# Check for continuation
[436] Fix | Delete
if line[0] in ' \t':
[437] Fix | Delete
if not lastheader:
[438] Fix | Delete
# The first line of the headers was a continuation. This
[439] Fix | Delete
# is illegal, so let's note the defect, store the illegal
[440] Fix | Delete
# line, and ignore it for purposes of headers.
[441] Fix | Delete
defect = errors.FirstHeaderLineIsContinuationDefect(line)
[442] Fix | Delete
self._cur.defects.append(defect)
[443] Fix | Delete
continue
[444] Fix | Delete
lastvalue.append(line)
[445] Fix | Delete
continue
[446] Fix | Delete
if lastheader:
[447] Fix | Delete
# XXX reconsider the joining of folded lines
[448] Fix | Delete
lhdr = EMPTYSTRING.join(lastvalue)[:-1].rstrip('\r\n')
[449] Fix | Delete
self._cur[lastheader] = lhdr
[450] Fix | Delete
lastheader, lastvalue = '', []
[451] Fix | Delete
# Check for envelope header, i.e. unix-from
[452] Fix | Delete
if line.startswith('From '):
[453] Fix | Delete
if lineno == 0:
[454] Fix | Delete
# Strip off the trailing newline
[455] Fix | Delete
mo = NLCRE_eol.search(line)
[456] Fix | Delete
if mo:
[457] Fix | Delete
line = line[:-len(mo.group(0))]
[458] Fix | Delete
self._cur.set_unixfrom(line)
[459] Fix | Delete
continue
[460] Fix | Delete
elif lineno == len(lines) - 1:
[461] Fix | Delete
# Something looking like a unix-from at the end - it's
[462] Fix | Delete
# probably the first line of the body, so push back the
[463] Fix | Delete
# line and stop.
[464] Fix | Delete
self._input.unreadline(line)
[465] Fix | Delete
return
[466] Fix | Delete
else:
[467] Fix | Delete
# Weirdly placed unix-from line. Note this as a defect
[468] Fix | Delete
# and ignore it.
[469] Fix | Delete
defect = errors.MisplacedEnvelopeHeaderDefect(line)
[470] Fix | Delete
self._cur.defects.append(defect)
[471] Fix | Delete
continue
[472] Fix | Delete
# Split the line on the colon separating field name from value.
[473] Fix | Delete
i = line.find(':')
[474] Fix | Delete
if i < 0:
[475] Fix | Delete
defect = errors.MalformedHeaderDefect(line)
[476] Fix | Delete
self._cur.defects.append(defect)
[477] Fix | Delete
continue
[478] Fix | Delete
lastheader = line[:i]
[479] Fix | Delete
lastvalue = [line[i+1:].lstrip()]
[480] Fix | Delete
# Done with all the lines, so handle the last header.
[481] Fix | Delete
if lastheader:
[482] Fix | Delete
# XXX reconsider the joining of folded lines
[483] Fix | Delete
self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip('\r\n')
[484] Fix | Delete
[485] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function