Your IP : 3.144.95.167
�
^
bc @ sl d Z d d l Z d g Z d d l Z e j d e d � d d d � � YZ e d k rh d d l Z n d S( s� Generic MIME writer.
This module defines the class MimeWriter. The MimeWriter class implements
a basic formatter for creating MIME multi-part files. It doesn't seek around
the output file nor does it use large amounts of buffer space. You must write
the parts out in the order that they should occur in the final file.
MimeWriter does buffer the headers you add, allowing you to rearrange their
order.
i����Nt
MimeWritersB the MimeWriter module is deprecated; use the email package insteadi c B s_ e Z d Z d � Z d d � Z d � Z g d d � Z d
g d d � Z d � Z d � Z
RS( sO Generic MIME writer.
Methods:
__init__()
addheader()
flushheaders()
startbody()
startmultipartbody()
nextpart()
lastpart()
A MIME writer is much more primitive than a MIME parser. It
doesn't seek around on the output file, and it doesn't use large
amounts of buffer space, so you have to write the parts in the
order they should occur on the output file. It does buffer the
headers you add, allowing you to rearrange their order.
General usage is:
f = <open the output file>
w = MimeWriter(f)
...call w.addheader(key, value) 0 or more times...
followed by either:
f = w.startbody(content_type)
...call f.write(data) for body data...
or:
w.startmultipartbody(subtype)
for each part:
subwriter = w.nextpart()
...use the subwriter's methods to create the subpart...
w.lastpart()
The subwriter is another MimeWriter instance, and should be
treated in the same way as the toplevel MimeWriter. This way,
writing recursive body parts is easy.
Warning: don't forget to call lastpart()!
XXX There should be more state so calls made in the wrong order
are detected.
Some special cases:
- startbody() just returns the file passed to the constructor;
but don't use this knowledge, as it may be changed.
- startmultipartbody() actually returns a file as well;
this can be used to write the initial 'if you can read this your
mailer is not MIME-aware' message.
- If you call flushheaders(), the headers accumulated so far are
written out (and forgotten); this is useful if you don't need a
body part at all, e.g. for a subpart of type message/rfc822
that's (mis)used to store some header-like information.
- Passing a keyword argument 'prefix=<flag>' to addheader(),
start*body() affects where the header is inserted; 0 means
append at the end, 1 means insert at the start; default is
append for addheader(), but insert for start*body(), which use
it to determine where the Content-Type header goes.
c C s | | _ g | _ d S( N( t _fpt _headers( t selft fp( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt __init__\ s i c C s� | j d � } x | r- | d r- | d =q Wx | rL | d rL | d =q1 Wx5 t d t | � � D] } d | | j � | | <qc Wd j | � d } | d | } | r� | j j d | � n | j j | � d S( sH Add a header line to the MIME message.
The key is the name of the header, where the value obviously provides
the value of the header. The optional argument prefix determines
where the header is inserted; 0 means append at the end, 1 means
insert at the start. The default is to append.
s
i����i i s s : N( t splitt ranget lent stript joinR t insertt append( R t keyt valuet prefixt linest it line( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt addheader` s c C s | j j | j � g | _ d S( s� Writes out and forgets all headers accumulated so far.
This is useful if you don't need a body part at all; for example,
for a subpart of type message/rfc822 that's (mis)used to store some
header-like information.
N( R t
writelinesR ( R ( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt flushheadersu s i c C sb x( | D] \ } } | d | | f } q W| j d | d | �| j � | j j d � | j S( s� Returns a file-like object for writing the body of the message.
The content-type is set to the provided ctype, and the optional
parameter, plist, provides additional parameters for the
content-type declaration. The optional argument prefix determines
where the header is inserted; 0 means append at the end, 1 means
insert at the start. The default is to insert at the start.
s
;
%s="%s"s Content-TypeR s
( R R R t write( R t ctypet plistR t nameR ( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt startbody� s
c C s? | p t j � | _ | j d | d | j f g | d | �S( sr Returns a file-like object for writing the body of the message.
Additionally, this method initializes the multi-part code, where the
subtype parameter provides the multipart subtype, the boundary
parameter may provide a user-defined boundary specification, and the
plist parameter provides optional parameters for the subtype. The
optional argument, prefix, determines where the header is inserted;
0 means append at the end, 1 means insert at the start. The default
is to insert at the start. Subparts should be created using the
nextpart() method.
s
multipart/t boundaryR ( t mimetoolst choose_boundaryt _boundaryR ( R t subtypeR R R ( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt startmultipartbody� s
c C s+ | j j d | j d � | j | j � S( sj Returns a new instance of MimeWriter which represents an
individual part in a multipart message.
This may be used to write the part as well as used for creating
recursively complex multipart messages. The message must first be
initialized with the startmultipartbody() method before using the
nextpart() method.
s
--s
( R R R t __class__( R ( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt nextpart� s
c C s | j j d | j d � d S( s� This is used to designate the last part of a multipart message.
It should always be used when writing multipart messages.
s
--s --
N( R R R ( R ( ( s/ /opt/alt/python27/lib64/python2.7/MimeWriter.pyt lastpart� s N( t __name__t
__module__t __doc__R R R R t NoneR R"