"""This will be the home for the policy that hooks in the newcode that adds all the email6 features."""import reimport sysfrom email._policybase import Policy, Compat32, compat32, _extend_docstringsfrom email.utils import _has_surrogatesfrom email.headerregistry import HeaderRegistry as HeaderRegistryfrom email.contentmanager import raw_data_managerfrom email.message import EmailMessage__all__ = ['Compat32','compat32','Policy','EmailPolicy','default','strict','SMTP','HTTP',]linesep_splitter = re.compile(r'\n|\r')@_extend_docstringsclass EmailPolicy(Policy):"""+PROVISIONALThe API extensions enabled by this policy are currently provisional.Refer to the documentation for details.This policy adds new header parsing and folding algorithms. Instead ofsimple strings, headers are custom objects with custom attributesdepending on the type of the field. The folding algorithm fullyimplements RFCs 2047 and 5322.In addition to the settable attributes listed above that apply toall Policies, this policy adds the following additional attributes:utf8 -- if False (the default) message headers will beserialized as ASCII, using encoded words to encodeany non-ASCII characters in the source strings. IfTrue, the message headers will be serialized usingutf8 and will not contain encoded words (see RFC6532 for more on this serialization format).refold_source -- if the value for a header in the Message objectcame from the parsing of some source, this attributeindicates whether or not a generator should refoldthat value when transforming the message back intostream form. The possible values are:none -- all source values use original foldinglong -- source values that have any line that islonger than max_line_length will berefoldedall -- all values are refolded.The default is 'long'.header_factory -- a callable that takes two arguments, 'name' and'value', where 'name' is a header field name and'value' is an unfolded header field value, andreturns a string-like object that represents thatheader. A default header_factory is provided thatunderstands some of the RFC5322 header field types.(Currently address fields and date fields havespecial treatment, while all other fields aretreated as unstructured. This list will becompleted before the extension is marked stable.)content_manager -- an object with at least two methods: get_contentand set_content. When the get_content orset_content method of a Message object is called,it calls the corresponding method of this object,passing it the message object as its first argument,and any arguments or keywords that were passed toit as additional arguments. The defaultcontent_manager is:data:`~email.contentmanager.raw_data_manager`."""message_factory = EmailMessageutf8 = Falserefold_source = 'long'header_factory = HeaderRegistry()content_manager = raw_data_managerdef __init__(self, **kw):# Ensure that each new instance gets a unique header factory# (as opposed to clones, which share the factory).if 'header_factory' not in kw:object.__setattr__(self, 'header_factory', HeaderRegistry())super().__init__(**kw)def header_max_count(self, name):"""+The implementation for this class returns the max_count attribute fromthe specialized header class that would be used to construct a headerof type 'name'."""return self.header_factory[name].max_count# The logic of the next three methods is chosen such that it is possible to# switch a Message object between a Compat32 policy and a policy derived# from this class and have the results stay consistent. This allows a# Message object constructed with this policy to be passed to a library# that only handles Compat32 objects, or to receive such an object and# convert it to use the newer style by just changing its policy. It is# also chosen because it postpones the relatively expensive full rfc5322# parse until as late as possible when parsing from source, since in many# applications only a few headers will actually be inspected.def header_source_parse(self, sourcelines):"""+The name is parsed as everything up to the ':' and returned unmodified.The value is determined by stripping leading whitespace off theremainder of the first line, joining all subsequent lines together, andstripping any trailing carriage return or linefeed characters. (Thisis the same as Compat32)."""name, value = sourcelines[0].split(':', 1)value = value.lstrip(' \t') + ''.join(sourcelines[1:])return (name, value.rstrip('\r\n'))def header_store_parse(self, name, value):"""+The name is returned unchanged. If the input value has a 'name'attribute and it matches the name ignoring case, the value is returnedunchanged. Otherwise the name and value are passed to header_factorymethod, and the resulting custom header object is returned as thevalue. In this case a ValueError is raised if the input value containsCR or LF characters."""if hasattr(value, 'name') and value.name.lower() == name.lower():return (name, value)if isinstance(value, str) and len(value.splitlines())>1:# XXX this error message isn't quite right when we use splitlines# (see issue 22233), but I'm not sure what should happen here.raise ValueError("Header values may not contain linefeed ""or carriage return characters")return (name, self.header_factory(name, value))def header_fetch_parse(self, name, value):"""+If the value has a 'name' attribute, it is returned to unmodified.Otherwise the name and the value with any linesep characters removedare passed to the header_factory method, and the resulting customheader object is returned. Any surrogateescaped bytes get turnedinto the unicode unknown-character glyph."""if hasattr(value, 'name'):return value# We can't use splitlines here because it splits on more than \r and \n.value = ''.join(linesep_splitter.split(value))return self.header_factory(name, value)def fold(self, name, value):"""+Header folding is controlled by the refold_source policy setting. Avalue is considered to be a 'source value' if and only if it does nothave a 'name' attribute (having a 'name' attribute means it is a headerobject of some sort). If a source value needs to be refolded accordingto the policy, it is converted into a custom header object by passingthe name and the value with any linesep characters removed to theheader_factory method. Folding of a custom header object is done bycalling its fold method with the current policy.Source values are split into lines using splitlines. If the value isnot to be refolded, the lines are rejoined using the linesep from thepolicy and returned. The exception is lines containing non-asciibinary data. In that case the value is refolded regardless of therefold_source setting, which causes the binary data to be CTE encodedusing the unknown-8bit charset."""return self._fold(name, value, refold_binary=True)def fold_binary(self, name, value):"""+The same as fold if cte_type is 7bit, except that the returned value isbytes.If cte_type is 8bit, non-ASCII binary data is converted back intobytes. Headers with binary data are not refolded, regardless of therefold_header setting, since there is no way to know whether the binarydata consists of single byte characters or multibyte characters.If utf8 is true, headers are encoded to utf8, otherwise to ascii withnon-ASCII unicode rendered as encoded words."""folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')charset = 'utf8' if self.utf8 else 'ascii'return folded.encode(charset, 'surrogateescape')def _fold(self, name, value, refold_binary=False):if hasattr(value, 'name'):return value.fold(policy=self)maxlen = self.max_line_length if self.max_line_length else sys.maxsizelines = value.splitlines()refold = (self.refold_source == 'all' orself.refold_source == 'long' and(lines and len(lines[0])+len(name)+2 > maxlen orany(len(x) > maxlen for x in lines[1:])))if refold or refold_binary and _has_surrogates(value):return self.header_factory(name, ''.join(lines)).fold(policy=self)return name + ': ' + self.linesep.join(lines) + self.linesepdefault = EmailPolicy()# Make the default policy use the class default header_factorydel default.header_factorystrict = default.clone(raise_on_defect=True)SMTP = default.clone(linesep='\r\n')HTTP = default.clone(linesep='\r\n', max_line_length=None)SMTPUTF8 = SMTP.clone(utf8=True)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。