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