xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/email/mime/text.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1*cda5da8dSAndroid Build Coastguard Worker# Copyright (C) 2001-2006 Python Software Foundation
2*cda5da8dSAndroid Build Coastguard Worker# Author: Barry Warsaw
3*cda5da8dSAndroid Build Coastguard Worker# Contact: [email protected]
4*cda5da8dSAndroid Build Coastguard Worker
5*cda5da8dSAndroid Build Coastguard Worker"""Class representing text/* type MIME documents."""
6*cda5da8dSAndroid Build Coastguard Worker
7*cda5da8dSAndroid Build Coastguard Worker__all__ = ['MIMEText']
8*cda5da8dSAndroid Build Coastguard Worker
9*cda5da8dSAndroid Build Coastguard Workerfrom email.charset import Charset
10*cda5da8dSAndroid Build Coastguard Workerfrom email.mime.nonmultipart import MIMENonMultipart
11*cda5da8dSAndroid Build Coastguard Worker
12*cda5da8dSAndroid Build Coastguard Worker
13*cda5da8dSAndroid Build Coastguard Worker
14*cda5da8dSAndroid Build Coastguard Workerclass MIMEText(MIMENonMultipart):
15*cda5da8dSAndroid Build Coastguard Worker    """Class for generating text/* type MIME documents."""
16*cda5da8dSAndroid Build Coastguard Worker
17*cda5da8dSAndroid Build Coastguard Worker    def __init__(self, _text, _subtype='plain', _charset=None, *, policy=None):
18*cda5da8dSAndroid Build Coastguard Worker        """Create a text/* type MIME document.
19*cda5da8dSAndroid Build Coastguard Worker
20*cda5da8dSAndroid Build Coastguard Worker        _text is the string for this message object.
21*cda5da8dSAndroid Build Coastguard Worker
22*cda5da8dSAndroid Build Coastguard Worker        _subtype is the MIME sub content type, defaulting to "plain".
23*cda5da8dSAndroid Build Coastguard Worker
24*cda5da8dSAndroid Build Coastguard Worker        _charset is the character set parameter added to the Content-Type
25*cda5da8dSAndroid Build Coastguard Worker        header.  This defaults to "us-ascii".  Note that as a side-effect, the
26*cda5da8dSAndroid Build Coastguard Worker        Content-Transfer-Encoding header will also be set.
27*cda5da8dSAndroid Build Coastguard Worker        """
28*cda5da8dSAndroid Build Coastguard Worker
29*cda5da8dSAndroid Build Coastguard Worker        # If no _charset was specified, check to see if there are non-ascii
30*cda5da8dSAndroid Build Coastguard Worker        # characters present. If not, use 'us-ascii', otherwise use utf-8.
31*cda5da8dSAndroid Build Coastguard Worker        # XXX: This can be removed once #7304 is fixed.
32*cda5da8dSAndroid Build Coastguard Worker        if _charset is None:
33*cda5da8dSAndroid Build Coastguard Worker            try:
34*cda5da8dSAndroid Build Coastguard Worker                _text.encode('us-ascii')
35*cda5da8dSAndroid Build Coastguard Worker                _charset = 'us-ascii'
36*cda5da8dSAndroid Build Coastguard Worker            except UnicodeEncodeError:
37*cda5da8dSAndroid Build Coastguard Worker                _charset = 'utf-8'
38*cda5da8dSAndroid Build Coastguard Worker
39*cda5da8dSAndroid Build Coastguard Worker        MIMENonMultipart.__init__(self, 'text', _subtype, policy=policy,
40*cda5da8dSAndroid Build Coastguard Worker                                  **{'charset': str(_charset)})
41*cda5da8dSAndroid Build Coastguard Worker
42*cda5da8dSAndroid Build Coastguard Worker        self.set_payload(_text, _charset)
43