Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 2 | # Author: barry@zope.com (Barry Warsaw) |
| 3 | |
| 4 | """Class representing image/* type MIME documents. |
| 5 | """ |
| 6 | |
| 7 | import imghdr |
| 8 | |
Barry Warsaw | 524af6f | 2002-06-02 19:05:08 +0000 | [diff] [blame] | 9 | from email import Errors |
| 10 | from email import Encoders |
| 11 | from email.MIMENonMultipart import MIMENonMultipart |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 12 | |
| 13 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | 524af6f | 2002-06-02 19:05:08 +0000 | [diff] [blame] | 15 | class MIMEImage(MIMENonMultipart): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 16 | """Class for generating image/* type MIME documents.""" |
| 17 | |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 18 | def __init__(self, _imagedata, _subtype=None, |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 19 | _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 Warsaw | 0031982 | 2002-09-30 22:15:00 +0000 | [diff] [blame] | 24 | subtype will be automatically included in the Content-Type header. |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 25 | Otherwise, you can specify the specific image subtype via the _subtype |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 26 | 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 Warsaw | 0031982 | 2002-09-30 22:15:00 +0000 | [diff] [blame] | 32 | Content-Transfer-Encoding or other headers to the message as |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 33 | necessary. The default encoding is Base64. |
| 34 | |
| 35 | Any additional keyword arguments are passed to the base class |
Barry Warsaw | 0031982 | 2002-09-30 22:15:00 +0000 | [diff] [blame] | 36 | constructor, which turns them into parameters on the Content-Type |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 37 | header. |
| 38 | """ |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 39 | 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 Warsaw | 524af6f | 2002-06-02 19:05:08 +0000 | [diff] [blame] | 43 | MIMENonMultipart.__init__(self, 'image', _subtype, **_params) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 44 | self.set_payload(_imagedata) |
| 45 | _encoder(self) |