blob: 5306e537066d5dec4de9f182ea42e5d39602a5e6 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Class representing image/* type MIME documents.
5"""
6
7import imghdr
8
Barry Warsaw524af6f2002-06-02 19:05:08 +00009from email import Errors
10from email import Encoders
11from email.MIMENonMultipart import MIMENonMultipart
Barry Warsawba925802001-09-23 03:17:28 +000012
13
Barry Warsawe968ead2001-10-04 17:05:11 +000014
Barry Warsaw524af6f2002-06-02 19:05:08 +000015class MIMEImage(MIMENonMultipart):
Barry Warsawba925802001-09-23 03:17:28 +000016 """Class for generating image/* type MIME documents."""
17
Barry Warsaw3dd978d2001-09-26 05:34:30 +000018 def __init__(self, _imagedata, _subtype=None,
Barry Warsawba925802001-09-23 03:17:28 +000019 _encoder=Encoders.encode_base64, **_params):
20 """Create an image/* type MIME document.
21
22 _imagedata is a string containing the raw image data. If this data
23 can be decoded by the standard Python `imghdr' module, then the
Barry Warsaw00319822002-09-30 22:15:00 +000024 subtype will be automatically included in the Content-Type header.
Barry Warsaw3dd978d2001-09-26 05:34:30 +000025 Otherwise, you can specify the specific image subtype via the _subtype
Barry Warsawba925802001-09-23 03:17:28 +000026 parameter.
27
28 _encoder is a function which will perform the actual encoding for
29 transport of the image data. It takes one argument, which is this
30 Image instance. It should use get_payload() and set_payload() to
31 change the payload to the encoded form. It should also add any
Barry Warsaw00319822002-09-30 22:15:00 +000032 Content-Transfer-Encoding or other headers to the message as
Barry Warsawba925802001-09-23 03:17:28 +000033 necessary. The default encoding is Base64.
34
35 Any additional keyword arguments are passed to the base class
Barry Warsaw00319822002-09-30 22:15:00 +000036 constructor, which turns them into parameters on the Content-Type
Barry Warsawba925802001-09-23 03:17:28 +000037 header.
38 """
Barry Warsaw3dd978d2001-09-26 05:34:30 +000039 if _subtype is None:
40 _subtype = imghdr.what(None, _imagedata)
41 if _subtype is None:
42 raise TypeError, 'Could not guess image MIME subtype'
Barry Warsaw524af6f2002-06-02 19:05:08 +000043 MIMENonMultipart.__init__(self, 'image', _subtype, **_params)
Barry Warsawba925802001-09-23 03:17:28 +000044 self.set_payload(_imagedata)
45 _encoder(self)