Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
MimeWriter Class Reference

Public Member Functions

def __init__
 
def addheader
 
def flushheaders
 
def startbody
 
def startmultipartbody
 
def nextpart
 
def lastpart
 

Detailed Description

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.

Definition at line 14 of file MimeWriter.py.

Constructor & Destructor Documentation

def __init__ (   self,
  fp 
)

Definition at line 84 of file MimeWriter.py.

References MimeWriter._fp, and MimeWriter._headers.

84 
85  def __init__(self, fp):
86  self._fp = fp
87  self._headers = []

Member Function Documentation

def addheader (   self,
  key,
  value,
  prefix = 0 
)

Definition at line 88 of file MimeWriter.py.

References dospath.join(), and string.strip().

88 
89  def addheader(self, key, value, prefix=0):
90  lines = value.split("\n")
91  while lines and not lines[-1]: del lines[-1]
92  while lines and not lines[0]: del lines[0]
93  for i in range(1, len(lines)):
94  lines[i] = " " + lines[i].strip()
95  value = "\n".join(lines) + "\n"
96  line = key + ": " + value
97  if prefix:
98  self._headers.insert(0, line)
99  else:
100  self._headers.append(line)
def flushheaders (   self)

Definition at line 101 of file MimeWriter.py.

References MimeWriter._headers.

102  def flushheaders(self):
103  self._fp.writelines(self._headers)
104  self._headers = []
def lastpart (   self)

Definition at line 123 of file MimeWriter.py.

References MimeWriter._boundary.

124  def lastpart(self):
125  self._fp.write("\n--" + self._boundary + "--\n")
126 
def nextpart (   self)

Definition at line 119 of file MimeWriter.py.

References SymbolTable.__class__, MimeWriter._boundary, and MimeWriter._fp.

120  def nextpart(self):
121  self._fp.write("\n--" + self._boundary + "\n")
122  return self.__class__(self._fp)
def startbody (   self,
  ctype,
  plist = [],
  prefix = 1 
)

Definition at line 105 of file MimeWriter.py.

References MimeWriter._fp, MimeWriter.addheader(), and MimeWriter.flushheaders().

106  def startbody(self, ctype, plist=[], prefix=1):
107  for name, value in plist:
108  ctype = ctype + ';\n %s=\"%s\"' % (name, value)
109  self.addheader("Content-Type", ctype, prefix=prefix)
110  self.flushheaders()
111  self._fp.write("\n")
112  return self._fp
def startmultipartbody (   self,
  subtype,
  boundary = None,
  plist = [],
  prefix = 1 
)

Definition at line 113 of file MimeWriter.py.

References MimeWriter._boundary, mimetools.choose_boundary(), and MimeWriter.startbody().

114  def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
115  self._boundary = boundary or mimetools.choose_boundary()
116  return self.startbody("multipart/" + subtype,
117  [("boundary", self._boundary)] + plist,
118  prefix=prefix)

The documentation for this class was generated from the following file: