ยปCore Development>Code coverage>Lib/email/mime/audio.py

Python code coverage for Lib/email/mime/audio.py

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