jpayne@68
|
1 # Copyright (C) 2001-2006 Python Software Foundation
|
jpayne@68
|
2 # Author: Barry Warsaw
|
jpayne@68
|
3 # Contact: email-sig@python.org
|
jpayne@68
|
4
|
jpayne@68
|
5 """Various types of useful iterators and generators."""
|
jpayne@68
|
6
|
jpayne@68
|
7 __all__ = [
|
jpayne@68
|
8 'body_line_iterator',
|
jpayne@68
|
9 'typed_subpart_iterator',
|
jpayne@68
|
10 'walk',
|
jpayne@68
|
11 # Do not include _structure() since it's part of the debugging API.
|
jpayne@68
|
12 ]
|
jpayne@68
|
13
|
jpayne@68
|
14 import sys
|
jpayne@68
|
15 from io import StringIO
|
jpayne@68
|
16
|
jpayne@68
|
17
|
jpayne@68
|
18
|
jpayne@68
|
19 # This function will become a method of the Message class
|
jpayne@68
|
20 def walk(self):
|
jpayne@68
|
21 """Walk over the message tree, yielding each subpart.
|
jpayne@68
|
22
|
jpayne@68
|
23 The walk is performed in depth-first order. This method is a
|
jpayne@68
|
24 generator.
|
jpayne@68
|
25 """
|
jpayne@68
|
26 yield self
|
jpayne@68
|
27 if self.is_multipart():
|
jpayne@68
|
28 for subpart in self.get_payload():
|
jpayne@68
|
29 yield from subpart.walk()
|
jpayne@68
|
30
|
jpayne@68
|
31
|
jpayne@68
|
32
|
jpayne@68
|
33 # These two functions are imported into the Iterators.py interface module.
|
jpayne@68
|
34 def body_line_iterator(msg, decode=False):
|
jpayne@68
|
35 """Iterate over the parts, returning string payloads line-by-line.
|
jpayne@68
|
36
|
jpayne@68
|
37 Optional decode (default False) is passed through to .get_payload().
|
jpayne@68
|
38 """
|
jpayne@68
|
39 for subpart in msg.walk():
|
jpayne@68
|
40 payload = subpart.get_payload(decode=decode)
|
jpayne@68
|
41 if isinstance(payload, str):
|
jpayne@68
|
42 yield from StringIO(payload)
|
jpayne@68
|
43
|
jpayne@68
|
44
|
jpayne@68
|
45 def typed_subpart_iterator(msg, maintype='text', subtype=None):
|
jpayne@68
|
46 """Iterate over the subparts with a given MIME type.
|
jpayne@68
|
47
|
jpayne@68
|
48 Use `maintype' as the main MIME type to match against; this defaults to
|
jpayne@68
|
49 "text". Optional `subtype' is the MIME subtype to match against; if
|
jpayne@68
|
50 omitted, only the main type is matched.
|
jpayne@68
|
51 """
|
jpayne@68
|
52 for subpart in msg.walk():
|
jpayne@68
|
53 if subpart.get_content_maintype() == maintype:
|
jpayne@68
|
54 if subtype is None or subpart.get_content_subtype() == subtype:
|
jpayne@68
|
55 yield subpart
|
jpayne@68
|
56
|
jpayne@68
|
57
|
jpayne@68
|
58
|
jpayne@68
|
59 def _structure(msg, fp=None, level=0, include_default=False):
|
jpayne@68
|
60 """A handy debugging aid"""
|
jpayne@68
|
61 if fp is None:
|
jpayne@68
|
62 fp = sys.stdout
|
jpayne@68
|
63 tab = ' ' * (level * 4)
|
jpayne@68
|
64 print(tab + msg.get_content_type(), end='', file=fp)
|
jpayne@68
|
65 if include_default:
|
jpayne@68
|
66 print(' [%s]' % msg.get_default_type(), file=fp)
|
jpayne@68
|
67 else:
|
jpayne@68
|
68 print(file=fp)
|
jpayne@68
|
69 if msg.is_multipart():
|
jpayne@68
|
70 for subpart in msg.get_payload():
|
jpayne@68
|
71 _structure(subpart, fp, level+1, include_default)
|