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