blob: 0c05224489e2df45b0f517308cde6158a3fadd4e [file] [log] [blame]
Barry Warsaw235c8eb2004-05-09 03:46:42 +00001# Copyright (C) 2001-2004 Python Software Foundation
2# Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
3# Contact: email-sig@python.org
Barry Warsawba925802001-09-23 03:17:28 +00004
Barry Warsaw235c8eb2004-05-09 03:46:42 +00005"""A parser of RFC 2822 and MIME email messages."""
Barry Warsawba925802001-09-23 03:17:28 +00006
Barry Warsawbb113862004-10-03 03:16:19 +00007import warnings
Barry Warsawba925802001-09-23 03:17:28 +00008from cStringIO import StringIO
Barry Warsaw235c8eb2004-05-09 03:46:42 +00009from email.FeedParser import FeedParser
10from email.Message import Message
Barry Warsawe03e8f02002-09-28 20:44:58 +000011
12
Barry Warsawe968ead2001-10-04 17:05:11 +000013
Barry Warsawba925802001-09-23 03:17:28 +000014class Parser:
Barry Warsawbb113862004-10-03 03:16:19 +000015 def __init__(self, *args, **kws):
Barry Warsawba925802001-09-23 03:17:28 +000016 """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 """
Barry Warsawbb113862004-10-03 03:16:19 +000031 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')
Barry Warsawba925802001-09-23 03:17:28 +000052
Barry Warsawe03e8f02002-09-28 20:44:58 +000053 def parse(self, fp, headersonly=False):
Barry Warsaw057b8422002-09-30 20:07:22 +000054 """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 """
Barry Warsaw235c8eb2004-05-09 03:46:42 +000061 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()
Barry Warsawba925802001-09-23 03:17:28 +000070
Barry Warsawe03e8f02002-09-28 20:44:58 +000071 def parsestr(self, text, headersonly=False):
Barry Warsaw057b8422002-09-30 20:07:22 +000072 """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 """
Barry Warsawf6caeba2002-07-09 02:50:02 +000079 return self.parse(StringIO(text), headersonly=headersonly)
Barry Warsawba925802001-09-23 03:17:28 +000080
Barry Warsawe5528822001-10-11 15:43:00 +000081
82
83class HeaderParser(Parser):
Barry Warsaw235c8eb2004-05-09 03:46:42 +000084 def parse(self, fp, headersonly=True):
85 return Parser.parse(self, fp, True)
Barry Warsawe5528822001-10-11 15:43:00 +000086
Barry Warsaw235c8eb2004-05-09 03:46:42 +000087 def parsestr(self, text, headersonly=True):
88 return Parser.parsestr(self, text, True)