home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / Texte / scribus / scribus-1.3.3.9-win32-install.exe / lib / email / MIMEAudio.py < prev    next >
Text File  |  2004-10-15  |  3KB  |  73 lines

  1. # Copyright (C) 2001-2004 Python Software Foundation
  2. # Author: Anthony Baxter
  3. # Contact: email-sig@python.org
  4.  
  5. """Class representing audio/* type MIME documents."""
  6.  
  7. import sndhdr
  8. from cStringIO import StringIO
  9.  
  10. from email import Errors
  11. from email import Encoders
  12. from email.MIMENonMultipart import MIMENonMultipart
  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.  
  41. class MIMEAudio(MIMENonMultipart):
  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
  50.         subtype will be automatically included in the Content-Type header.
  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
  59.         Content-Transfer-Encoding or other headers to the message as
  60.         necessary.  The default encoding is Base64.
  61.  
  62.         Any additional keyword arguments are passed to the base class
  63.         constructor, which turns them into parameters on the Content-Type
  64.         header.
  65.         """
  66.         if _subtype is None:
  67.             _subtype = _whatsnd(_audiodata)
  68.         if _subtype is None:
  69.             raise TypeError('Could not find audio MIME subtype')
  70.         MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
  71.         self.set_payload(_audiodata)
  72.         _encoder(self)
  73.