c 20012007 python software foundation barry warsaw thomas wouters anthony baxter contact emailsigpython org a parser of rfc 2822 and mime email messages all parser headerparser bytesparser bytesheaderparser feedparser bytesfeedparser from io import stringio textiowrapper from email feedparser import feedparser bytesfeedparser from email policybase import compat32 class parser def initself classnone policycompat32 self class class self policy policy def parseself fp headersonlyfalse feedparser feedparserself class policyself policy if headersonly feedparser setheadersonly while data fp read8192 feedparser feeddata return feedparser close def parsestrself text headersonlyfalse return self parsestringiotext headersonlyheadersonly class headerparserparser def parseself fp headersonlytrue return parser parseself fp true def parsestrself text headersonlytrue return parser parsestrself text true class bytesparser def initself args kw self parser parserargs kw def parseself fp headersonlyfalse fp textiowrapperfp encoding ascii errors surrogateescape try return self parser parsefp headersonly finally fp detach def parsebytesself text headersonlyfalse text text decode ascii errors surrogateescape return self parser parsestrtext headersonly class bytesheaderparserbytesparser def parseself fp headersonlytrue return bytesparser parseself fp headersonlytrue def parsebytesself text headersonlytrue return bytesparser parsebytesself text headersonlytrue c 2001 2007 python software foundation barry warsaw thomas wouters anthony baxter contact email sig python org a parser of rfc 2822 and mime email messages parser of rfc 2822 and mime email messages creates an in memory object tree representing the email message which can then be manipulated and turned over to a generator to return the textual representation of the message the string must be formatted as a block of rfc 2822 headers and header continuation lines optionally preceded by a unix from header the header block is terminated either by the end of the string or by a blank line _class is the class to instantiate for new message objects when they must be created this class must have a constructor that can take zero arguments default is message message the policy keyword specifies a policy object that controls a number of aspects of the parser s operation the default policy maintains backward compatibility create a message structure from the data in a file reads all the data from the file and returns the root of the message structure optional headersonly is a flag specifying whether to stop parsing after reading the headers or not the default is false meaning it parses the entire contents of the file create a message structure from a string returns the root of the message structure optional headersonly is a flag specifying whether to stop parsing after reading the headers or not the default is false meaning it parses the entire contents of the file parser of binary rfc 2822 and mime email messages creates an in memory object tree representing the email message which can then be manipulated and turned over to a generator to return the textual representation of the message the input must be formatted as a block of rfc 2822 headers and header continuation lines optionally preceded by a unix from header the header block is terminated either by the end of the input or by a blank line _class is the class to instantiate for new message objects when they must be created this class must have a constructor that can take zero arguments default is message message create a message structure from the data in a binary file reads all the data from the file and returns the root of the message structure optional headersonly is a flag specifying whether to stop parsing after reading the headers or not the default is false meaning it parses the entire contents of the file create a message structure from a byte string returns the root of the message structure optional headersonly is a flag specifying whether to stop parsing after reading the headers or not the default is false meaning it parses the entire contents of the file
__all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser',
           'FeedParser', 'BytesFeedParser']

from io import StringIO, TextIOWrapper

from email.feedparser import FeedParser, BytesFeedParser
from email._policybase import compat32


class Parser:
    def __init__(self, _class=None, *, policy=compat32):
        
        self._class = _class
        self.policy = policy

    def parse(self, fp, headersonly=False):
        
        feedparser = FeedParser(self._class, policy=self.policy)
        if headersonly:
            feedparser._set_headersonly()
        while data := fp.read(8192):
            feedparser.feed(data)
        return feedparser.close()

    def parsestr(self, text, headersonly=False):
        
        return self.parse(StringIO(text), headersonly=headersonly)


class HeaderParser(Parser):
    def parse(self, fp, headersonly=True):
        return Parser.parse(self, fp, True)

    def parsestr(self, text, headersonly=True):
        return Parser.parsestr(self, text, True)


class BytesParser:

    def __init__(self, *args, **kw):
        
        self.parser = Parser(*args, **kw)

    def parse(self, fp, headersonly=False):
        
        fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
        try:
            return self.parser.parse(fp, headersonly)
        finally:
            fp.detach()


    def parsebytes(self, text, headersonly=False):
        
        text = text.decode('ASCII', errors='surrogateescape')
        return self.parser.parsestr(text, headersonly)


class BytesHeaderParser(BytesParser):
    def parse(self, fp, headersonly=True):
        return BytesParser.parse(self, fp, headersonly=True)

    def parsebytes(self, text, headersonly=True):
        return BytesParser.parsebytes(self, text, headersonly=True)