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