Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3..../email
File: policy.py
"""This will be the home for the policy that hooks in the new
[0] Fix | Delete
code that adds all the email6 features.
[1] Fix | Delete
"""
[2] Fix | Delete
[3] Fix | Delete
import re
[4] Fix | Delete
import sys
[5] Fix | Delete
from email._policybase import Policy, Compat32, compat32, _extend_docstrings
[6] Fix | Delete
from email.utils import _has_surrogates
[7] Fix | Delete
from email.headerregistry import HeaderRegistry as HeaderRegistry
[8] Fix | Delete
from email.contentmanager import raw_data_manager
[9] Fix | Delete
from email.message import EmailMessage
[10] Fix | Delete
[11] Fix | Delete
__all__ = [
[12] Fix | Delete
'Compat32',
[13] Fix | Delete
'compat32',
[14] Fix | Delete
'Policy',
[15] Fix | Delete
'EmailPolicy',
[16] Fix | Delete
'default',
[17] Fix | Delete
'strict',
[18] Fix | Delete
'SMTP',
[19] Fix | Delete
'HTTP',
[20] Fix | Delete
]
[21] Fix | Delete
[22] Fix | Delete
linesep_splitter = re.compile(r'\n|\r')
[23] Fix | Delete
[24] Fix | Delete
@_extend_docstrings
[25] Fix | Delete
class EmailPolicy(Policy):
[26] Fix | Delete
[27] Fix | Delete
"""+
[28] Fix | Delete
PROVISIONAL
[29] Fix | Delete
[30] Fix | Delete
The API extensions enabled by this policy are currently provisional.
[31] Fix | Delete
Refer to the documentation for details.
[32] Fix | Delete
[33] Fix | Delete
This policy adds new header parsing and folding algorithms. Instead of
[34] Fix | Delete
simple strings, headers are custom objects with custom attributes
[35] Fix | Delete
depending on the type of the field. The folding algorithm fully
[36] Fix | Delete
implements RFCs 2047 and 5322.
[37] Fix | Delete
[38] Fix | Delete
In addition to the settable attributes listed above that apply to
[39] Fix | Delete
all Policies, this policy adds the following additional attributes:
[40] Fix | Delete
[41] Fix | Delete
utf8 -- if False (the default) message headers will be
[42] Fix | Delete
serialized as ASCII, using encoded words to encode
[43] Fix | Delete
any non-ASCII characters in the source strings. If
[44] Fix | Delete
True, the message headers will be serialized using
[45] Fix | Delete
utf8 and will not contain encoded words (see RFC
[46] Fix | Delete
6532 for more on this serialization format).
[47] Fix | Delete
[48] Fix | Delete
refold_source -- if the value for a header in the Message object
[49] Fix | Delete
came from the parsing of some source, this attribute
[50] Fix | Delete
indicates whether or not a generator should refold
[51] Fix | Delete
that value when transforming the message back into
[52] Fix | Delete
stream form. The possible values are:
[53] Fix | Delete
[54] Fix | Delete
none -- all source values use original folding
[55] Fix | Delete
long -- source values that have any line that is
[56] Fix | Delete
longer than max_line_length will be
[57] Fix | Delete
refolded
[58] Fix | Delete
all -- all values are refolded.
[59] Fix | Delete
[60] Fix | Delete
The default is 'long'.
[61] Fix | Delete
[62] Fix | Delete
header_factory -- a callable that takes two arguments, 'name' and
[63] Fix | Delete
'value', where 'name' is a header field name and
[64] Fix | Delete
'value' is an unfolded header field value, and
[65] Fix | Delete
returns a string-like object that represents that
[66] Fix | Delete
header. A default header_factory is provided that
[67] Fix | Delete
understands some of the RFC5322 header field types.
[68] Fix | Delete
(Currently address fields and date fields have
[69] Fix | Delete
special treatment, while all other fields are
[70] Fix | Delete
treated as unstructured. This list will be
[71] Fix | Delete
completed before the extension is marked stable.)
[72] Fix | Delete
[73] Fix | Delete
content_manager -- an object with at least two methods: get_content
[74] Fix | Delete
and set_content. When the get_content or
[75] Fix | Delete
set_content method of a Message object is called,
[76] Fix | Delete
it calls the corresponding method of this object,
[77] Fix | Delete
passing it the message object as its first argument,
[78] Fix | Delete
and any arguments or keywords that were passed to
[79] Fix | Delete
it as additional arguments. The default
[80] Fix | Delete
content_manager is
[81] Fix | Delete
:data:`~email.contentmanager.raw_data_manager`.
[82] Fix | Delete
[83] Fix | Delete
"""
[84] Fix | Delete
[85] Fix | Delete
message_factory = EmailMessage
[86] Fix | Delete
utf8 = False
[87] Fix | Delete
refold_source = 'long'
[88] Fix | Delete
header_factory = HeaderRegistry()
[89] Fix | Delete
content_manager = raw_data_manager
[90] Fix | Delete
[91] Fix | Delete
def __init__(self, **kw):
[92] Fix | Delete
# Ensure that each new instance gets a unique header factory
[93] Fix | Delete
# (as opposed to clones, which share the factory).
[94] Fix | Delete
if 'header_factory' not in kw:
[95] Fix | Delete
object.__setattr__(self, 'header_factory', HeaderRegistry())
[96] Fix | Delete
super().__init__(**kw)
[97] Fix | Delete
[98] Fix | Delete
def header_max_count(self, name):
[99] Fix | Delete
"""+
[100] Fix | Delete
The implementation for this class returns the max_count attribute from
[101] Fix | Delete
the specialized header class that would be used to construct a header
[102] Fix | Delete
of type 'name'.
[103] Fix | Delete
"""
[104] Fix | Delete
return self.header_factory[name].max_count
[105] Fix | Delete
[106] Fix | Delete
# The logic of the next three methods is chosen such that it is possible to
[107] Fix | Delete
# switch a Message object between a Compat32 policy and a policy derived
[108] Fix | Delete
# from this class and have the results stay consistent. This allows a
[109] Fix | Delete
# Message object constructed with this policy to be passed to a library
[110] Fix | Delete
# that only handles Compat32 objects, or to receive such an object and
[111] Fix | Delete
# convert it to use the newer style by just changing its policy. It is
[112] Fix | Delete
# also chosen because it postpones the relatively expensive full rfc5322
[113] Fix | Delete
# parse until as late as possible when parsing from source, since in many
[114] Fix | Delete
# applications only a few headers will actually be inspected.
[115] Fix | Delete
[116] Fix | Delete
def header_source_parse(self, sourcelines):
[117] Fix | Delete
"""+
[118] Fix | Delete
The name is parsed as everything up to the ':' and returned unmodified.
[119] Fix | Delete
The value is determined by stripping leading whitespace off the
[120] Fix | Delete
remainder of the first line, joining all subsequent lines together, and
[121] Fix | Delete
stripping any trailing carriage return or linefeed characters. (This
[122] Fix | Delete
is the same as Compat32).
[123] Fix | Delete
[124] Fix | Delete
"""
[125] Fix | Delete
name, value = sourcelines[0].split(':', 1)
[126] Fix | Delete
value = value.lstrip(' \t') + ''.join(sourcelines[1:])
[127] Fix | Delete
return (name, value.rstrip('\r\n'))
[128] Fix | Delete
[129] Fix | Delete
def header_store_parse(self, name, value):
[130] Fix | Delete
"""+
[131] Fix | Delete
The name is returned unchanged. If the input value has a 'name'
[132] Fix | Delete
attribute and it matches the name ignoring case, the value is returned
[133] Fix | Delete
unchanged. Otherwise the name and value are passed to header_factory
[134] Fix | Delete
method, and the resulting custom header object is returned as the
[135] Fix | Delete
value. In this case a ValueError is raised if the input value contains
[136] Fix | Delete
CR or LF characters.
[137] Fix | Delete
[138] Fix | Delete
"""
[139] Fix | Delete
if hasattr(value, 'name') and value.name.lower() == name.lower():
[140] Fix | Delete
return (name, value)
[141] Fix | Delete
if isinstance(value, str) and len(value.splitlines())>1:
[142] Fix | Delete
# XXX this error message isn't quite right when we use splitlines
[143] Fix | Delete
# (see issue 22233), but I'm not sure what should happen here.
[144] Fix | Delete
raise ValueError("Header values may not contain linefeed "
[145] Fix | Delete
"or carriage return characters")
[146] Fix | Delete
return (name, self.header_factory(name, value))
[147] Fix | Delete
[148] Fix | Delete
def header_fetch_parse(self, name, value):
[149] Fix | Delete
"""+
[150] Fix | Delete
If the value has a 'name' attribute, it is returned to unmodified.
[151] Fix | Delete
Otherwise the name and the value with any linesep characters removed
[152] Fix | Delete
are passed to the header_factory method, and the resulting custom
[153] Fix | Delete
header object is returned. Any surrogateescaped bytes get turned
[154] Fix | Delete
into the unicode unknown-character glyph.
[155] Fix | Delete
[156] Fix | Delete
"""
[157] Fix | Delete
if hasattr(value, 'name'):
[158] Fix | Delete
return value
[159] Fix | Delete
# We can't use splitlines here because it splits on more than \r and \n.
[160] Fix | Delete
value = ''.join(linesep_splitter.split(value))
[161] Fix | Delete
return self.header_factory(name, value)
[162] Fix | Delete
[163] Fix | Delete
def fold(self, name, value):
[164] Fix | Delete
"""+
[165] Fix | Delete
Header folding is controlled by the refold_source policy setting. A
[166] Fix | Delete
value is considered to be a 'source value' if and only if it does not
[167] Fix | Delete
have a 'name' attribute (having a 'name' attribute means it is a header
[168] Fix | Delete
object of some sort). If a source value needs to be refolded according
[169] Fix | Delete
to the policy, it is converted into a custom header object by passing
[170] Fix | Delete
the name and the value with any linesep characters removed to the
[171] Fix | Delete
header_factory method. Folding of a custom header object is done by
[172] Fix | Delete
calling its fold method with the current policy.
[173] Fix | Delete
[174] Fix | Delete
Source values are split into lines using splitlines. If the value is
[175] Fix | Delete
not to be refolded, the lines are rejoined using the linesep from the
[176] Fix | Delete
policy and returned. The exception is lines containing non-ascii
[177] Fix | Delete
binary data. In that case the value is refolded regardless of the
[178] Fix | Delete
refold_source setting, which causes the binary data to be CTE encoded
[179] Fix | Delete
using the unknown-8bit charset.
[180] Fix | Delete
[181] Fix | Delete
"""
[182] Fix | Delete
return self._fold(name, value, refold_binary=True)
[183] Fix | Delete
[184] Fix | Delete
def fold_binary(self, name, value):
[185] Fix | Delete
"""+
[186] Fix | Delete
The same as fold if cte_type is 7bit, except that the returned value is
[187] Fix | Delete
bytes.
[188] Fix | Delete
[189] Fix | Delete
If cte_type is 8bit, non-ASCII binary data is converted back into
[190] Fix | Delete
bytes. Headers with binary data are not refolded, regardless of the
[191] Fix | Delete
refold_header setting, since there is no way to know whether the binary
[192] Fix | Delete
data consists of single byte characters or multibyte characters.
[193] Fix | Delete
[194] Fix | Delete
If utf8 is true, headers are encoded to utf8, otherwise to ascii with
[195] Fix | Delete
non-ASCII unicode rendered as encoded words.
[196] Fix | Delete
[197] Fix | Delete
"""
[198] Fix | Delete
folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
[199] Fix | Delete
charset = 'utf8' if self.utf8 else 'ascii'
[200] Fix | Delete
return folded.encode(charset, 'surrogateescape')
[201] Fix | Delete
[202] Fix | Delete
def _fold(self, name, value, refold_binary=False):
[203] Fix | Delete
if hasattr(value, 'name'):
[204] Fix | Delete
return value.fold(policy=self)
[205] Fix | Delete
maxlen = self.max_line_length if self.max_line_length else sys.maxsize
[206] Fix | Delete
lines = value.splitlines()
[207] Fix | Delete
refold = (self.refold_source == 'all' or
[208] Fix | Delete
self.refold_source == 'long' and
[209] Fix | Delete
(lines and len(lines[0])+len(name)+2 > maxlen or
[210] Fix | Delete
any(len(x) > maxlen for x in lines[1:])))
[211] Fix | Delete
if refold or refold_binary and _has_surrogates(value):
[212] Fix | Delete
return self.header_factory(name, ''.join(lines)).fold(policy=self)
[213] Fix | Delete
return name + ': ' + self.linesep.join(lines) + self.linesep
[214] Fix | Delete
[215] Fix | Delete
[216] Fix | Delete
default = EmailPolicy()
[217] Fix | Delete
# Make the default policy use the class default header_factory
[218] Fix | Delete
del default.header_factory
[219] Fix | Delete
strict = default.clone(raise_on_defect=True)
[220] Fix | Delete
SMTP = default.clone(linesep='\r\n')
[221] Fix | Delete
HTTP = default.clone(linesep='\r\n', max_line_length=None)
[222] Fix | Delete
SMTPUTF8 = SMTP.clone(utf8=True)
[223] Fix | Delete
[224] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function