You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Generic MIME writer.
  2. Classes:
  3. MimeWriter - the only thing here.
  4. """
  5. import mimetools
  6. __all__ = ["MimeWriter"]
  7. class MimeWriter:
  8. """Generic MIME writer.
  9. Methods:
  10. __init__()
  11. addheader()
  12. flushheaders()
  13. startbody()
  14. startmultipartbody()
  15. nextpart()
  16. lastpart()
  17. A MIME writer is much more primitive than a MIME parser. It
  18. doesn't seek around on the output file, and it doesn't use large
  19. amounts of buffer space, so you have to write the parts in the
  20. order they should occur on the output file. It does buffer the
  21. headers you add, allowing you to rearrange their order.
  22. General usage is:
  23. f = <open the output file>
  24. w = MimeWriter(f)
  25. ...call w.addheader(key, value) 0 or more times...
  26. followed by either:
  27. f = w.startbody(content_type)
  28. ...call f.write(data) for body data...
  29. or:
  30. w.startmultipartbody(subtype)
  31. for each part:
  32. subwriter = w.nextpart()
  33. ...use the subwriter's methods to create the subpart...
  34. w.lastpart()
  35. The subwriter is another MimeWriter instance, and should be
  36. treated in the same way as the toplevel MimeWriter. This way,
  37. writing recursive body parts is easy.
  38. Warning: don't forget to call lastpart()!
  39. XXX There should be more state so calls made in the wrong order
  40. are detected.
  41. Some special cases:
  42. - startbody() just returns the file passed to the constructor;
  43. but don't use this knowledge, as it may be changed.
  44. - startmultipartbody() actually returns a file as well;
  45. this can be used to write the initial 'if you can read this your
  46. mailer is not MIME-aware' message.
  47. - If you call flushheaders(), the headers accumulated so far are
  48. written out (and forgotten); this is useful if you don't need a
  49. body part at all, e.g. for a subpart of type message/rfc822
  50. that's (mis)used to store some header-like information.
  51. - Passing a keyword argument 'prefix=<flag>' to addheader(),
  52. start*body() affects where the header is inserted; 0 means
  53. append at the end, 1 means insert at the start; default is
  54. append for addheader(), but insert for start*body(), which use
  55. it to determine where the Content-Type header goes.
  56. """
  57. def __init__(self, fp):
  58. self._fp = fp
  59. self._headers = []
  60. def addheader(self, key, value, prefix=0):
  61. lines = value.split("\n")
  62. while lines and not lines[-1]: del lines[-1]
  63. while lines and not lines[0]: del lines[0]
  64. for i in range(1, len(lines)):
  65. lines[i] = " " + lines[i].strip()
  66. value = "\n".join(lines) + "\n"
  67. line = key + ": " + value
  68. if prefix:
  69. self._headers.insert(0, line)
  70. else:
  71. self._headers.append(line)
  72. def flushheaders(self):
  73. self._fp.writelines(self._headers)
  74. self._headers = []
  75. def startbody(self, ctype, plist=[], prefix=1):
  76. for name, value in plist:
  77. ctype = ctype + ';\n %s=\"%s\"' % (name, value)
  78. self.addheader("Content-Type", ctype, prefix=prefix)
  79. self.flushheaders()
  80. self._fp.write("\n")
  81. return self._fp
  82. def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
  83. self._boundary = boundary or mimetools.choose_boundary()
  84. return self.startbody("multipart/" + subtype,
  85. [("boundary", self._boundary)] + plist,
  86. prefix=prefix)
  87. def nextpart(self):
  88. self._fp.write("\n--" + self._boundary + "\n")
  89. return self.__class__(self._fp)
  90. def lastpart(self):
  91. self._fp.write("\n--" + self._boundary + "--\n")
  92. if __name__ == '__main__':
  93. import test.test_MimeWriter