charset.py 15.7 KB
Newer Older
1
# Copyright (C) 2001-2006 Python Software Foundation
2 3
# Author: Ben Gertzfield, Barry Warsaw
# Contact: email-sig@python.org
4

5 6 7 8 9 10 11
__all__ = [
    'Charset',
    'add_alias',
    'add_charset',
    'add_codec',
    ]

12
import codecs
13 14 15 16 17
import email.base64mime
import email.quoprimime

from email import errors
from email.encoders import encode_7or8bit
18

19 20 21


# Flags for types of header encodings
22 23 24
QP          = 1 # Quoted-Printable
BASE64      = 2 # Base64
SHORTEST    = 3 # the shorter of QP and base64, but only for headers
25 26

# In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7
Tim Peters's avatar
Tim Peters committed
27
MISC_LEN = 7
28 29 30 31 32 33 34 35

DEFAULT_CHARSET = 'us-ascii'



# Defaults
CHARSETS = {
    # input        header enc  body enc output conv
Tim Peters's avatar
Tim Peters committed
36
    'iso-8859-1':  (QP,        QP,      None),
37
    'iso-8859-2':  (QP,        QP,      None),
38 39 40 41 42 43 44 45 46 47 48 49
    'iso-8859-3':  (QP,        QP,      None),
    'iso-8859-4':  (QP,        QP,      None),
    # iso-8859-5 is Cyrillic, and not especially used
    # iso-8859-6 is Arabic, also not particularly used
    # iso-8859-7 is Greek, QP will not make it readable
    # iso-8859-8 is Hebrew, QP will not make it readable
    'iso-8859-9':  (QP,        QP,      None),
    'iso-8859-10': (QP,        QP,      None),
    # iso-8859-11 is Thai, QP will not make it readable
    'iso-8859-13': (QP,        QP,      None),
    'iso-8859-14': (QP,        QP,      None),
    'iso-8859-15': (QP,        QP,      None),
50
    'iso-8859-16': (QP,        QP,      None),
51 52
    'windows-1252':(QP,        QP,      None),
    'viscii':      (QP,        QP,      None),
53 54
    'us-ascii':    (None,      None,    None),
    'big5':        (BASE64,    BASE64,  None),
Tim Peters's avatar
Tim Peters committed
55
    'gb2312':      (BASE64,    BASE64,  None),
56 57 58 59
    'euc-jp':      (BASE64,    None,    'iso-2022-jp'),
    'shift_jis':   (BASE64,    None,    'iso-2022-jp'),
    'iso-2022-jp': (BASE64,    None,    None),
    'koi8-r':      (BASE64,    BASE64,  None),
60
    'utf-8':       (SHORTEST,  BASE64, 'utf-8'),
61 62
    # We're making this one up to represent raw unencoded 8-bit
    '8bit':        (None,      BASE64, 'utf-8'),
63 64 65 66 67 68 69
    }

# Aliases for other commonly-used names for character sets.  Map
# them to the real ones used in email.
ALIASES = {
    'latin_1': 'iso-8859-1',
    'latin-1': 'iso-8859-1',
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    'latin_2': 'iso-8859-2',
    'latin-2': 'iso-8859-2',
    'latin_3': 'iso-8859-3',
    'latin-3': 'iso-8859-3',
    'latin_4': 'iso-8859-4',
    'latin-4': 'iso-8859-4',
    'latin_5': 'iso-8859-9',
    'latin-5': 'iso-8859-9',
    'latin_6': 'iso-8859-10',
    'latin-6': 'iso-8859-10',
    'latin_7': 'iso-8859-13',
    'latin-7': 'iso-8859-13',
    'latin_8': 'iso-8859-14',
    'latin-8': 'iso-8859-14',
    'latin_9': 'iso-8859-15',
    'latin-9': 'iso-8859-15',
86 87
    'latin_10':'iso-8859-16',
    'latin-10':'iso-8859-16',
88 89 90
    'cp949':   'ks_c_5601-1987',
    'euc_jp':  'euc-jp',
    'euc_kr':  'euc-kr',
91 92 93
    'ascii':   'us-ascii',
    }

