1 # Copyright (C) 2002 Python Software Foundation
2 # Author: barry@zope.com
4 """Module containing compatibility functions for Python 2.1.
7 from cStringIO
import StringIO
8 from types
import StringType
, UnicodeType
15 # This function will become a method of the Message class
17 """Walk over the message tree, yielding each subpart.
19 The walk is performed in depth-first order. This method is a
24 if self
.is_multipart():
25 for subpart
in self
.get_payload():
26 parts
.extend(subpart
.walk())
30 # Python 2.2 spells floor division //
32 """Do a floor division, i/j."""
37 return isinstance(obj
, StringType
) or isinstance(obj
, UnicodeType
)
41 # These two functions are imported into the Iterators.py interface module.
42 # The Python 2.2 version uses generators for efficiency.
43 def body_line_iterator(msg
, decode
=False):
44 """Iterate over the parts, returning string payloads line-by-line.
46 Optional decode (default False) is passed through to .get_payload().
49 for subpart
in msg
.walk():
50 payload
= subpart
.get_payload(decode
=decode
)
51 if _isstring(payload
):
52 for line
in StringIO(payload
).readlines():
57 def typed_subpart_iterator(msg
, maintype
='text', subtype
=None):
58 """Iterate over the subparts with a given MIME type.
60 Use `maintype' as the main MIME type to match against; this defaults to
61 "text". Optional `subtype' is the MIME subtype to match against; if
62 omitted, only the main type is matched.
65 for subpart
in msg
.walk():
66 if subpart
.get_content_maintype() == maintype
:
67 if subtype
is None or subpart
.get_content_subtype() == subtype
: