Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/root/sys/lib/python2.7/email/message.pyo

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�`^c@s�dZdgZddlZddlZddlZddlZddlmZddlZ	ddl	m
Z
ddl	mZdZej
d�Zd	�Zded
�Zd�Zd�Zddd
��YZdS(s8Basic message object for the email package object model.tMessagei�N(tStringIO(tutils(terrorss; s[ \(\)<>@,;:\\"/\[\]\?=]cCsD|jd�\}}}|s.|j�dfS|j�|j�fS(Nt;(t	partitiontstriptNone(tparamtatseptb((s#/sys/lib/python2.7/email/message.pyt_splitparamscCs�|dk	r�t|�dkr�t|t�r[|d7}tj|d|d|d�}n|sptj|�r�d|tj|�fSd||fSn|SdS(s�Convenience function to format and return a key=value pair.

    This will quote the value if needed or if quote is true.  If value is a
    three tuple (charset, language, value), it will be encoded according
    to RFC2231 rules.
    it*iis%s="%s"s%s=%sN(	Rtlent
isinstancettupleRtencode_rfc2231t	tspecialstsearchtquote(RtvalueR((s#/sys/lib/python2.7/email/message.pyt_formatparam&s
$cCsg}x|d dkr|d}|jd�}xR|dkr�|jdd|�|jdd|�dr�|jd|d�}q5W|dkr�t|�}n|| }d|kr�jd�}|| j�j�d||dj�}n|j|j��||}q	W|S(NiRit"s\"it=(tfindtcountRtindexRtlowertappend(tstplisttendtfti((s#/sys/lib/python2.7/email/message.pyt_parseparam>s
;
/cCsBt|t�r1|d|dtj|d�fStj|�SdS(Niii(RRRtunquote(R((s#/sys/lib/python2.7/email/message.pyt
_unquotevaluePs"cBs�eZdZd�Zd�Zed�Zd�Zd�Zd�Z	d�Z
d.ed�Zd.d	�Z
d
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd.d�Zd.d�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d.de#d �Z$d.de#d!�Z%d"e#d.d#d$�Z&de#d%�Z'd"e#d&�Z(d.d'�Z)d.d(�Z*d)�Z+d.d*�Z,d.d+�Z-d,d-l.m/Z/RS(/s�Basic message object.

    A message object is defined as something that has a bunch of RFC 2822
    headers and a payload.  It may optionally have an envelope header
    (a.k.a. Unix-From or From_ header).  If the message is a container (i.e. a
    multipart or a message/rfc822), then the payload is a list of Message
    objects, otherwise it is a string.

    Message objects implement part of the `mapping' interface, which assumes
    there is exactly one occurrence of the header per message.  Some headers
    do in fact appear multiple times (e.g. Received) and for those headers,
    you must use the explicit API to set or get all the headers.  Not all of
    the mapping methods are implemented.
    cCsJg|_d|_d|_d|_d|_|_g|_d|_dS(Ns
text/plain(	t_headersRt	_unixfromt_payloadt_charsettpreambletepiloguetdefectst
_default_type(tself((s#/sys/lib/python2.7/email/message.pyt__init__ks					cCs|jdt�S(swReturn the entire formatted message as a string.
        This includes the headers, body, and envelope header.
        tunixfrom(t	as_stringtTrue(R.((s#/sys/lib/python2.7/email/message.pyt__str__vscCsBddlm}t�}||�}|j|d|�|j�S(s�Return the entire formatted message as a string.
        Optional `unixfrom' when True, means include the Unix From_ envelope
        header.

        This is a convenience method and may not generate the message exactly
        as you intend because by default it mangles lines that begin with
        "From ".  For more flexibility, use the flatten() method of a
        Generator instance.
        i�(t	GeneratorR0(temail.generatorR4Rtflattentgetvalue(R.R0R4tfptg((s#/sys/lib/python2.7/email/message.pyR1|s

	cCst|jt�S(s6Return True if the message consists of multiple parts.(RR(tlist(R.((s#/sys/lib/python2.7/email/message.pytis_multipart�scCs
||_dS(N(R'(R.R0((s#/sys/lib/python2.7/email/message.pytset_unixfrom�scCs|jS(N(R'(R.((s#/sys/lib/python2.7/email/message.pytget_unixfrom�scCs2|jdkr|g|_n|jj|�dS(s�Add the given payload to the current payload.

        The current payload will always be a list of objects after this method
        is called.  If you want to set the payload to a scalar object, use
        set_payload() instead.
        N(R(RR(R.tpayload((s#/sys/lib/python2.7/email/message.pytattach�scCs9|dkr|j}n;t|jt�sFtdt|j���n
|j|}|r5|j�ridS|jdd�j�}|dkr�t	j
|�S|dkr�yt	j|�SWq2tj
k
r�|SXq5|d
kr5t�}y0tjt|d
�|dt�|j�}Wq2tj
k
r.|SXq5n|S(sZReturn a reference to the payload.

        The payload will either be a list object or a string.  If you mutate
        the list object, you modify the message's payload in place.  Optional
        i returns that index into the payload.

        Optional decode is a flag indicating whether the payload should be
        decoded or not, according to the Content-Transfer-Encoding header
        (default is False).

        When True and the message is not a multipart, the payload will be
        decoded if this header's value is `quoted-printable' or `base64'.  If
        some other encoding is used, or the header is missing, or if the
        payload has bogus data (i.e. bogus base64 or uuencoded data), the
        payload is returned as-is.

        If the message is a multipart and the decode flag is True, then None
        is returned.
        sExpected list, got %sscontent-transfer-encodingtsquoted-printabletbase64s
x-uuencodetuuencodetuuesx-uues
tquietN(s
x-uuencodeRBRCsx-uue(RR(RR:t	TypeErrorttypeR;tgetRRt_qdecodet_bdecodetbinasciitErrorRtuutdecodeR2R7(R.R"RMR>tctetsfp((s#/sys/lib/python2.7/email/message.pytget_payload�s0

	 cCs)||_|dk	r%|j|�ndS(s�Set the payload to the given value.

        Optional charset sets the message's default character set.  See
        set_charset() for details.
        N(R(Rtset_charset(R.R>tcharset((s#/sys/lib/python2.7/email/message.pytset_payload�s	cCs�|dkr&|jd�d|_dSt|t�rJtjj|�}nt|tjj�snt|��n||_d|kr�|j	dd�nd|kr�|j	ddd|j
��n|jd|j
��t|jt
�r|jj|j�|_nt|�|j
�kr4|j|j�|_nd|kr�|j�}y||�Wq�tk
r�|j|j�|_|j	d|�q�XndS(s�Set the charset of the payload to a given character set.

        charset can be a Charset instance, a string naming a character set, or
        None.  If it is a string it will be converted to a Charset instance.
        If charset is None, the charset parameter will be removed from the
        Content-Type field.  Anything else will generate a TypeError.

        The message will be assumed to be of type text/* encoded with
        charset.input_charset.  It will be converted to charset.output_charset
        and encoded properly, if needed, when generating the plain text
        representation of the message.  MIME headers (MIME-Version,
        Content-Type, Content-Transfer-Encoding) will be added as needed.

        RRNsMIME-Versions1.0sContent-Types
text/plainsContent-Transfer-Encoding(Rt	del_paramR)Rt
basestringtemailRRtCharsetREt
add_headertget_output_charsett	set_paramR(tunicodetencodetoutput_charsettstrtbody_encodetget_body_encoding(R.RRRN((s#/sys/lib/python2.7/email/message.pyRQ�s4
		
cCs|jS(sKReturn the Charset instance associated with the message's payload.
        (R)(R.((s#/sys/lib/python2.7/email/message.pytget_charsetscCs
t|j�S(s9Return the total number of headers, including duplicates.(RR&(R.((s#/sys/lib/python2.7/email/message.pyt__len__scCs
|j|�S(s-Get a header value.

        Return None if the header is missing instead of raising an exception.

        Note that if the header appeared multiple times, exactly which
        occurrence gets returned is undefined.  Use get_all() to get all
        the values matching a header field name.
        (RG(R.tname((s#/sys/lib/python2.7/email/message.pyt__getitem__s	cCs|jj||f�dS(s�Set the value of a header.

        Note: this does not overwrite an existing header with the same field
        name.  Use __delitem__() first to delete any existing headers.
        N(R&R(R.Rctval((s#/sys/lib/python2.7/email/message.pyt__setitem__(scCsa|j�}g}x?|jD]4\}}|j�|kr|j||f�qqW||_dS(swDelete all occurrences of a header, if present.

        Does not raise an exception if the header is missing.
        N(RR&R(R.Rct
newheaderstktv((s#/sys/lib/python2.7/email/message.pyt__delitem__0scCs2|j�g|jD]\}}|j�^qkS(N(RR&(R.RcRhRi((s#/sys/lib/python2.7/email/message.pyt__contains__<scCst�}|j||�|k	S(s/Return true if the message contains the header.(tobjectRG(R.Rctmissing((s#/sys/lib/python2.7/email/message.pythas_key?s	cCs g|jD]\}}|^q
S(s.Return a list of all the message's header field names.

        These will be sorted in the order they appeared in the original
        message, or were added to the message, and may contain duplicates.
        Any fields deleted and re-inserted are always appended to the header
        list.
        (R&(R.RhRi((s#/sys/lib/python2.7/email/message.pytkeysDscCs g|jD]\}}|^q
S(s)Return a list of all the message's header values.

        These will be sorted in the order they appeared in the original
        message, or were added to the message, and may contain duplicates.
        Any fields deleted and re-inserted are always appended to the header
        list.
        (R&(R.RhRi((s#/sys/lib/python2.7/email/message.pytvaluesNscCs|jS(s'Get all the message's header fields and values.

        These will be sorted in the order they appeared in the original
        message, or were added to the message, and may contain duplicates.
        Any fields deleted and re-inserted are always appended to the header
        list.
        (R&(R.((s#/sys/lib/python2.7/email/message.pytitemsXscCs@|j�}x-|jD]"\}}|j�|kr|SqW|S(s~Get a header value.

        Like __getitem__() but return failobj instead of None when the field
        is missing.
        (RR&(R.RctfailobjRhRi((s#/sys/lib/python2.7/email/message.pyRGbs
cCs\g}|j�}x9|jD].\}}|j�|kr|j|�qqW|sX|S|S(sQReturn a list of all the values for the named field.

        These will be sorted in the order they appeared in the original
        message, and may contain duplicates.  Any fields deleted and
        re-inserted are always appended to the header list.

        If no such fields exist, failobj is returned (defaults to None).
        (RR&R(R.RcRrRpRhRi((s#/sys/lib/python2.7/email/message.pytget_allrs	cKs�g}xd|j�D]V\}}|dkrG|j|jdd��q|jt|jdd�|��qW|dk	r�|jd|�n|jj|tj|�f�dS(s�Extended header setting.

        name is the header field to add.  keyword arguments can be used to set
        additional parameters for the header field, with underscores converted
        to dashes.  Normally the parameter will be added as key="value" unless
        value is None, in which case only the key will be added.  If a
        parameter value contains non-ASCII characters it must be specified as a
        three-tuple of (charset, language, value), in which case it will be
        encoded according to RFC2231 rules.

        Example:

        msg.add_header('content-disposition', 'attachment', filename='bud.gif')
        t_t-iN(	RqRRtreplaceRtinsertR&t	SEMISPACEtjoin(R.t_namet_valuet_paramstpartsRhRi((s#/sys/lib/python2.7/email/message.pyRX�s&cCs}|j�}xjttt|j��|j�D];\}\}}|j�|kr.||f|j|<Pq.q.Wt|��dS(s�Replace a header.

        Replace the first matching header found in the message, retaining
        header order and case.  If no matching header was found, a KeyError is
        raised.
        N(RtziptrangeRR&tKeyError(R.RzR{R"RhRi((s#/sys/lib/python2.7/email/message.pytreplace_header�s4cCsdt�}|jd|�}||kr1|j�St|�dj�}|jd�dkr`dS|S(s0Return the message's content type.

        The returned string is coerced to lower case of the form
        `maintype/subtype'.  If there was no Content-Type header in the
        message, the default type as given by get_default_type() will be
        returned.  Since according to RFC 2045, messages always have a default
        type this will always return a value.

        RFC 2045 defines a message's default type to be text/plain unless it
        appears inside a multipart/digest container, in which case it would be
        message/rfc822.
        scontent-typeit/is
text/plain(RlRGtget_default_typeRRR(R.RmRtctype((s#/sys/lib/python2.7/email/message.pytget_content_type�s
	
cCs|j�}|jd�dS(s�Return the message's main content type.

        This is the `maintype' part of the string returned by
        get_content_type().
        R�i(R�tsplit(R.R�((s#/sys/lib/python2.7/email/message.pytget_content_maintype�scCs|j�}|jd�dS(s�Returns the message's sub-content type.

        This is the `subtype' part of the string returned by
        get_content_type().
        R�i(R�R�(R.R�((s#/sys/lib/python2.7/email/message.pytget_content_subtype�scCs|jS(sReturn the `default' content type.

        Most messages have a default content type of text/plain, except for
        messages that are subparts of multipart/digest containers.  Such
        subparts have a default content type of message/rfc822.
        (R-(R.((s#/sys/lib/python2.7/email/message.pyR��scCs
||_dS(s�Set the `default' content type.

        ctype should be either "text/plain" or "message/rfc822", although this
        is not enforced.  The default content type is not stored in the
        Content-Type header.
        N(R-(R.R�((s#/sys/lib/python2.7/email/message.pytset_default_type�sc	Cs�t�}|j||�}||kr+|Sg}x�td|�D]s}y4|jdd�\}}|j�}|j�}Wn#tk
r�|j�}d}nX|j||f�qBWtj|�}|S(NRRiR@(	RlRGR#R�Rt
ValueErrorRRt
decode_params(	R.RrtheaderRmRtparamstpRcRe((s#/sys/lib/python2.7/email/message.pyt_get_params_preserve�s 	

scontent-typecCsbt�}|j||�}||kr+|S|rZg|D]\}}|t|�f^q8S|SdS(smReturn the message's Content-Type parameters, as a list.

        The elements of the returned list are 2-tuples of key/value pairs, as
        split on the `=' sign.  The left hand side of the `=' is the key,
        while the right hand side is the value.  If there is no `=' sign in
        the parameter the value is the empty string.  The value is as
        described in the get_param() method.

        Optional failobj is the object to return if there is no Content-Type
        header.  Optional header is the header to search instead of
        Content-Type.  If unquote is True, the value is unquoted.
        N(RlR�R%(R.RrR�R$RmR�RhRi((s#/sys/lib/python2.7/email/message.pyt
get_paramss
	)cCsf||kr|SxO|j||�D];\}}|j�|j�kr#|rWt|�S|Sq#q#W|S(s�Return the parameter value if found in the Content-Type header.

        Optional failobj is the object to return if there is no Content-Type
        header, or the Content-Type header has no such parameter.  Optional
        header is the header to search instead of Content-Type.

        Parameter keys are always compared case insensitively.  The return
        value can either be a string, or a 3-tuple if the parameter was RFC
        2231 encoded.  When it's a 3-tuple, the elements of the value are of
        the form (CHARSET, LANGUAGE, VALUE).  Note that both CHARSET and
        LANGUAGE can be None, in which case you should consider VALUE to be
        encoded in the us-ascii charset.  You can usually ignore LANGUAGE.

        Your application should be prepared to deal with 3-tuple return
        values, and can convert the parameter to a Unicode string like so:

            param = msg.get_param('foo')
            if isinstance(param, tuple):
                param = unicode(param[2], param[0] or 'us-ascii')

        In any case, the parameter value (either the returned string, or the
        VALUE item in the 3-tuple) is always unquoted, unless unquote is set
        to False.
        (R�RR%(R.RRrR�R$RhRi((s#/sys/lib/python2.7/email/message.pyt	get_params
sContent-TypeR@cCswt|t�r(|r(|||f}n||krO|j�dkrOd}n|j|�}|j|d|�s�|s�t|||�}qJtj|t|||�g�}n�d}x�|jd|d|�D]u\}}	d}
|j�|j�krt|||�}
nt||	|�}
|s1|
}q�tj||
g�}q�W||j|�krs||=|||<ndS(s�Set a parameter in the Content-Type header.

        If the parameter already exists in the header, its value will be
        replaced with the new value.

        If header is Content-Type and has not yet been defined for this
        message, it will be set to "text/plain" and the new parameter and
        value will be appended as per RFC 2045.

        An alternate header can specified in the header argument, and all
        parameters will be quoted as necessary unless requote is False.

        If charset is specified, the parameter will be encoded according to RFC
        2231.  Optional language specifies the RFC 2231 language, defaulting
        to the empty string.  Both charset and language should be strings.
        scontent-types
text/plainR�R@R$N(	RRRRGR�RRxRyR�(R.RRR�trequoteRRtlanguageR�t	old_paramt	old_valuetappend_param((s#/sys/lib/python2.7/email/message.pyRZ;s.		cCs�||krdSd}x}|jd|d|�D]c\}}|j�|j�kr/|snt|||�}q�tj|t|||�g�}q/q/W||j|�kr�||=|||<ndS(s>Remove the given parameter completely from the Content-Type header.

        The header will be re-written in place without the parameter or its
        value. All values will be quoted as necessary unless requote is
        False.  Optional header specifies an alternative to the Content-Type
        header.
        NR@R�R$(R�RRRxRyRG(R.RR�R�t	new_ctypeR�Ri((s#/sys/lib/python2.7/email/message.pyRTks%	cCs�|jd�dkst�n|j�dkrD|d=d|d<n||kr^|||<dS|jd|d	|�}||=|||<x.|dD]"\}}|j||||�q�WdS(
sKSet the main type and subtype for the Content-Type header.

        type must be a string in the form "maintype/subtype", otherwise a
        ValueError is raised.

        This method replaces the Content-Type header, keeping all the
        parameters in place.  If requote is False, this leaves the existing
        header's quoting as is.  Otherwise, the parameters will be quoted (the
        default).

        An alternative header can be specified in the header argument.  When
        the Content-Type header is set, we'll always also add a MIME-Version
        header.
        R�iscontent-typesmime-versions1.0sMIME-VersionNR�R$(RR�RR�RZ(R.RFR�R�R�R�Ri((s#/sys/lib/python2.7/email/message.pytset_type�s	


cCset�}|jd|d�}||krB|jd|d�}n||krR|Stj|�j�S(s@Return the filename associated with the payload if present.

        The filename is extracted from the Content-Disposition header's
        `filename' parameter, and it is unquoted.  If that header is missing
        the `filename' parameter, this method falls back to looking for the
        `name' parameter.
        tfilenamescontent-dispositionRcscontent-type(RlR�Rtcollapse_rfc2231_valueR(R.RrRmR�((s#/sys/lib/python2.7/email/message.pytget_filename�s	cCs>t�}|jd|�}||kr+|Stj|�j�S(s�Return the boundary associated with the payload if present.

        The boundary is extracted from the Content-Type header's `boundary'
        parameter, and it is unquoted.
        tboundary(RlR�RR�trstrip(R.RrRmR�((s#/sys/lib/python2.7/email/message.pytget_boundary�s
	c
Cs�t�}|j|d�}||kr9tjd��ng}t}xY|D]Q\}}|j�dkr�|jdd|f�t}qL|j||f�qLW|s�|jdd|f�ng}x�|jD]�\}	}
|	j�dkr^g}xG|D]?\}}
|
dkr$|j|�q�jd||
f�q�j|	t	j
|�f�q�|j|	|
f�q�W||_dS(s�Set the boundary parameter in Content-Type to 'boundary'.

        This is subtly different than deleting the Content-Type header and
        adding a new one with a new boundary parameter via add_header().  The
        main difference is that using the set_boundary() method preserves the
        order of the Content-Type header in the original message.

        HeaderParseError is raised if the message has no Content-Type header.
        scontent-typesNo Content-Type header foundR�s"%s"R@s%s=%sN(RlR�RtHeaderParseErrortFalseRRR2R&RxRy(
R.R�RmR�t	newparamstfoundptpktpvRgthRiR}Rh((s#/sys/lib/python2.7/email/message.pytset_boundary�s0
		cCs�t�}|jd|�}||kr+|St|t�r�|dpGd}y t|d|�jd�}Wq�ttfk
r�|d}q�Xny4t|t�r�t|d�}n|jd�}Wntk
r�|SX|j	�S(s�Return the charset parameter of the Content-Type header.

        The returned string is always coerced to lower case.  If there is no
        Content-Type header, or if that header has no charset parameter,
        failobj is returned.
        RRisus-asciii(
RlR�RRR[R\tLookupErrortUnicodeErrorR^R(R.RrRmRRtpcharset((s#/sys/lib/python2.7/email/message.pytget_content_charset�s"	 
cCs&g|j�D]}|j|�^q
S(s�Return a list containing the charset(s) used in this message.

        The returned list of items describes the Content-Type headers'
        charset parameter for this message and all the subparts in its
        payload.

        Each item will either be a string (the value of the charset parameter
        in the Content-Type header of that part) or the value of the
        'failobj' parameter (defaults to None), if the part does not have a
        main MIME type of "text", or the charset is not defined.

        The list will contain one string for each part of the message, plus
        one for the container message (i.e. self), so that a non-multipart
        message will still return a list of length 1.
        (twalkR�(R.Rrtpart((s#/sys/lib/python2.7/email/message.pytget_charsets
si�(R�N(0t__name__t
__module__t__doc__R/R3R�R1R;R<R=R?RRPRSRQRaRbRdRfRjRkRnRoRpRqRGRsRXR�R�R�R�R�R�R�R2R�R�RZRTR�R�R�R�R�R�temail.iteratorsR�(((s#/sys/lib/python2.7/email/message.pyR\sX						2
	-								
	
	
												#/ 
	-((R�t__all__treRLRJtwarningst	cStringIORt
email.charsetRVRRRxtcompileRRRR2RR#R%R(((s#/sys/lib/python2.7/email/message.pyt<module>s 		
		

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].