94

95
# Map charsets to their Unicode codec strings.
96
CODEC_MAP = {
97
    'gb2312':      'eucgb2312_cn',
98 99 100 101 102 103 104 105 106 107 108
    'big5':        'big5_tw',
    # Hack: We don't want *any* conversion for stuff marked us-ascii, as all
    # sorts of garbage might be sent to us in the guise of 7-bit us-ascii.
    # Let that stuff pass through without conversion to/from Unicode.
    'us-ascii':    None,
    }



# Convenience functions for extending the above mappings
def add_charset(charset, header_enc=None, body_enc=None, output_charset=None):
109
    """Add character set properties to the global registry.
110 111 112 113 114

    charset is the input character set, and must be the canonical name of a
    character set.

    Optional header_enc and body_enc is either Charset.QP for
115 116 117 118 119
    quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for
    the shortest of qp or base64 encoding, or None for no encoding.  SHORTEST
    is only valid for header_enc.  It describes how message headers and
    message bodies in the input charset are to be encoded.  Default is no
    encoding.
120 121 122 123 124 125 126 127

    Optional output_charset is the character set that the output should be
    in.  Conversions will proceed from input charset, to Unicode, to the
    output charset when the method Charset.convert() is called.  The default
    is to output in the same character set as the input.

    Both input_charset and output_charset must have Unicode codec entries in
    the module's charset-to-codec mapping; use add_codec(charset, codecname)
128
    to add codecs the module does not know about.  See the codecs module's
129 130
    documentation for more information.
    """
131
    if body_enc == SHORTEST:
132
        raise ValueError('SHORTEST not allowed for body_enc')
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    CHARSETS[charset] = (header_enc, body_enc, output_charset)


def add_alias(alias, canonical):
    """Add a character set alias.

    alias is the alias name, e.g. latin-1
    canonical is the character set's canonical name, e.g. iso-8859-1
    """
    ALIASES[alias] = canonical


def add_codec(charset, codecname):
    """Add a codec that map characters in the given charset to/from Unicode.

    charset is the canonical name of a character set.  codecname is the name
    of a Python codec, as appropriate for the second argument to the unicode()
150
    built-in, or to the encode() method of a Unicode string.
151 152 153 154 155 156 157 158 159 160 161
    """
    CODEC_MAP[charset] = codecname



class Charset:
    """Map character sets to their email properties.

    This class provides information about the requirements imposed on email
    for a specific character set.  It also provides convenience routines for
    converting between character sets, given the availability of the
162 163 164
    applicable codecs.  Given a character set, it will do its best to provide
    information on how to use that character set in an email in an
    RFC-compliant way.
Tim Peters's avatar
Tim Peters committed
165

166 167 168 169 170 171 172 173 174 175 176
    Certain character sets must be encoded with quoted-printable or base64
    when used in email headers or bodies.  Certain character sets must be
    converted outright, and are not allowed in email.  Instances of this
    module expose the following information about a character set:

    input_charset: The initial character set specified.  Common aliases
                   are converted to their `official' email names (e.g. latin_1
                   is converted to iso-8859-1).  Defaults to 7-bit us-ascii.

    header_encoding: If the character set must be encoded before it can be
                     used in an email header, this attribute will be set to
177 178 179
                     Charset.QP (for quoted-printable), Charset.BASE64 (for
                     base64 encoding), or Charset.SHORTEST for the shortest of
                     QP or BASE64 encoding.  Otherwise, it will be None.
180 181 182

    body_encoding: Same as header_encoding, but describes the encoding for the
                   mail message's body, which indeed may be different than the
183 184
                   header encoding.  Charset.SHORTEST is not allowed for
                   body_encoding.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

    output_charset: Some character sets must be converted before the can be
                    used in email headers or bodies.  If the input_charset is
                    one of them, this attribute will contain the name of the
                    charset output will be converted to.  Otherwise, it will
                    be None.

    input_codec: The name of the Python codec used to convert the
                 input_charset to Unicode.  If no conversion codec is
                 necessary, this attribute will be None.

    output_codec: The name of the Python codec used to convert Unicode
                  to the output_charset.  If no conversion codec is necessary,
                  this attribute will have the same value as the input_codec.
    """
    def __init__(self, input_charset=DEFAULT_CHARSET):
