Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3..../email
File: parser.py
# Copyright (C) 2001-2007 Python Software Foundation
[0] Fix | Delete
# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
[1] Fix | Delete
# Contact: email-sig@python.org
[2] Fix | Delete
[3] Fix | Delete
"""A parser of RFC 2822 and MIME email messages."""
[4] Fix | Delete
[5] Fix | Delete
__all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser',
[6] Fix | Delete
'FeedParser', 'BytesFeedParser']
[7] Fix | Delete
[8] Fix | Delete
from io import StringIO, TextIOWrapper
[9] Fix | Delete
[10] Fix | Delete
from email.feedparser import FeedParser, BytesFeedParser
[11] Fix | Delete
from email._policybase import compat32
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
class Parser:
[15] Fix | Delete
def __init__(self, _class=None, *, policy=compat32):
[16] Fix | Delete
"""Parser of RFC 2822 and MIME email messages.
[17] Fix | Delete
[18] Fix | Delete
Creates an in-memory object tree representing the email message, which
[19] Fix | Delete
can then be manipulated and turned over to a Generator to return the
[20] Fix | Delete
textual representation of the message.
[21] Fix | Delete
[22] Fix | Delete
The string must be formatted as a block of RFC 2822 headers and header
[23] Fix | Delete
continuation lines, optionally preceded by a `Unix-from' header. The
[24] Fix | Delete
header block is terminated either by the end of the string or by a
[25] Fix | Delete
blank line.
[26] Fix | Delete
[27] Fix | Delete
_class is the class to instantiate for new message objects when they
[28] Fix | Delete
must be created. This class must have a constructor that can take
[29] Fix | Delete
zero arguments. Default is Message.Message.
[30] Fix | Delete
[31] Fix | Delete
The policy keyword specifies a policy object that controls a number of
[32] Fix | Delete
aspects of the parser's operation. The default policy maintains
[33] Fix | Delete
backward compatibility.
[34] Fix | Delete
[35] Fix | Delete
"""
[36] Fix | Delete
self._class = _class
[37] Fix | Delete
self.policy = policy
[38] Fix | Delete
[39] Fix | Delete
def parse(self, fp, headersonly=False):
[40] Fix | Delete
"""Create a message structure from the data in a file.
[41] Fix | Delete
[42] Fix | Delete
Reads all the data from the file and returns the root of the message
[43] Fix | Delete
structure. Optional headersonly is a flag specifying whether to stop
[44] Fix | Delete
parsing after reading the headers or not. The default is False,
[45] Fix | Delete
meaning it parses the entire contents of the file.
[46] Fix | Delete
"""
[47] Fix | Delete
feedparser = FeedParser(self._class, policy=self.policy)
[48] Fix | Delete
if headersonly:
[49] Fix | Delete
feedparser._set_headersonly()
[50] Fix | Delete
while True:
[51] Fix | Delete
data = fp.read(8192)
[52] Fix | Delete
if not data:
[53] Fix | Delete
break
[54] Fix | Delete
feedparser.feed(data)
[55] Fix | Delete
return feedparser.close()
[56] Fix | Delete
[57] Fix | Delete
def parsestr(self, text, headersonly=False):
[58] Fix | Delete
"""Create a message structure from a string.
[59] Fix | Delete
[60] Fix | Delete
Returns the root of the message structure. Optional headersonly is a
[61] Fix | Delete
flag specifying whether to stop parsing after reading the headers or
[62] Fix | Delete
not. The default is False, meaning it parses the entire contents of
[63] Fix | Delete
the file.
[64] Fix | Delete
"""
[65] Fix | Delete
return self.parse(StringIO(text), headersonly=headersonly)
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
[69] Fix | Delete
class HeaderParser(Parser):
[70] Fix | Delete
def parse(self, fp, headersonly=True):
[71] Fix | Delete
return Parser.parse(self, fp, True)
[72] Fix | Delete
[73] Fix | Delete
def parsestr(self, text, headersonly=True):
[74] Fix | Delete
return Parser.parsestr(self, text, True)
[75] Fix | Delete
[76] Fix | Delete
[77] Fix | Delete
class BytesParser:
[78] Fix | Delete
[79] Fix | Delete
def __init__(self, *args, **kw):
[80] Fix | Delete
"""Parser of binary RFC 2822 and MIME email messages.
[81] Fix | Delete
[82] Fix | Delete
Creates an in-memory object tree representing the email message, which
[83] Fix | Delete
can then be manipulated and turned over to a Generator to return the
[84] Fix | Delete
textual representation of the message.
[85] Fix | Delete
[86] Fix | Delete
The input must be formatted as a block of RFC 2822 headers and header
[87] Fix | Delete
continuation lines, optionally preceded by a `Unix-from' header. The
[88] Fix | Delete
header block is terminated either by the end of the input or by a
[89] Fix | Delete
blank line.
[90] Fix | Delete
[91] Fix | Delete
_class is the class to instantiate for new message objects when they
[92] Fix | Delete
must be created. This class must have a constructor that can take
[93] Fix | Delete
zero arguments. Default is Message.Message.
[94] Fix | Delete
"""
[95] Fix | Delete
self.parser = Parser(*args, **kw)
[96] Fix | Delete
[97] Fix | Delete
def parse(self, fp, headersonly=False):
[98] Fix | Delete
"""Create a message structure from the data in a binary file.
[99] Fix | Delete
[100] Fix | Delete
Reads all the data from the file and returns the root of the message
[101] Fix | Delete
structure. Optional headersonly is a flag specifying whether to stop
[102] Fix | Delete
parsing after reading the headers or not. The default is False,
[103] Fix | Delete
meaning it parses the entire contents of the file.
[104] Fix | Delete
"""
[105] Fix | Delete
fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
[106] Fix | Delete
try:
[107] Fix | Delete
return self.parser.parse(fp, headersonly)
[108] Fix | Delete
finally:
[109] Fix | Delete
fp.detach()
[110] Fix | Delete
[111] Fix | Delete
[112] Fix | Delete
def parsebytes(self, text, headersonly=False):
[113] Fix | Delete
"""Create a message structure from a byte string.
[114] Fix | Delete
[115] Fix | Delete
Returns the root of the message structure. Optional headersonly is a
[116] Fix | Delete
flag specifying whether to stop parsing after reading the headers or
[117] Fix | Delete
not. The default is False, meaning it parses the entire contents of
[118] Fix | Delete
the file.
[119] Fix | Delete
"""
[120] Fix | Delete
text = text.decode('ASCII', errors='surrogateescape')
[121] Fix | Delete
return self.parser.parsestr(text, headersonly)
[122] Fix | Delete
[123] Fix | Delete
[124] Fix | Delete
class BytesHeaderParser(BytesParser):
[125] Fix | Delete
def parse(self, fp, headersonly=True):
[126] Fix | Delete
return BytesParser.parse(self, fp, headersonly=True)
[127] Fix | Delete
[128] Fix | Delete
def parsebytes(self, text, headersonly=True):
[129] Fix | Delete
return BytesParser.parsebytes(self, text, headersonly=True)
[130] Fix | Delete
[131] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function