blob: 266ec4c4095f5fe0482c054c24a05471af5dbc30 [file] [log] [blame]
Barry Warsawbb113862004-10-03 03:16:19 +00001# Copyright (C) 2001-2004 Python Software Foundation
Barry Warsaw53f8fe42001-10-09 19:41:18 +00002# Author: Anthony Baxter
Barry Warsawbb113862004-10-03 03:16:19 +00003# Contact: email-sig@python.org
Barry Warsaw53f8fe42001-10-09 19:41:18 +00004
Barry Warsawbb113862004-10-03 03:16:19 +00005"""Class representing audio/* type MIME documents."""
Barry Warsaw53f8fe42001-10-09 19:41:18 +00006
7import sndhdr
8from cStringIO import StringIO
9
Barry Warsaw524af6f2002-06-02 19:05:08 +000010from email import Errors
11from email import Encoders
12from email.MIMENonMultipart import MIMENonMultipart
Barry Warsaw53f8fe42001-10-09 19:41:18 +000013
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??
24def _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 Warsaw524af6f2002-06-02 19:05:08 +000041class MIMEAudio(MIMENonMultipart):
Barry Warsaw53f8fe42001-10-09 19:41:18 +000042 """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 Warsaw03a75592002-09-30 21:29:10 +000050 subtype will be automatically included in the Content-Type header.
Barry Warsaw53f8fe42001-10-09 19:41:18 +000051 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 Warsaw03a75592002-09-30 21:29:10 +000059 Content-Transfer-Encoding or other headers to the message as
Barry Warsaw53f8fe42001-10-09 19:41:18 +000060 necessary. The default encoding is Base64.
61
62 Any additional keyword arguments are passed to the base class
Barry Warsaw03a75592002-09-30 21:29:10 +000063 constructor, which turns them into parameters on the Content-Type
Barry Warsaw53f8fe42001-10-09 19:41:18 +000064 header.
65 """
66 if _subtype is None:
67 _subtype = _whatsnd(_audiodata)
68 if _subtype is None:
Barry Warsawbb113862004-10-03 03:16:19 +000069 raise TypeError('Could not find audio MIME subtype')
Barry Warsaw524af6f2002-06-02 19:05:08 +000070 MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
Barry Warsaw53f8fe42001-10-09 19:41:18 +000071 self.set_payload(_audiodata)
72 _encoder(self)