201
        # RFC 2046, $4.1.2 says charsets are not case sensitive.  We coerce to
202 203 204 205 206 207 208 209 210 211
        # unicode because its .lower() is locale insensitive.  If the argument
        # is already a unicode, we leave it at that, but ensure that the
        # charset is ASCII, as the standard (RFC XXX) requires.
        try:
            if isinstance(input_charset, unicode):
                input_charset.encode('ascii')
            else:
                input_charset = unicode(input_charset, 'ascii')
        except UnicodeError:
            raise errors.CharsetError(input_charset)
212
        input_charset = input_charset.lower().encode('ascii')
213 214 215 216 217 218
        # Set the input charset after filtering through the aliases and/or codecs
        if not (input_charset in ALIASES or input_charset in CHARSETS):
            try:
                input_charset = codecs.lookup(input_charset).name
            except LookupError:
                pass
219 220 221 222 223
        self.input_charset = ALIASES.get(input_charset, input_charset)
        # We can try to guess which encoding and conversion to use by the
        # charset_map dictionary.  Try that first, but let the user override
        # it.
        henc, benc, conv = CHARSETS.get(self.input_charset,
224
                                        (SHORTEST, BASE64, None))
225 226
        if not conv:
            conv = self.input_charset
227 228 229 230 231 232 233 234 235
        # Set the attributes, allowing the arguments to override the default.
        self.header_encoding = henc
        self.body_encoding = benc
        self.output_charset = ALIASES.get(conv, conv)
        # Now set the codecs.  If one isn't defined for input_charset,
        # guess and try a Unicode codec with the same name as input_codec.
        self.input_codec = CODEC_MAP.get(self.input_charset,
                                         self.input_charset)
        self.output_codec = CODEC_MAP.get(self.output_charset,
236
                                          self.output_charset)
237 238 239 240

    def __str__(self):
        return self.input_charset.lower()

241 242
    __repr__ = __str__

243 244 245 246 247 248 249 250 251 252 253 254
    def __eq__(self, other):
        return str(self) == str(other).lower()

    def __ne__(self, other):
        return not self.__eq__(other)

    def get_body_encoding(self):
        """Return the content-transfer-encoding used for body encoding.

        This is either the string `quoted-printable' or `base64' depending on
        the encoding used, or it is a function in which case you should call
        the function with a single argument, the Message object being
255
        encoded.  The function should then set the Content-Transfer-Encoding
256 257 258 259 260 261
        header itself to whatever is appropriate.

        Returns "quoted-printable" if self.body_encoding is QP.
        Returns "base64" if self.body_encoding is BASE64.
        Returns "7bit" otherwise.
        """
262
        assert self.body_encoding != SHORTEST
263 264 265 266 267 268 269 270 271
        if self.body_encoding == QP:
            return 'quoted-printable'
        elif self.body_encoding == BASE64:
            return 'base64'
        else:
            return encode_7or8bit

    def convert(self, s):
        """Convert a string from the input_codec to the output_codec."""
272
        if self.input_codec != self.output_codec:
273 274 275 276 277 278 279 280
            return unicode(s, self.input_codec).encode(self.output_codec)
        else:
            return s

    def to_splittable(self, s):
        """Convert a possibly multibyte string to a safely splittable format.

        Uses the input_codec to try and convert the string to Unicode, so it
281
        can be safely split on character boundaries (even for multibyte
282 283
        characters).

284
        Returns the string as-is if it isn't known how to convert it to
285 286 287 288 289
        Unicode with the input_charset.

        Characters that could not be converted to Unicode will be replaced
        with the Unicode replacement character U+FFFD.
        """
290
        if isinstance(s, unicode) or self.input_codec is None:
291 292 293 294 295 296 297 298
            return s
        try:
            return unicode(s, self.input_codec, 'replace')
        except LookupError:
            # Input codec not installed on system, so return the original
            # string unchanged.
            return s

