PyXR

c:\python24\lib \ email \ MIMEAudio.py



0001 # Copyright (C) 2001-2004 Python Software Foundation
0002 # Author: Anthony Baxter
0003 # Contact: email-sig@python.org
0004 
0005 """Class representing audio/* type MIME documents."""
0006 
0007 import sndhdr
0008 from cStringIO import StringIO
0009 
0010 from email import Errors
0011 from email import Encoders
0012 from email.MIMENonMultipart import MIMENonMultipart
0013 
0014 
0015 
0016 _sndhdr_MIMEmap = {'au'  : 'basic',
0017                    'wav' :'x-wav',
0018                    'aiff':'x-aiff',
0019                    'aifc':'x-aiff',
0020                    }
0021 
0022 # There are others in sndhdr that don't have MIME types. :(
0023 # Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
0024 def _whatsnd(data):
0025     """Try to identify a sound file type.
0026 
0027     sndhdr.what() has a pretty cruddy interface, unfortunately.  This is why
0028     we re-do it here.  It would be easier to reverse engineer the Unix 'file'
0029     command and use the standard 'magic' file, as shipped with a modern Unix.
0030     """
0031     hdr = data[:512]
0032     fakefile = StringIO(hdr)
0033     for testfn in sndhdr.tests:
0034         res = testfn(hdr, fakefile)
0035         if res is not None:
0036             return _sndhdr_MIMEmap.get(res[0])
0037     return None
0038 
0039 
0040 
0041 class MIMEAudio(MIMENonMultipart):
0042     """Class for generating audio/* MIME documents."""
0043 
0044     def __init__(self, _audiodata, _subtype=None,
0045                  _encoder=Encoders.encode_base64, **_params):
0046         """Create an audio/* type MIME document.
0047 
0048         _audiodata is a string containing the raw audio data.  If this data
0049         can be decoded by the standard Python `sndhdr' module, then the
0050         subtype will be automatically included in the Content-Type header.
0051         Otherwise, you can specify  the specific audio subtype via the
0052         _subtype parameter.  If _subtype is not given, and no subtype can be
0053         guessed, a TypeError is raised.
0054 
0055         _encoder is a function which will perform the actual encoding for
0056         transport of the image data.  It takes one argument, which is this
0057         Image instance.  It should use get_payload() and set_payload() to
0058         change the payload to the encoded form.  It should also add any
0059         Content-Transfer-Encoding or other headers to the message as
0060         necessary.  The default encoding is Base64.
0061 
0062         Any additional keyword arguments are passed to the base class
0063         constructor, which turns them into parameters on the Content-Type
0064         header.
0065         """
0066         if _subtype is None:
0067             _subtype = _whatsnd(_audiodata)
0068         if _subtype is None:
0069             raise TypeError('Could not find audio MIME subtype')
0070         MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
0071         self.set_payload(_audiodata)
0072         _encoder(self)
0073 

Generated by PyXR 0.9.4
SourceForge.net Logo