(root)/
Python-3.11.7/
Lib/
email/
parser.py
       1  # Copyright (C) 2001-2007 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  __all__ = ['Parser', 'HeaderParser', 'BytesParser', 'BytesHeaderParser',
       8             'FeedParser', 'BytesFeedParser']
       9  
      10  from io import StringIO, TextIOWrapper
      11  
      12  from email.feedparser import FeedParser, BytesFeedParser
      13  from email._policybase import compat32
      14  
      15  
      16  class ESC[4;38;5;81mParser:
      17      def __init__(self, _class=None, *, policy=compat32):
      18          """Parser of RFC 2822 and MIME email messages.
      19  
      20          Creates an in-memory object tree representing the email message, which
      21          can then be manipulated and turned over to a Generator to return the
      22          textual representation of the message.
      23  
      24          The string must be formatted as a block of RFC 2822 headers and header
      25          continuation lines, optionally preceded by a `Unix-from' header.  The
      26          header block is terminated either by the end of the string or by a
      27          blank line.
      28  
      29          _class is the class to instantiate for new message objects when they
      30          must be created.  This class must have a constructor that can take
      31          zero arguments.  Default is Message.Message.
      32  
      33          The policy keyword specifies a policy object that controls a number of
      34          aspects of the parser's operation.  The default policy maintains
      35          backward compatibility.
      36  
      37          """
      38          self._class = _class
      39          self.policy = policy
      40  
      41      def parse(self, fp, headersonly=False):
      42          """Create a message structure from the data in a file.
      43  
      44          Reads all the data from the file and returns the root of the message
      45          structure.  Optional headersonly is a flag specifying whether to stop
      46          parsing after reading the headers or not.  The default is False,
      47          meaning it parses the entire contents of the file.
      48          """
      49          feedparser = FeedParser(self._class, policy=self.policy)
      50          if headersonly:
      51              feedparser._set_headersonly()
      52          while True:
      53              data = fp.read(8192)
      54              if not data:
      55                  break
      56              feedparser.feed(data)
      57          return feedparser.close()
      58  
      59      def parsestr(self, text, headersonly=False):
      60          """Create a message structure from a string.
      61  
      62          Returns the root of the message structure.  Optional headersonly is a
      63          flag specifying whether to stop parsing after reading the headers or
      64          not.  The default is False, meaning it parses the entire contents of
      65          the file.
      66          """
      67          return self.parse(StringIO(text), headersonly=headersonly)
      68  
      69  
      70  class ESC[4;38;5;81mHeaderParser(ESC[4;38;5;149mParser):
      71      def parse(self, fp, headersonly=True):
      72          return Parser.parse(self, fp, True)
      73  
      74      def parsestr(self, text, headersonly=True):
      75          return Parser.parsestr(self, text, True)
      76  
      77  
      78  class ESC[4;38;5;81mBytesParser:
      79  
      80      def __init__(self, *args, **kw):
      81          """Parser of binary RFC 2822 and MIME email messages.
      82  
      83          Creates an in-memory object tree representing the email message, which
      84          can then be manipulated and turned over to a Generator to return the
      85          textual representation of the message.
      86  
      87          The input must be formatted as a block of RFC 2822 headers and header
      88          continuation lines, optionally preceded by a `Unix-from' header.  The
      89          header block is terminated either by the end of the input or by a
      90          blank line.
      91  
      92          _class is the class to instantiate for new message objects when they
      93          must be created.  This class must have a constructor that can take
      94          zero arguments.  Default is Message.Message.
      95          """
      96          self.parser = Parser(*args, **kw)
      97  
      98      def parse(self, fp, headersonly=False):
      99          """Create a message structure from the data in a binary file.
     100  
     101          Reads all the data from the file and returns the root of the message
     102          structure.  Optional headersonly is a flag specifying whether to stop
     103          parsing after reading the headers or not.  The default is False,
     104          meaning it parses the entire contents of the file.
     105          """
     106          fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
     107          try:
     108              return self.parser.parse(fp, headersonly)
     109          finally:
     110              fp.detach()
     111  
     112  
     113      def parsebytes(self, text, headersonly=False):
     114          """Create a message structure from a byte string.
     115  
     116          Returns the root of the message structure.  Optional headersonly is a
     117          flag specifying whether to stop parsing after reading the headers or
     118          not.  The default is False, meaning it parses the entire contents of
     119          the file.
     120          """
     121          text = text.decode('ASCII', errors='surrogateescape')
     122          return self.parser.parsestr(text, headersonly)
     123  
     124  
     125  class ESC[4;38;5;81mBytesHeaderParser(ESC[4;38;5;149mBytesParser):
     126      def parse(self, fp, headersonly=True):
     127          return BytesParser.parse(self, fp, headersonly=True)
     128  
     129      def parsebytes(self, text, headersonly=True):
     130          return BytesParser.parsebytes(self, text, headersonly=True)