Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001 Python Software Foundation |
| 2 | # Author: barry@zope.com (Barry Warsaw) |
| 3 | |
| 4 | """Class representing image/* type MIME documents. |
| 5 | """ |
| 6 | |
| 7 | import imghdr |
| 8 | |
| 9 | # Intrapackage imports |
| 10 | import MIMEBase |
| 11 | import Errors |
| 12 | import Encoders |
| 13 | |
| 14 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 16 | class MIMEImage(MIMEBase.MIMEBase): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 17 | """Class for generating image/* type MIME documents.""" |
| 18 | |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 19 | def __init__(self, _imagedata, _subtype=None, |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 20 | _encoder=Encoders.encode_base64, **_params): |
| 21 | """Create an image/* type MIME document. |
| 22 | |
| 23 | _imagedata is a string containing the raw image data. If this data |
| 24 | can be decoded by the standard Python `imghdr' module, then the |
| 25 | subtype will be automatically included in the Content-Type: header. |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 26 | Otherwise, you can specify the specific image subtype via the _subtype |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 27 | parameter. |
| 28 | |
| 29 | _encoder is a function which will perform the actual encoding for |
| 30 | transport of the image data. It takes one argument, which is this |
| 31 | Image instance. It should use get_payload() and set_payload() to |
| 32 | change the payload to the encoded form. It should also add any |
| 33 | Content-Transfer-Encoding: or other headers to the message as |
| 34 | necessary. The default encoding is Base64. |
| 35 | |
| 36 | Any additional keyword arguments are passed to the base class |
| 37 | constructor, which turns them into parameters on the Content-Type: |
| 38 | header. |
| 39 | """ |
Barry Warsaw | 3dd978d | 2001-09-26 05:34:30 +0000 | [diff] [blame] | 40 | if _subtype is None: |
| 41 | _subtype = imghdr.what(None, _imagedata) |
| 42 | if _subtype is None: |
| 43 | raise TypeError, 'Could not guess image MIME subtype' |
| 44 | MIMEBase.MIMEBase.__init__(self, 'image', _subtype, **_params) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 45 | self.set_payload(_imagedata) |
| 46 | _encoder(self) |