299
    def from_splittable(self, ustr, to_output=True):
300 301
        """Convert a splittable string back into an encoded string.

302 303 304
        Uses the proper codec to try and convert the string from Unicode back
        into an encoded format.  Return the string as-is if it is not Unicode,
        or if it could not be converted from Unicode.
305 306 307 308

        Characters that could not be converted from Unicode will be replaced
        with an appropriate character (usually '?').

309 310
        If to_output is True (the default), uses output_codec to convert to an
        encoded format.  If to_output is False, uses input_codec.
311 312 313 314 315
        """
        if to_output:
            codec = self.output_codec
        else:
            codec = self.input_codec
316
        if not isinstance(ustr, unicode) or codec is None:
317 318 319 320 321 322 323 324 325 326
            return ustr
        try:
            return ustr.encode(codec, 'replace')
        except LookupError:
            # Output codec not installed
            return ustr

    def get_output_charset(self):
        """Return the output character set.

327
        This is self.output_charset if that is not None, otherwise it is
328 329 330 331 332 333 334 335
        self.input_charset.
        """
        return self.output_charset or self.input_charset

    def encoded_header_len(self, s):
        """Return the length of the encoded header string."""
        cset = self.get_output_charset()
        # The len(s) of a 7bit encoding is len(s)
336
        if self.header_encoding == BASE64:
337
            return email.base64mime.base64_len(s) + len(cset) + MISC_LEN
338
        elif self.header_encoding == QP:
339
            return email.quoprimime.header_quopri_len(s) + len(cset) + MISC_LEN
340
        elif self.header_encoding == SHORTEST:
341 342
            lenb64 = email.base64mime.base64_len(s)
            lenqp = email.quoprimime.header_quopri_len(s)
343
            return min(lenb64, lenqp) + len(cset) + MISC_LEN
344 345 346
        else:
            return len(s)

347
    def header_encode(self, s, convert=False):
348 349
        """Header-encode a string, optionally converting it to output_charset.

350
        If convert is True, the string will be converted from the input
351 352 353 354
        charset to the output charset automatically.  This is not useful for
        multibyte character sets, which have line length issues (multibyte
        characters must be split on a character, not a byte boundary); use the
        high-level Header class to deal with these issues.  convert defaults
355
        to False.
356 357 358 359 360 361 362 363

        The type of encoding (base64 or quoted-printable) will be based on
        self.header_encoding.
        """
        cset = self.get_output_charset()
        if convert:
            s = self.convert(s)
        # 7bit/8bit encodings return the string unchanged (modulo conversions)
364
        if self.header_encoding == BASE64:
365
            return email.base64mime.header_encode(s, cset)
366
        elif self.header_encoding == QP:
367
            return email.quoprimime.header_encode(s, cset, maxlinelen=None)
368
        elif self.header_encoding == SHORTEST:
369 370
            lenb64 = email.base64mime.base64_len(s)
            lenqp = email.quoprimime.header_quopri_len(s)
371
            if lenb64 < lenqp:
372
                return email.base64mime.header_encode(s, cset)
373
            else:
374
                return email.quoprimime.header_encode(s, cset, maxlinelen=None)
375 376 377
        else:
            return s

378
    def body_encode(self, s, convert=True):
379 380
        """Body-encode a string and convert it to output_charset.

381
        If convert is True (the default), the string will be converted from
382 383 384 385 386 387 388 389 390 391 392
        the input charset to output charset automatically.  Unlike
        header_encode(), there are no issues with byte boundaries and
        multibyte charsets in email bodies, so this is usually pretty safe.

        The type of encoding (base64 or quoted-printable) will be based on
        self.body_encoding.
        """
        if convert:
            s = self.convert(s)
        # 7bit/8bit encodings return the string unchanged (module conversions)
        if self.body_encoding is BASE64:
393
            return email.base64mime.body_encode(s)
394
        elif self.body_encoding is QP:
395
            return email.quoprimime.body_encode(s)
396 397
        else:
            return s