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