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