Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../email
File: charset.py
# Copyright (C) 2001-2006 Python Software Foundation
[0] Fix | Delete
# Author: Ben Gertzfield, Barry Warsaw
[1] Fix | Delete
# Contact: email-sig@python.org
[2] Fix | Delete
[3] Fix | Delete
__all__ = [
[4] Fix | Delete
'Charset',
[5] Fix | Delete
'add_alias',
[6] Fix | Delete
'add_charset',
[7] Fix | Delete
'add_codec',
[8] Fix | Delete
]
[9] Fix | Delete
[10] Fix | Delete
import codecs
[11] Fix | Delete
import email.base64mime
[12] Fix | Delete
import email.quoprimime
[13] Fix | Delete
[14] Fix | Delete
from email import errors
[15] Fix | Delete
from email.encoders import encode_7or8bit
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
# Flags for types of header encodings
[20] Fix | Delete
QP = 1 # Quoted-Printable
[21] Fix | Delete
BASE64 = 2 # Base64
[22] Fix | Delete
SHORTEST = 3 # the shorter of QP and base64, but only for headers
[23] Fix | Delete
[24] Fix | Delete
# In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7
[25] Fix | Delete
MISC_LEN = 7
[26] Fix | Delete
[27] Fix | Delete
DEFAULT_CHARSET = 'us-ascii'
[28] Fix | Delete
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
# Defaults
[32] Fix | Delete
CHARSETS = {
[33] Fix | Delete
# input header enc body enc output conv
[34] Fix | Delete
'iso-8859-1': (QP, QP, None),
[35] Fix | Delete
'iso-8859-2': (QP, QP, None),
[36] Fix | Delete
'iso-8859-3': (QP, QP, None),
[37] Fix | Delete
'iso-8859-4': (QP, QP, None),
[38] Fix | Delete
# iso-8859-5 is Cyrillic, and not especially used
[39] Fix | Delete
# iso-8859-6 is Arabic, also not particularly used
[40] Fix | Delete
# iso-8859-7 is Greek, QP will not make it readable
[41] Fix | Delete
# iso-8859-8 is Hebrew, QP will not make it readable
[42] Fix | Delete
'iso-8859-9': (QP, QP, None),
[43] Fix | Delete
'iso-8859-10': (QP, QP, None),
[44] Fix | Delete
# iso-8859-11 is Thai, QP will not make it readable
[45] Fix | Delete
'iso-8859-13': (QP, QP, None),
[46] Fix | Delete
'iso-8859-14': (QP, QP, None),
[47] Fix | Delete
'iso-8859-15': (QP, QP, None),
[48] Fix | Delete
'iso-8859-16': (QP, QP, None),
[49] Fix | Delete
'windows-1252':(QP, QP, None),
[50] Fix | Delete
'viscii': (QP, QP, None),
[51] Fix | Delete
'us-ascii': (None, None, None),
[52] Fix | Delete
'big5': (BASE64, BASE64, None),
[53] Fix | Delete
'gb2312': (BASE64, BASE64, None),
[54] Fix | Delete
'euc-jp': (BASE64, None, 'iso-2022-jp'),
[55] Fix | Delete
'shift_jis': (BASE64, None, 'iso-2022-jp'),
[56] Fix | Delete
'iso-2022-jp': (BASE64, None, None),
[57] Fix | Delete
'koi8-r': (BASE64, BASE64, None),
[58] Fix | Delete
'utf-8': (SHORTEST, BASE64, 'utf-8'),
[59] Fix | Delete
# We're making this one up to represent raw unencoded 8-bit
[60] Fix | Delete
'8bit': (None, BASE64, 'utf-8'),
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
# Aliases for other commonly-used names for character sets. Map
[64] Fix | Delete
# them to the real ones used in email.
[65] Fix | Delete
ALIASES = {
[66] Fix | Delete
'latin_1': 'iso-8859-1',
[67] Fix | Delete
'latin-1': 'iso-8859-1',
[68] Fix | Delete
'latin_2': 'iso-8859-2',
[69] Fix | Delete
'latin-2': 'iso-8859-2',
[70] Fix | Delete
'latin_3': 'iso-8859-3',
[71] Fix | Delete
'latin-3': 'iso-8859-3',
[72] Fix | Delete
'latin_4': 'iso-8859-4',
[73] Fix | Delete
'latin-4': 'iso-8859-4',
[74] Fix | Delete
'latin_5': 'iso-8859-9',
[75] Fix | Delete
'latin-5': 'iso-8859-9',
[76] Fix | Delete
'latin_6': 'iso-8859-10',
[77] Fix | Delete
'latin-6': 'iso-8859-10',
[78] Fix | Delete
'latin_7': 'iso-8859-13',
[79] Fix | Delete
'latin-7': 'iso-8859-13',
[80] Fix | Delete
'latin_8': 'iso-8859-14',
[81] Fix | Delete
'latin-8': 'iso-8859-14',
[82] Fix | Delete
'latin_9': 'iso-8859-15',
[83] Fix | Delete
'latin-9': 'iso-8859-15',
[84] Fix | Delete
'latin_10':'iso-8859-16',
[85] Fix | Delete
'latin-10':'iso-8859-16',
[86] Fix | Delete
'cp949': 'ks_c_5601-1987',
[87] Fix | Delete
'euc_jp': 'euc-jp',
[88] Fix | Delete
'euc_kr': 'euc-kr',
[89] Fix | Delete
'ascii': 'us-ascii',
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
[93] Fix | Delete
# Map charsets to their Unicode codec strings.
[94] Fix | Delete
CODEC_MAP = {
[95] Fix | Delete
'gb2312': 'eucgb2312_cn',
[96] Fix | Delete
'big5': 'big5_tw',
[97] Fix | Delete
# Hack: We don't want *any* conversion for stuff marked us-ascii, as all
[98] Fix | Delete
# sorts of garbage might be sent to us in the guise of 7-bit us-ascii.
[99] Fix | Delete
# Let that stuff pass through without conversion to/from Unicode.
[100] Fix | Delete
'us-ascii': None,
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
[105] Fix | Delete
# Convenience functions for extending the above mappings
[106] Fix | Delete
def add_charset(charset, header_enc=None, body_enc=None, output_charset=None):
[107] Fix | Delete
"""Add character set properties to the global registry.
[108] Fix | Delete
[109] Fix | Delete
charset is the input character set, and must be the canonical name of a
[110] Fix | Delete
character set.
[111] Fix | Delete
[112] Fix | Delete
Optional header_enc and body_enc is either Charset.QP for
[113] Fix | Delete
quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for
[114] Fix | Delete
the shortest of qp or base64 encoding, or None for no encoding. SHORTEST
[115] Fix | Delete
is only valid for header_enc. It describes how message headers and
[116] Fix | Delete
message bodies in the input charset are to be encoded. Default is no
[117] Fix | Delete
encoding.
[118] Fix | Delete
[119] Fix | Delete
Optional output_charset is the character set that the output should be
[120] Fix | Delete
in. Conversions will proceed from input charset, to Unicode, to the
[121] Fix | Delete
output charset when the method Charset.convert() is called. The default
[122] Fix | Delete
is to output in the same character set as the input.
[123] Fix | Delete
[124] Fix | Delete
Both input_charset and output_charset must have Unicode codec entries in
[125] Fix | Delete
the module's charset-to-codec mapping; use add_codec(charset, codecname)
[126] Fix | Delete
to add codecs the module does not know about. See the codecs module's
[127] Fix | Delete
documentation for more information.
[128] Fix | Delete
"""
[129] Fix | Delete
if body_enc == SHORTEST:
[130] Fix | Delete
raise ValueError('SHORTEST not allowed for body_enc')
[131] Fix | Delete
CHARSETS[charset] = (header_enc, body_enc, output_charset)
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
def add_alias(alias, canonical):
[135] Fix | Delete
"""Add a character set alias.
[136] Fix | Delete
[137] Fix | Delete
alias is the alias name, e.g. latin-1
[138] Fix | Delete
canonical is the character set's canonical name, e.g. iso-8859-1
[139] Fix | Delete
"""
[140] Fix | Delete
ALIASES[alias] = canonical
[141] Fix | Delete
[142] Fix | Delete
[143] Fix | Delete
def add_codec(charset, codecname):
[144] Fix | Delete
"""Add a codec that map characters in the given charset to/from Unicode.
[145] Fix | Delete
[146] Fix | Delete
charset is the canonical name of a character set. codecname is the name
[147] Fix | Delete
of a Python codec, as appropriate for the second argument to the unicode()
[148] Fix | Delete
built-in, or to the encode() method of a Unicode string.
[149] Fix | Delete
"""
[150] Fix | Delete
CODEC_MAP[charset] = codecname
[151] Fix | Delete
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
class Charset:
[155] Fix | Delete
"""Map character sets to their email properties.
[156] Fix | Delete
[157] Fix | Delete
This class provides information about the requirements imposed on email
[158] Fix | Delete
for a specific character set. It also provides convenience routines for
[159] Fix | Delete
converting between character sets, given the availability of the
[160] Fix | Delete
applicable codecs. Given a character set, it will do its best to provide
[161] Fix | Delete
information on how to use that character set in an email in an
[162] Fix | Delete
RFC-compliant way.
[163] Fix | Delete
[164] Fix | Delete
Certain character sets must be encoded with quoted-printable or base64
[165] Fix | Delete
when used in email headers or bodies. Certain character sets must be
[166] Fix | Delete
converted outright, and are not allowed in email. Instances of this
[167] Fix | Delete
module expose the following information about a character set:
[168] Fix | Delete
[169] Fix | Delete
input_charset: The initial character set specified. Common aliases
[170] Fix | Delete
are converted to their `official' email names (e.g. latin_1
[171] Fix | Delete
is converted to iso-8859-1). Defaults to 7-bit us-ascii.
[172] Fix | Delete
[173] Fix | Delete
header_encoding: If the character set must be encoded before it can be
[174] Fix | Delete
used in an email header, this attribute will be set to
[175] Fix | Delete
Charset.QP (for quoted-printable), Charset.BASE64 (for
[176] Fix | Delete
base64 encoding), or Charset.SHORTEST for the shortest of
[177] Fix | Delete
QP or BASE64 encoding. Otherwise, it will be None.
[178] Fix | Delete
[179] Fix | Delete
body_encoding: Same as header_encoding, but describes the encoding for the
[180] Fix | Delete
mail message's body, which indeed may be different than the
[181] Fix | Delete
header encoding. Charset.SHORTEST is not allowed for
[182] Fix | Delete
body_encoding.
[183] Fix | Delete
[184] Fix | Delete
output_charset: Some character sets must be converted before they can be
[185] Fix | Delete
used in email headers or bodies. If the input_charset is
[186] Fix | Delete
one of them, this attribute will contain the name of the
[187] Fix | Delete
charset output will be converted to. Otherwise, it will
[188] Fix | Delete
be None.
[189] Fix | Delete
[190] Fix | Delete
input_codec: The name of the Python codec used to convert the
[191] Fix | Delete
input_charset to Unicode. If no conversion codec is
[192] Fix | Delete
necessary, this attribute will be None.
[193] Fix | Delete
[194] Fix | Delete
output_codec: The name of the Python codec used to convert Unicode
[195] Fix | Delete
to the output_charset. If no conversion codec is necessary,
[196] Fix | Delete
this attribute will have the same value as the input_codec.
[197] Fix | Delete
"""
[198] Fix | Delete
def __init__(self, input_charset=DEFAULT_CHARSET):
[199] Fix | Delete
# RFC 2046, $4.1.2 says charsets are not case sensitive. We coerce to
[200] Fix | Delete
# unicode because its .lower() is locale insensitive. If the argument
[201] Fix | Delete
# is already a unicode, we leave it at that, but ensure that the
[202] Fix | Delete
# charset is ASCII, as the standard (RFC XXX) requires.
[203] Fix | Delete
try:
[204] Fix | Delete
if isinstance(input_charset, unicode):
[205] Fix | Delete
input_charset.encode('ascii')
[206] Fix | Delete
else:
[207] Fix | Delete
input_charset = unicode(input_charset, 'ascii')
[208] Fix | Delete
except UnicodeError:
[209] Fix | Delete
raise errors.CharsetError(input_charset)
[210] Fix | Delete
input_charset = input_charset.lower().encode('ascii')
[211] Fix | Delete
# Set the input charset after filtering through the aliases and/or codecs
[212] Fix | Delete
if not (input_charset in ALIASES or input_charset in CHARSETS):
[213] Fix | Delete
try:
[214] Fix | Delete
input_charset = codecs.lookup(input_charset).name
[215] Fix | Delete
except LookupError:
[216] Fix | Delete
pass
[217] Fix | Delete
self.input_charset = ALIASES.get(input_charset, input_charset)
[218] Fix | Delete
# We can try to guess which encoding and conversion to use by the
[219] Fix | Delete
# charset_map dictionary. Try that first, but let the user override
[220] Fix | Delete
# it.
[221] Fix | Delete
henc, benc, conv = CHARSETS.get(self.input_charset,
[222] Fix | Delete
(SHORTEST, BASE64, None))
[223] Fix | Delete
if not conv:
[224] Fix | Delete
conv = self.input_charset
[225] Fix | Delete
# Set the attributes, allowing the arguments to override the default.
[226] Fix | Delete
self.header_encoding = henc
[227] Fix | Delete
self.body_encoding = benc
[228] Fix | Delete
self.output_charset = ALIASES.get(conv, conv)
[229] Fix | Delete
# Now set the codecs. If one isn't defined for input_charset,
[230] Fix | Delete
# guess and try a Unicode codec with the same name as input_codec.
[231] Fix | Delete
self.input_codec = CODEC_MAP.get(self.input_charset,
[232] Fix | Delete
self.input_charset)
[233] Fix | Delete
self.output_codec = CODEC_MAP.get(self.output_charset,
[234] Fix | Delete
self.output_charset)
[235] Fix | Delete
[236] Fix | Delete
def __str__(self):
[237] Fix | Delete
return self.input_charset.lower()
[238] Fix | Delete
[239] Fix | Delete
__repr__ = __str__
[240] Fix | Delete
[241] Fix | Delete
def __eq__(self, other):
[242] Fix | Delete
return str(self) == str(other).lower()
[243] Fix | Delete
[244] Fix | Delete
def __ne__(self, other):
[245] Fix | Delete
return not self.__eq__(other)
[246] Fix | Delete
[247] Fix | Delete
def get_body_encoding(self):
[248] Fix | Delete
"""Return the content-transfer-encoding used for body encoding.
[249] Fix | Delete
[250] Fix | Delete
This is either the string `quoted-printable' or `base64' depending on
[251] Fix | Delete
the encoding used, or it is a function in which case you should call
[252] Fix | Delete
the function with a single argument, the Message object being
[253] Fix | Delete
encoded. The function should then set the Content-Transfer-Encoding
[254] Fix | Delete
header itself to whatever is appropriate.
[255] Fix | Delete
[256] Fix | Delete
Returns "quoted-printable" if self.body_encoding is QP.
[257] Fix | Delete
Returns "base64" if self.body_encoding is BASE64.
[258] Fix | Delete
Returns "7bit" otherwise.
[259] Fix | Delete
"""
[260] Fix | Delete
assert self.body_encoding != SHORTEST
[261] Fix | Delete
if self.body_encoding == QP:
[262] Fix | Delete
return 'quoted-printable'
[263] Fix | Delete
elif self.body_encoding == BASE64:
[264] Fix | Delete
return 'base64'
[265] Fix | Delete
else:
[266] Fix | Delete
return encode_7or8bit
[267] Fix | Delete
[268] Fix | Delete
def convert(self, s):
[269] Fix | Delete
"""Convert a string from the input_codec to the output_codec."""
[270] Fix | Delete
if self.input_codec != self.output_codec:
[271] Fix | Delete
return unicode(s, self.input_codec).encode(self.output_codec)
[272] Fix | Delete
else:
[273] Fix | Delete
return s
[274] Fix | Delete
[275] Fix | Delete
def to_splittable(self, s):
[276] Fix | Delete
"""Convert a possibly multibyte string to a safely splittable format.
[277] Fix | Delete
[278] Fix | Delete
Uses the input_codec to try and convert the string to Unicode, so it
[279] Fix | Delete
can be safely split on character boundaries (even for multibyte
[280] Fix | Delete
characters).
[281] Fix | Delete
[282] Fix | Delete
Returns the string as-is if it isn't known how to convert it to
[283] Fix | Delete
Unicode with the input_charset.
[284] Fix | Delete
[285] Fix | Delete
Characters that could not be converted to Unicode will be replaced
[286] Fix | Delete
with the Unicode replacement character U+FFFD.
[287] Fix | Delete
"""
[288] Fix | Delete
if isinstance(s, unicode) or self.input_codec is None:
[289] Fix | Delete
return s
[290] Fix | Delete
try:
[291] Fix | Delete
return unicode(s, self.input_codec, 'replace')
[292] Fix | Delete
except LookupError:
[293] Fix | Delete
# Input codec not installed on system, so return the original
[294] Fix | Delete
# string unchanged.
[295] Fix | Delete
return s
[296] Fix | Delete
[297] Fix | Delete
def from_splittable(self, ustr, to_output=True):
[298] Fix | Delete
"""Convert a splittable string back into an encoded string.
[299] Fix | Delete
[300] Fix | Delete
Uses the proper codec to try and convert the string from Unicode back
[301] Fix | Delete
into an encoded format. Return the string as-is if it is not Unicode,
[302] Fix | Delete
or if it could not be converted from Unicode.
[303] Fix | Delete
[304] Fix | Delete
Characters that could not be converted from Unicode will be replaced
[305] Fix | Delete
with an appropriate character (usually '?').
[306] Fix | Delete
[307] Fix | Delete
If to_output is True (the default), uses output_codec to convert to an
[308] Fix | Delete
encoded format. If to_output is False, uses input_codec.
[309] Fix | Delete
"""
[310] Fix | Delete
if to_output:
[311] Fix | Delete
codec = self.output_codec
[312] Fix | Delete
else:
[313] Fix | Delete
codec = self.input_codec
[314] Fix | Delete
if not isinstance(ustr, unicode) or codec is None:
[315] Fix | Delete
return ustr
[316] Fix | Delete
try:
[317] Fix | Delete
return ustr.encode(codec, 'replace')
[318] Fix | Delete
except LookupError:
[319] Fix | Delete
# Output codec not installed
[320] Fix | Delete
return ustr
[321] Fix | Delete
[322] Fix | Delete
def get_output_charset(self):
[323] Fix | Delete
"""Return the output character set.
[324] Fix | Delete
[325] Fix | Delete
This is self.output_charset if that is not None, otherwise it is
[326] Fix | Delete
self.input_charset.
[327] Fix | Delete
"""
[328] Fix | Delete
return self.output_charset or self.input_charset
[329] Fix | Delete
[330] Fix | Delete
def encoded_header_len(self, s):
[331] Fix | Delete
"""Return the length of the encoded header string."""
[332] Fix | Delete
cset = self.get_output_charset()
[333] Fix | Delete
# The len(s) of a 7bit encoding is len(s)
[334] Fix | Delete
if self.header_encoding == BASE64:
[335] Fix | Delete
return email.base64mime.base64_len(s) + len(cset) + MISC_LEN
[336] Fix | Delete
elif self.header_encoding == QP:
[337] Fix | Delete
return email.quoprimime.header_quopri_len(s) + len(cset) + MISC_LEN
[338] Fix | Delete
elif self.header_encoding == SHORTEST:
[339] Fix | Delete
lenb64 = email.base64mime.base64_len(s)
[340] Fix | Delete
lenqp = email.quoprimime.header_quopri_len(s)
[341] Fix | Delete
return min(lenb64, lenqp) + len(cset) + MISC_LEN
[342] Fix | Delete
else:
[343] Fix | Delete
return len(s)
[344] Fix | Delete
[345] Fix | Delete
def header_encode(self, s, convert=False):
[346] Fix | Delete
"""Header-encode a string, optionally converting it to output_charset.
[347] Fix | Delete
[348] Fix | Delete
If convert is True, the string will be converted from the input
[349] Fix | Delete
charset to the output charset automatically. This is not useful for
[350] Fix | Delete
multibyte character sets, which have line length issues (multibyte
[351] Fix | Delete
characters must be split on a character, not a byte boundary); use the
[352] Fix | Delete
high-level Header class to deal with these issues. convert defaults
[353] Fix | Delete
to False.
[354] Fix | Delete
[355] Fix | Delete
The type of encoding (base64 or quoted-printable) will be based on
[356] Fix | Delete
self.header_encoding.
[357] Fix | Delete
"""
[358] Fix | Delete
cset = self.get_output_charset()
[359] Fix | Delete
if convert:
[360] Fix | Delete
s = self.convert(s)
[361] Fix | Delete
# 7bit/8bit encodings return the string unchanged (modulo conversions)
[362] Fix | Delete
if self.header_encoding == BASE64:
[363] Fix | Delete
return email.base64mime.header_encode(s, cset)
[364] Fix | Delete
elif self.header_encoding == QP:
[365] Fix | Delete
return email.quoprimime.header_encode(s, cset, maxlinelen=None)
[366] Fix | Delete
elif self.header_encoding == SHORTEST:
[367] Fix | Delete
lenb64 = email.base64mime.base64_len(s)
[368] Fix | Delete
lenqp = email.quoprimime.header_quopri_len(s)
[369] Fix | Delete
if lenb64 < lenqp:
[370] Fix | Delete
return email.base64mime.header_encode(s, cset)
[371] Fix | Delete
else:
[372] Fix | Delete
return email.quoprimime.header_encode(s, cset, maxlinelen=None)
[373] Fix | Delete
else:
[374] Fix | Delete
return s
[375] Fix | Delete
[376] Fix | Delete
def body_encode(self, s, convert=True):
[377] Fix | Delete
"""Body-encode a string and convert it to output_charset.
[378] Fix | Delete
[379] Fix | Delete
If convert is True (the default), the string will be converted from
[380] Fix | Delete
the input charset to output charset automatically. Unlike
[381] Fix | Delete
header_encode(), there are no issues with byte boundaries and
[382] Fix | Delete
multibyte charsets in email bodies, so this is usually pretty safe.
[383] Fix | Delete
[384] Fix | Delete
The type of encoding (base64 or quoted-printable) will be based on
[385] Fix | Delete
self.body_encoding.
[386] Fix | Delete
"""
[387] Fix | Delete
if convert:
[388] Fix | Delete
s = self.convert(s)
[389] Fix | Delete
# 7bit/8bit encodings return the string unchanged (module conversions)
[390] Fix | Delete
if self.body_encoding is BASE64:
[391] Fix | Delete
return email.base64mime.body_encode(s)
[392] Fix | Delete
elif self.body_encoding is QP:
[393] Fix | Delete
return email.quoprimime.body_encode(s)
[394] Fix | Delete
else:
[395] Fix | Delete
return s
[396] Fix | Delete
[397] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function