Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../email
File: parser.py
# Copyright (C) 2001-2006 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']
[6] Fix | Delete
[7] Fix | Delete
import warnings
[8] Fix | Delete
from cStringIO import StringIO
[9] Fix | Delete
[10] Fix | Delete
from email.feedparser import FeedParser
[11] Fix | Delete
from email.message import Message
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
class Parser:
[16] Fix | Delete
def __init__(self, *args, **kws):
[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
if len(args) >= 1:
[33] Fix | Delete
if '_class' in kws:
[34] Fix | Delete
raise TypeError("Multiple values for keyword arg '_class'")
[35] Fix | Delete
kws['_class'] = args[0]
[36] Fix | Delete
if len(args) == 2:
[37] Fix | Delete
if 'strict' in kws:
[38] Fix | Delete
raise TypeError("Multiple values for keyword arg 'strict'")
[39] Fix | Delete
kws['strict'] = args[1]
[40] Fix | Delete
if len(args) > 2:
[41] Fix | Delete
raise TypeError('Too many arguments')
[42] Fix | Delete
if '_class' in kws:
[43] Fix | Delete
self._class = kws['_class']
[44] Fix | Delete
del kws['_class']
[45] Fix | Delete
else:
[46] Fix | Delete
self._class = Message
[47] Fix | Delete
if 'strict' in kws:
[48] Fix | Delete
warnings.warn("'strict' argument is deprecated (and ignored)",
[49] Fix | Delete
DeprecationWarning, 2)
[50] Fix | Delete
del kws['strict']
[51] Fix | Delete
if kws:
[52] Fix | Delete
raise TypeError('Unexpected keyword arguments')
[53] Fix | Delete
[54] Fix | Delete
def parse(self, fp, headersonly=False):
[55] Fix | Delete
"""Create a message structure from the data in a file.
[56] Fix | Delete
[57] Fix | Delete
Reads all the data from the file and returns the root of the message
[58] Fix | Delete
structure. Optional headersonly is a flag specifying whether to stop
[59] Fix | Delete
parsing after reading the headers or not. The default is False,
[60] Fix | Delete
meaning it parses the entire contents of the file.
[61] Fix | Delete
"""
[62] Fix | Delete
feedparser = FeedParser(self._class)
[63] Fix | Delete
if headersonly:
[64] Fix | Delete
feedparser._set_headersonly()
[65] Fix | Delete
while True:
[66] Fix | Delete
data = fp.read(8192)
[67] Fix | Delete
if not data:
[68] Fix | Delete
break
[69] Fix | Delete
feedparser.feed(data)
[70] Fix | Delete
return feedparser.close()
[71] Fix | Delete
[72] Fix | Delete
def parsestr(self, text, headersonly=False):
[73] Fix | Delete
"""Create a message structure from a string.
[74] Fix | Delete
[75] Fix | Delete
Returns the root of the message structure. Optional headersonly is a
[76] Fix | Delete
flag specifying whether to stop parsing after reading the headers or
[77] Fix | Delete
not. The default is False, meaning it parses the entire contents of
[78] Fix | Delete
the file.
[79] Fix | Delete
"""
[80] Fix | Delete
return self.parse(StringIO(text), headersonly=headersonly)
[81] Fix | Delete
[82] Fix | Delete
[83] Fix | Delete
[84] Fix | Delete
class HeaderParser(Parser):
[85] Fix | Delete
def parse(self, fp, headersonly=True):
[86] Fix | Delete
return Parser.parse(self, fp, True)
[87] Fix | Delete
[88] Fix | Delete
def parsestr(self, text, headersonly=True):
[89] Fix | Delete
return Parser.parsestr(self, text, True)
[90] Fix | Delete
[91] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function