parser.py 3.2 KB

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