Barry Warsaw | 40ef006 | 2006-03-18 15:41:53 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2006 Python Software Foundation |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 2 | # Author: Anthony Baxter |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 3 | # Contact: email-sig@python.org |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 5 | """Class representing audio/* type MIME documents.""" |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 6 | |
Barry Warsaw | 40ef006 | 2006-03-18 15:41:53 +0000 | [diff] [blame] | 7 | __all__ = ['MIMEAudio'] |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 40ef006 | 2006-03-18 15:41:53 +0000 | [diff] [blame] | 9 | import sndhdr |
| 10 | |
| 11 | from cStringIO import StringIO |
| 12 | from email import encoders |
| 13 | from email.mime.nonmultipart import MIMENonMultipart |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | |
| 17 | _sndhdr_MIMEmap = {'au' : 'basic', |
| 18 | 'wav' :'x-wav', |
| 19 | 'aiff':'x-aiff', |
| 20 | 'aifc':'x-aiff', |
| 21 | } |
| 22 | |
| 23 | # There are others in sndhdr that don't have MIME types. :( |
| 24 | # Additional ones to be added to sndhdr? midi, mp3, realaudio, wma?? |
| 25 | def _whatsnd(data): |
| 26 | """Try to identify a sound file type. |
| 27 | |
| 28 | sndhdr.what() has a pretty cruddy interface, unfortunately. This is why |
| 29 | we re-do it here. It would be easier to reverse engineer the Unix 'file' |
| 30 | command and use the standard 'magic' file, as shipped with a modern Unix. |
| 31 | """ |
| 32 | hdr = data[:512] |
| 33 | fakefile = StringIO(hdr) |
| 34 | for testfn in sndhdr.tests: |
| 35 | res = testfn(hdr, fakefile) |
| 36 | if res is not None: |
| 37 | return _sndhdr_MIMEmap.get(res[0]) |
| 38 | return None |
| 39 | |
| 40 | |
| 41 | |
Barry Warsaw | 524af6f | 2002-06-02 19:05:08 +0000 | [diff] [blame] | 42 | class MIMEAudio(MIMENonMultipart): |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 43 | """Class for generating audio/* MIME documents.""" |
| 44 | |
| 45 | def __init__(self, _audiodata, _subtype=None, |
Barry Warsaw | 40ef006 | 2006-03-18 15:41:53 +0000 | [diff] [blame] | 46 | _encoder=encoders.encode_base64, **_params): |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 47 | """Create an audio/* type MIME document. |
| 48 | |
| 49 | _audiodata is a string containing the raw audio data. If this data |
| 50 | can be decoded by the standard Python `sndhdr' module, then the |
Barry Warsaw | 03a7559 | 2002-09-30 21:29:10 +0000 | [diff] [blame] | 51 | subtype will be automatically included in the Content-Type header. |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 52 | Otherwise, you can specify the specific audio subtype via the |
| 53 | _subtype parameter. If _subtype is not given, and no subtype can be |
| 54 | guessed, a TypeError is raised. |
| 55 | |
| 56 | _encoder is a function which will perform the actual encoding for |
| 57 | transport of the image data. It takes one argument, which is this |
| 58 | Image instance. It should use get_payload() and set_payload() to |
| 59 | change the payload to the encoded form. It should also add any |
Barry Warsaw | 03a7559 | 2002-09-30 21:29:10 +0000 | [diff] [blame] | 60 | Content-Transfer-Encoding or other headers to the message as |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 61 | necessary. The default encoding is Base64. |
| 62 | |
| 63 | Any additional keyword arguments are passed to the base class |
Barry Warsaw | 03a7559 | 2002-09-30 21:29:10 +0000 | [diff] [blame] | 64 | constructor, which turns them into parameters on the Content-Type |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 65 | header. |
| 66 | """ |
| 67 | if _subtype is None: |
| 68 | _subtype = _whatsnd(_audiodata) |
| 69 | if _subtype is None: |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 70 | raise TypeError('Could not find audio MIME subtype') |
Barry Warsaw | 524af6f | 2002-06-02 19:05:08 +0000 | [diff] [blame] | 71 | MIMENonMultipart.__init__(self, 'audio', _subtype, **_params) |
Barry Warsaw | 53f8fe4 | 2001-10-09 19:41:18 +0000 | [diff] [blame] | 72 | self.set_payload(_audiodata) |
| 73 | _encoder(self) |