home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / email / iterators.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.3 KB  |  75 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Various types of useful iterators and generators.'''
  5. __all__ = [
  6.     'body_line_iterator',
  7.     'typed_subpart_iterator',
  8.     'walk']
  9. import sys
  10. from cStringIO import StringIO
  11.  
  12. def walk(self):
  13.     '''Walk over the message tree, yielding each subpart.
  14.  
  15.     The walk is performed in depth-first order.  This method is a
  16.     generator.
  17.     '''
  18.     yield self
  19.     if self.is_multipart():
  20.         for subpart in self.get_payload():
  21.             for subsubpart in subpart.walk():
  22.                 yield subsubpart
  23.             
  24.         
  25.     
  26.  
  27.  
  28. def body_line_iterator(msg, decode = False):
  29.     '''Iterate over the parts, returning string payloads line-by-line.
  30.  
  31.     Optional decode (default False) is passed through to .get_payload().
  32.     '''
  33.     for subpart in msg.walk():
  34.         payload = subpart.get_payload(decode = decode)
  35.         if isinstance(payload, basestring):
  36.             for line in StringIO(payload):
  37.                 yield line
  38.             
  39.     
  40.  
  41.  
  42. def typed_subpart_iterator(msg, maintype = 'text', subtype = None):
  43.     '''Iterate over the subparts with a given MIME type.
  44.  
  45.     Use `maintype\' as the main MIME type to match against; this defaults to
  46.     "text".  Optional `subtype\' is the MIME subtype to match against; if
  47.     omitted, only the main type is matched.
  48.     '''
  49.     for subpart in msg.walk():
  50.         if subpart.get_content_maintype() == maintype:
  51.             if subtype is None or subpart.get_content_subtype() == subtype:
  52.                 yield subpart
  53.             
  54.         subpart.get_content_subtype() == subtype
  55.     
  56.  
  57.  
  58. def _structure(msg, fp = None, level = 0, include_default = False):
  59.     '''A handy debugging aid'''
  60.     if fp is None:
  61.         fp = sys.stdout
  62.     
  63.     tab = ' ' * level * 4
  64.     print >>fp, tab + msg.get_content_type(),
  65.     if include_default:
  66.         print >>fp, '[%s]' % msg.get_default_type()
  67.     else:
  68.         print >>fp
  69.     if msg.is_multipart():
  70.         for subpart in msg.get_payload():
  71.             _structure(subpart, fp, level + 1, include_default)
  72.         
  73.     
  74.  
  75.