blob: 998530f52a2f5bd178cb33fe223ef6e9d009d684 [file] [log] [blame]
Barry Warsawba925802001-09-23 03:17:28 +00001# Copyright (C) 2001 Python Software Foundation
2# Author: barry@zope.com (Barry Warsaw)
3
4"""Various types of useful iterators and generators.
5"""
6
7from __future__ import generators
8from cStringIO import StringIO
9from types import StringType
10
11
12
13def body_line_iterator(msg):
14 """Iterator over the parts, returning the lines in a string payload."""
15 for subpart in msg.walk():
16 payload = subpart.get_payload()
17 if type(payload) is StringType:
18 for line in StringIO(payload):
19 yield line
20
21
22
23def typed_subpart_iterator(msg, major='text', minor=None):
24 """Iterator over the subparts with a given MIME type.
25
26 Use `major' as the main MIME type to match against; this defaults to
27 "text". Optional `minor' is the MIME subtype to match against; if
28 omitted, only the main type is matched.
29 """
30 for subpart in msg.walk():
31 if subpart.get_main_type() == major:
32 if minor is None or subpart.get_subtype() == minor:
33 yield subpart