Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3..../email
File: charset.py
# Copyright (C) 2001-2007 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
from functools import partial
[11] Fix | Delete
[12] Fix | Delete
import email.base64mime
[13] Fix | Delete
import email.quoprimime
[14] Fix | Delete
[15] Fix | Delete
from email import errors
[16] Fix | Delete
from email.encoders import encode_7or8bit
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
# Flags for types of header encodings
[21] Fix | Delete
QP = 1 # Quoted-Printable
[22] Fix | Delete
BASE64 = 2 # Base64
[23] Fix | Delete
SHORTEST = 3 # the shorter of QP and base64, but only for headers
[24] Fix | Delete
[25] Fix | Delete
# In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7
[26] Fix | Delete
RFC2047_CHROME_LEN = 7
[27] Fix | Delete
[28] Fix | Delete
DEFAULT_CHARSET = 'us-ascii'
[29] Fix | Delete
UNKNOWN8BIT = 'unknown-8bit'
[30] Fix | Delete
EMPTYSTRING = ''
[31] Fix | Delete
[32] Fix | Delete
[33] Fix | Delete
[34] Fix | Delete
# Defaults
[35] Fix | Delete
CHARSETS = {
[36] Fix | Delete
# input header enc body enc output conv
[37] Fix | Delete
'iso-8859-1': (QP, QP, None),
[38] Fix | Delete
'iso-8859-2': (QP, QP, None),
[39] Fix | Delete
'iso-8859-3': (QP, QP, None),
[40] Fix | Delete
'iso-8859-4': (QP, QP, None),
[41] Fix | Delete
# iso-8859-5 is Cyrillic, and not especially used
[42] Fix | Delete
# iso-8859-6 is Arabic, also not particularly used
[43] Fix | Delete
# iso-8859-7 is Greek, QP will not make it readable
[44] Fix | Delete
# iso-8859-8 is Hebrew, QP will not make it readable
[45] Fix | Delete
'iso-8859-9': (QP, QP, None),
[46] Fix | Delete
'iso-8859-10': (QP, QP, None),
[47] Fix | Delete
# iso-8859-11 is Thai, QP will not make it readable
[48] Fix | Delete
'iso-8859-13': (QP, QP, None),
[49] Fix | Delete
'iso-8859-14': (QP, QP, None),
[50] Fix | Delete
'iso-8859-15': (QP, QP, None),
[51] Fix | Delete
'iso-8859-16': (QP, QP, None),
[52] Fix | Delete
'windows-1252':(QP, QP, None),
[53] Fix | Delete
'viscii': (QP, QP, None),
[54] Fix | Delete
'us-ascii': (None, None, None),
[55] Fix | Delete
'big5': (BASE64, BASE64, None),
[56] Fix | Delete
'gb2312': (BASE64, BASE64, None),
[57] Fix | Delete
'euc-jp': (BASE64, None, 'iso-2022-jp'),
[58] Fix | Delete
'shift_jis': (BASE64, None, 'iso-2022-jp'),
[59] Fix | Delete
'iso-2022-jp': (BASE64, None, None),
[60] Fix | Delete
'koi8-r': (BASE64, BASE64, None),
[61] Fix | Delete
'utf-8': (SHORTEST, BASE64, 'utf-8'),
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
# Aliases for other commonly-used names for character sets. Map
[65] Fix | Delete
# them to the real ones used in email.
[66] Fix | Delete
ALIASES = {
[67] Fix | Delete
'latin_1': 'iso-8859-1',
[68] Fix | Delete
'latin-1': 'iso-8859-1',
[69] Fix | Delete
'latin_2': 'iso-8859-2',
[70] Fix | Delete
'latin-2': 'iso-8859-2',
[71] Fix | Delete
'latin_3': 'iso-8859-3',
[72] Fix | Delete
'latin-3': 'iso-8859-3',
[73] Fix | Delete
'latin_4': 'iso-8859-4',
[74] Fix | Delete
'latin-4': 'iso-8859-4',
[75] Fix | Delete
'latin_5': 'iso-8859-9',
[76] Fix | Delete
'latin-5': 'iso-8859-9',
[77] Fix | Delete
'latin_6': 'iso-8859-10',
[78] Fix | Delete
'latin-6': 'iso-8859-10',
[79] Fix | Delete
'latin_7': 'iso-8859-13',
[80] Fix | Delete
'latin-7': 'iso-8859-13',
[81] Fix | Delete
'latin_8': 'iso-8859-14',
[82] Fix | Delete
'latin-8': 'iso-8859-14',
[83] Fix | Delete
'latin_9': 'iso-8859-15',
[84] Fix | Delete
'latin-9': 'iso-8859-15',
[85] Fix | Delete
'latin_10':'iso-8859-16',
[86] Fix | Delete
'latin-10':'iso-8859-16',
[87] Fix | Delete
'cp949': 'ks_c_5601-1987',
[88] Fix | Delete
'euc_jp': 'euc-jp',
[89] Fix | Delete
'euc_kr': 'euc-kr',
[90] Fix | Delete
'ascii': 'us-ascii',
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
# Map charsets to their Unicode codec strings.
[95] Fix | Delete
CODEC_MAP = {
[96] Fix | Delete
'gb2312': 'eucgb2312_cn',
[97] Fix | Delete
'big5': 'big5_tw',
[98] Fix | Delete
# Hack: We don't want *any* conversion for stuff marked us-ascii, as all
[99] Fix | Delete
# sorts of garbage might be sent to us in the guise of 7-bit us-ascii.
[100] Fix | Delete
# Let that stuff pass through without conversion to/from Unicode.
[101] Fix | Delete
'us-ascii': None,
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
[105] Fix | Delete
[106] Fix | Delete
# Convenience functions for extending the above mappings
[107] Fix | Delete
def add_charset(charset, header_enc=None, body_enc=None, output_charset=None):
[108] Fix | Delete
"""Add character set properties to the global registry.
[109] Fix | Delete
[110] Fix | Delete
charset is the input character set, and must be the canonical name of a
[111] Fix | Delete
character set.
[112] Fix | Delete
[113] Fix | Delete
Optional header_enc and body_enc is either Charset.QP for
[114] Fix | Delete
quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for
[115] Fix | Delete
the shortest of qp or base64 encoding, or None for no encoding. SHORTEST
[116] Fix | Delete
is only valid for header_enc. It describes how message headers and
[117] Fix | Delete
message bodies in the input charset are to be encoded. Default is no
[118] Fix | Delete
encoding.
[119] Fix | Delete
[120] Fix | Delete
Optional output_charset is the character set that the output should be
[121] Fix | Delete
in. Conversions will proceed from input charset, to Unicode, to the
[122] Fix | Delete
output charset when the method Charset.convert() is called. The default
[123] Fix | Delete
is to output in the same character set as the input.
[124] Fix | Delete
[125] Fix | Delete
Both input_charset and output_charset must have Unicode codec entries in
[126] Fix | Delete
the module's charset-to-codec mapping; use add_codec(charset, codecname)
[127] Fix | Delete
to add codecs the module does not know about. See the codecs module's
[128] Fix | Delete
documentation for more information.
[129] Fix | Delete
"""
[130] Fix | Delete
if body_enc == SHORTEST:
[131] Fix | Delete
raise ValueError('SHORTEST not allowed for body_enc')
[132] Fix | Delete
CHARSETS[charset] = (header_enc, body_enc, output_charset)
[133] Fix | Delete
[134] Fix | Delete
[135] Fix | Delete
def add_alias(alias, canonical):
[136] Fix | Delete
"""Add a character set alias.
[137] Fix | Delete
[138] Fix | Delete
alias is the alias name, e.g. latin-1
[139] Fix | Delete
canonical is the character set's canonical name, e.g. iso-8859-1
[140] Fix | Delete
"""
[141] Fix | Delete
ALIASES[alias] = canonical
[142] Fix | Delete
[143] Fix | Delete
[144] Fix | Delete
def add_codec(charset, codecname):
[145] Fix | Delete
"""Add a codec that map characters in the given charset to/from Unicode.
[146] Fix | Delete
[147] Fix | Delete
charset is the canonical name of a character set. codecname is the name
[148] Fix | Delete
of a Python codec, as appropriate for the second argument to the unicode()
[149] Fix | Delete
built-in, or to the encode() method of a Unicode string.
[150] Fix | Delete
"""
[151] Fix | Delete
CODEC_MAP[charset] = codecname
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
[155] Fix | Delete
# Convenience function for encoding strings, taking into account
[156] Fix | Delete
# that they might be unknown-8bit (ie: have surrogate-escaped bytes)
[157] Fix | Delete
def _encode(string, codec):
[158] Fix | Delete
if codec == UNKNOWN8BIT:
[159] Fix | Delete
return string.encode('ascii', 'surrogateescape')
[160] Fix | Delete
else:
[161] Fix | Delete
return string.encode(codec)
[162] Fix | Delete
[163] Fix | Delete
[164] Fix | Delete
[165] Fix | Delete
class Charset:
[166] Fix | Delete
"""Map character sets to their email properties.
[167] Fix | Delete
[168] Fix | Delete
This class provides information about the requirements imposed on email
[169] Fix | Delete
for a specific character set. It also provides convenience routines for
[170] Fix | Delete
converting between character sets, given the availability of the
[171] Fix | Delete
applicable codecs. Given a character set, it will do its best to provide
[172] Fix | Delete
information on how to use that character set in an email in an
[173] Fix | Delete
RFC-compliant way.
[174] Fix | Delete
[175] Fix | Delete
Certain character sets must be encoded with quoted-printable or base64
[176] Fix | Delete
when used in email headers or bodies. Certain character sets must be
[177] Fix | Delete
converted outright, and are not allowed in email. Instances of this
[178] Fix | Delete
module expose the following information about a character set:
[179] Fix | Delete
[180] Fix | Delete
input_charset: The initial character set specified. Common aliases
[181] Fix | Delete
are converted to their `official' email names (e.g. latin_1
[182] Fix | Delete
is converted to iso-8859-1). Defaults to 7-bit us-ascii.
[183] Fix | Delete
[184] Fix | Delete
header_encoding: If the character set must be encoded before it can be
[185] Fix | Delete
used in an email header, this attribute will be set to
[186] Fix | Delete
Charset.QP (for quoted-printable), Charset.BASE64 (for
[187] Fix | Delete
base64 encoding), or Charset.SHORTEST for the shortest of
[188] Fix | Delete
QP or BASE64 encoding. Otherwise, it will be None.
[189] Fix | Delete
[190] Fix | Delete
body_encoding: Same as header_encoding, but describes the encoding for the
[191] Fix | Delete
mail message's body, which indeed may be different than the
[192] Fix | Delete
header encoding. Charset.SHORTEST is not allowed for
[193] Fix | Delete
body_encoding.
[194] Fix | Delete
[195] Fix | Delete
output_charset: Some character sets must be converted before they can be
[196] Fix | Delete
used in email headers or bodies. If the input_charset is
[197] Fix | Delete
one of them, this attribute will contain the name of the
[198] Fix | Delete
charset output will be converted to. Otherwise, it will
[199] Fix | Delete
be None.
[200] Fix | Delete
[201] Fix | Delete
input_codec: The name of the Python codec used to convert the
[202] Fix | Delete
input_charset to Unicode. If no conversion codec is
[203] Fix | Delete
necessary, this attribute will be None.
[204] Fix | Delete
[205] Fix | Delete
output_codec: The name of the Python codec used to convert Unicode
[206] Fix | Delete
to the output_charset. If no conversion codec is necessary,
[207] Fix | Delete
this attribute will have the same value as the input_codec.
[208] Fix | Delete
"""
[209] Fix | Delete
def __init__(self, input_charset=DEFAULT_CHARSET):
[210] Fix | Delete
# RFC 2046, $4.1.2 says charsets are not case sensitive. We coerce to
[211] Fix | Delete
# unicode because its .lower() is locale insensitive. If the argument
[212] Fix | Delete
# is already a unicode, we leave it at that, but ensure that the
[213] Fix | Delete
# charset is ASCII, as the standard (RFC XXX) requires.
[214] Fix | Delete
try:
[215] Fix | Delete
if isinstance(input_charset, str):
[216] Fix | Delete
input_charset.encode('ascii')
[217] Fix | Delete
else:
[218] Fix | Delete
input_charset = str(input_charset, 'ascii')
[219] Fix | Delete
except UnicodeError:
[220] Fix | Delete
raise errors.CharsetError(input_charset)
[221] Fix | Delete
input_charset = input_charset.lower()
[222] Fix | Delete
# Set the input charset after filtering through the aliases
[223] Fix | Delete
self.input_charset = ALIASES.get(input_charset, input_charset)
[224] Fix | Delete
# We can try to guess which encoding and conversion to use by the
[225] Fix | Delete
# charset_map dictionary. Try that first, but let the user override
[226] Fix | Delete
# it.
[227] Fix | Delete
henc, benc, conv = CHARSETS.get(self.input_charset,
[228] Fix | Delete
(SHORTEST, BASE64, None))
[229] Fix | Delete
if not conv:
[230] Fix | Delete
conv = self.input_charset
[231] Fix | Delete
# Set the attributes, allowing the arguments to override the default.
[232] Fix | Delete
self.header_encoding = henc
[233] Fix | Delete
self.body_encoding = benc
[234] Fix | Delete
self.output_charset = ALIASES.get(conv, conv)
[235] Fix | Delete
# Now set the codecs. If one isn't defined for input_charset,
[236] Fix | Delete
# guess and try a Unicode codec with the same name as input_codec.
[237] Fix | Delete
self.input_codec = CODEC_MAP.get(self.input_charset,
[238] Fix | Delete
self.input_charset)
[239] Fix | Delete
self.output_codec = CODEC_MAP.get(self.output_charset,
[240] Fix | Delete
self.output_charset)
[241] Fix | Delete
[242] Fix | Delete
def __repr__(self):
[243] Fix | Delete
return self.input_charset.lower()
[244] Fix | Delete
[245] Fix | Delete
def __eq__(self, other):
[246] Fix | Delete
return str(self) == str(other).lower()
[247] Fix | Delete
[248] Fix | Delete
def get_body_encoding(self):
[249] Fix | Delete
"""Return the content-transfer-encoding used for body encoding.
[250] Fix | Delete
[251] Fix | Delete
This is either the string `quoted-printable' or `base64' depending on
[252] Fix | Delete
the encoding used, or it is a function in which case you should call
[253] Fix | Delete
the function with a single argument, the Message object being
[254] Fix | Delete
encoded. The function should then set the Content-Transfer-Encoding
[255] Fix | Delete
header itself to whatever is appropriate.
[256] Fix | Delete
[257] Fix | Delete
Returns "quoted-printable" if self.body_encoding is QP.
[258] Fix | Delete
Returns "base64" if self.body_encoding is BASE64.
[259] Fix | Delete
Returns conversion function otherwise.
[260] Fix | Delete
"""
[261] Fix | Delete
assert self.body_encoding != SHORTEST
[262] Fix | Delete
if self.body_encoding == QP:
[263] Fix | Delete
return 'quoted-printable'
[264] Fix | Delete
elif self.body_encoding == BASE64:
[265] Fix | Delete
return 'base64'
[266] Fix | Delete
else:
[267] Fix | Delete
return encode_7or8bit
[268] Fix | Delete
[269] Fix | Delete
def get_output_charset(self):
[270] Fix | Delete
"""Return the output character set.
[271] Fix | Delete
[272] Fix | Delete
This is self.output_charset if that is not None, otherwise it is
[273] Fix | Delete
self.input_charset.
[274] Fix | Delete
"""
[275] Fix | Delete
return self.output_charset or self.input_charset
[276] Fix | Delete
[277] Fix | Delete
def header_encode(self, string):
[278] Fix | Delete
"""Header-encode a string by converting it first to bytes.
[279] Fix | Delete
[280] Fix | Delete
The type of encoding (base64 or quoted-printable) will be based on
[281] Fix | Delete
this charset's `header_encoding`.
[282] Fix | Delete
[283] Fix | Delete
:param string: A unicode string for the header. It must be possible
[284] Fix | Delete
to encode this string to bytes using the character set's
[285] Fix | Delete
output codec.
[286] Fix | Delete
:return: The encoded string, with RFC 2047 chrome.
[287] Fix | Delete
"""
[288] Fix | Delete
codec = self.output_codec or 'us-ascii'
[289] Fix | Delete
header_bytes = _encode(string, codec)
[290] Fix | Delete
# 7bit/8bit encodings return the string unchanged (modulo conversions)
[291] Fix | Delete
encoder_module = self._get_encoder(header_bytes)
[292] Fix | Delete
if encoder_module is None:
[293] Fix | Delete
return string
[294] Fix | Delete
return encoder_module.header_encode(header_bytes, codec)
[295] Fix | Delete
[296] Fix | Delete
def header_encode_lines(self, string, maxlengths):
[297] Fix | Delete
"""Header-encode a string by converting it first to bytes.
[298] Fix | Delete
[299] Fix | Delete
This is similar to `header_encode()` except that the string is fit
[300] Fix | Delete
into maximum line lengths as given by the argument.
[301] Fix | Delete
[302] Fix | Delete
:param string: A unicode string for the header. It must be possible
[303] Fix | Delete
to encode this string to bytes using the character set's
[304] Fix | Delete
output codec.
[305] Fix | Delete
:param maxlengths: Maximum line length iterator. Each element
[306] Fix | Delete
returned from this iterator will provide the next maximum line
[307] Fix | Delete
length. This parameter is used as an argument to built-in next()
[308] Fix | Delete
and should never be exhausted. The maximum line lengths should
[309] Fix | Delete
not count the RFC 2047 chrome. These line lengths are only a
[310] Fix | Delete
hint; the splitter does the best it can.
[311] Fix | Delete
:return: Lines of encoded strings, each with RFC 2047 chrome.
[312] Fix | Delete
"""
[313] Fix | Delete
# See which encoding we should use.
[314] Fix | Delete
codec = self.output_codec or 'us-ascii'
[315] Fix | Delete
header_bytes = _encode(string, codec)
[316] Fix | Delete
encoder_module = self._get_encoder(header_bytes)
[317] Fix | Delete
encoder = partial(encoder_module.header_encode, charset=codec)
[318] Fix | Delete
# Calculate the number of characters that the RFC 2047 chrome will
[319] Fix | Delete
# contribute to each line.
[320] Fix | Delete
charset = self.get_output_charset()
[321] Fix | Delete
extra = len(charset) + RFC2047_CHROME_LEN
[322] Fix | Delete
# Now comes the hard part. We must encode bytes but we can't split on
[323] Fix | Delete
# bytes because some character sets are variable length and each
[324] Fix | Delete
# encoded word must stand on its own. So the problem is you have to
[325] Fix | Delete
# encode to bytes to figure out this word's length, but you must split
[326] Fix | Delete
# on characters. This causes two problems: first, we don't know how
[327] Fix | Delete
# many octets a specific substring of unicode characters will get
[328] Fix | Delete
# encoded to, and second, we don't know how many ASCII characters
[329] Fix | Delete
# those octets will get encoded to. Unless we try it. Which seems
[330] Fix | Delete
# inefficient. In the interest of being correct rather than fast (and
[331] Fix | Delete
# in the hope that there will be few encoded headers in any such
[332] Fix | Delete
# message), brute force it. :(
[333] Fix | Delete
lines = []
[334] Fix | Delete
current_line = []
[335] Fix | Delete
maxlen = next(maxlengths) - extra
[336] Fix | Delete
for character in string:
[337] Fix | Delete
current_line.append(character)
[338] Fix | Delete
this_line = EMPTYSTRING.join(current_line)
[339] Fix | Delete
length = encoder_module.header_length(_encode(this_line, charset))
[340] Fix | Delete
if length > maxlen:
[341] Fix | Delete
# This last character doesn't fit so pop it off.
[342] Fix | Delete
current_line.pop()
[343] Fix | Delete
# Does nothing fit on the first line?
[344] Fix | Delete
if not lines and not current_line:
[345] Fix | Delete
lines.append(None)
[346] Fix | Delete
else:
[347] Fix | Delete
separator = (' ' if lines else '')
[348] Fix | Delete
joined_line = EMPTYSTRING.join(current_line)
[349] Fix | Delete
header_bytes = _encode(joined_line, codec)
[350] Fix | Delete
lines.append(encoder(header_bytes))
[351] Fix | Delete
current_line = [character]
[352] Fix | Delete
maxlen = next(maxlengths) - extra
[353] Fix | Delete
joined_line = EMPTYSTRING.join(current_line)
[354] Fix | Delete
header_bytes = _encode(joined_line, codec)
[355] Fix | Delete
lines.append(encoder(header_bytes))
[356] Fix | Delete
return lines
[357] Fix | Delete
[358] Fix | Delete
def _get_encoder(self, header_bytes):
[359] Fix | Delete
if self.header_encoding == BASE64:
[360] Fix | Delete
return email.base64mime
[361] Fix | Delete
elif self.header_encoding == QP:
[362] Fix | Delete
return email.quoprimime
[363] Fix | Delete
elif self.header_encoding == SHORTEST:
[364] Fix | Delete
len64 = email.base64mime.header_length(header_bytes)
[365] Fix | Delete
lenqp = email.quoprimime.header_length(header_bytes)
[366] Fix | Delete
if len64 < lenqp:
[367] Fix | Delete
return email.base64mime
[368] Fix | Delete
else:
[369] Fix | Delete
return email.quoprimime
[370] Fix | Delete
else:
[371] Fix | Delete
return None
[372] Fix | Delete
[373] Fix | Delete
def body_encode(self, string):
[374] Fix | Delete
"""Body-encode a string by converting it first to bytes.
[375] Fix | Delete
[376] Fix | Delete
The type of encoding (base64 or quoted-printable) will be based on
[377] Fix | Delete
self.body_encoding. If body_encoding is None, we assume the
[378] Fix | Delete
output charset is a 7bit encoding, so re-encoding the decoded
[379] Fix | Delete
string using the ascii codec produces the correct string version
[380] Fix | Delete
of the content.
[381] Fix | Delete
"""
[382] Fix | Delete
if not string:
[383] Fix | Delete
return string
[384] Fix | Delete
if self.body_encoding is BASE64:
[385] Fix | Delete
if isinstance(string, str):
[386] Fix | Delete
string = string.encode(self.output_charset)
[387] Fix | Delete
return email.base64mime.body_encode(string)
[388] Fix | Delete
elif self.body_encoding is QP:
[389] Fix | Delete
# quopromime.body_encode takes a string, but operates on it as if
[390] Fix | Delete
# it were a list of byte codes. For a (minimal) history on why
[391] Fix | Delete
# this is so, see changeset 0cf700464177. To correctly encode a
[392] Fix | Delete
# character set, then, we must turn it into pseudo bytes via the
[393] Fix | Delete
# latin1 charset, which will encode any byte as a single code point
[394] Fix | Delete
# between 0 and 255, which is what body_encode is expecting.
[395] Fix | Delete
if isinstance(string, str):
[396] Fix | Delete
string = string.encode(self.output_charset)
[397] Fix | Delete
string = string.decode('latin1')
[398] Fix | Delete
return email.quoprimime.body_encode(string)
[399] Fix | Delete
else:
[400] Fix | Delete
if isinstance(string, str):
[401] Fix | Delete
string = string.encode(self.output_charset).decode('ascii')
[402] Fix | Delete
return string
[403] Fix | Delete
[404] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function