Parser.py 2.79 KB
Newer Older
1 2 3
# Copyright (C) 2001-2004 Python Software Foundation
# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
# Contact: email-sig@python.org
4

5
"""A parser of RFC 2822 and MIME email messages."""
6

7
import re
8
from cStringIO import StringIO
9 10
from email.FeedParser import FeedParser
from email.Message import Message
11

12
NLCRE = re.compile('\r\n|\r|\n')
13

14

15

16
class Parser:
17
    def __init__(self, _class=Message, strict=False):
18 19 20 21 22 23 24 25 26 27 28 29 30 31
        """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 preceeded 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.
32 33 34 35 36

        Optional strict tells the parser to be strictly RFC compliant or to be
        more forgiving in parsing of ill-formatted MIME documents.  When
        non-strict mode is used, the parser will try to make up for missing or
        erroneous boundaries and other peculiarities seen in the wild.
37
        Default is non-strict parsing.
38 39 40
        """
        self._class = _class

41
    def parse(self, fp, headersonly=False):
42 43 44 45 46 47 48
        """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.
        """
49 50 51 52 53 54 55 56 57
        feedparser = FeedParser(self._class)
        if headersonly:
            feedparser._set_headersonly()
        while True:
            data = fp.read(8192)
            if not data:
                break
            feedparser.feed(data)
        return feedparser.close()
58

59
    def parsestr(self, text, headersonly=False):
60 61 62 63 64 65 66
        """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.
        """
67
        return self.parse(StringIO(text), headersonly=headersonly)
68

69 70 71

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

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