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