~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/2.4.1/email/Parser.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2001-2004 Python Software Foundation
 
2
# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
 
3
# Contact: email-sig@python.org
 
4
 
 
5
"""A parser of RFC 2822 and MIME email messages."""
 
6
 
 
7
import warnings
 
8
from cStringIO import StringIO
 
9
from email.FeedParser import FeedParser
 
10
from email.Message import Message
 
11
 
 
12
 
 
13
 
 
14
class Parser:
 
15
    def __init__(self, *args, **kws):
 
16
        """Parser of RFC 2822 and MIME email messages.
 
17
 
 
18
        Creates an in-memory object tree representing the email message, which
 
19
        can then be manipulated and turned over to a Generator to return the
 
20
        textual representation of the message.
 
21
 
 
22
        The string must be formatted as a block of RFC 2822 headers and header
 
23
        continuation lines, optionally preceeded by a `Unix-from' header.  The
 
24
        header block is terminated either by the end of the string or by a
 
25
        blank line.
 
26
 
 
27
        _class is the class to instantiate for new message objects when they
 
28
        must be created.  This class must have a constructor that can take
 
29
        zero arguments.  Default is Message.Message.
 
30
        """
 
31
        if len(args) >= 1:
 
32
            if '_class' in kws:
 
33
                raise TypeError("Multiple values for keyword arg '_class'")
 
34
            kws['_class'] = args[0]
 
35
        if len(args) == 2:
 
36
            if 'strict' in kws:
 
37
                raise TypeError("Multiple values for keyword arg 'strict'")
 
38
            kws['strict'] = args[1]
 
39
        if len(args) > 2:
 
40
            raise TypeError('Too many arguments')
 
41
        if '_class' in kws:
 
42
            self._class = kws['_class']
 
43
            del kws['_class']
 
44
        else:
 
45
            self._class = Message
 
46
        if 'strict' in kws:
 
47
            warnings.warn("'strict' argument is deprecated (and ignored)",
 
48
                          DeprecationWarning, 2)
 
49
            del kws['strict']
 
50
        if kws:
 
51
            raise TypeError('Unexpected keyword arguments')
 
52
 
 
53
    def parse(self, fp, headersonly=False):
 
54
        """Create a message structure from the data in a file.
 
55
 
 
56
        Reads all the data from the file and returns the root of the message
 
57
        structure.  Optional headersonly is a flag specifying whether to stop
 
58
        parsing after reading the headers or not.  The default is False,
 
59
        meaning it parses the entire contents of the file.
 
60
        """
 
61
        feedparser = FeedParser(self._class)
 
62
        if headersonly:
 
63
            feedparser._set_headersonly()
 
64
        while True:
 
65
            data = fp.read(8192)
 
66
            if not data:
 
67
                break
 
68
            feedparser.feed(data)
 
69
        return feedparser.close()
 
70
 
 
71
    def parsestr(self, text, headersonly=False):
 
72
        """Create a message structure from a string.
 
73
 
 
74
        Returns the root of the message structure.  Optional headersonly is a
 
75
        flag specifying whether to stop parsing after reading the headers or
 
76
        not.  The default is False, meaning it parses the entire contents of
 
77
        the file.
 
78
        """
 
79
        return self.parse(StringIO(text), headersonly=headersonly)
 
80
 
 
81
 
 
82
 
 
83
class HeaderParser(Parser):
 
84
    def parse(self, fp, headersonly=True):
 
85
        return Parser.parse(self, fp, True)
 
86
 
 
87
    def parsestr(self, text, headersonly=True):
 
88
        return Parser.parsestr(self, text, True)