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