Results of a rewrite pass
[python/dscho.git] / Lib / email / _compat22.py
bloba05451f25d9637a40cdd63c0bc27edc79455a21a
1 # Copyright (C) 2002 Python Software Foundation
2 # Author: barry@zope.com
4 """Module containing compatibility functions for Python 2.1.
5 """
7 from __future__ import generators
8 from __future__ import division
9 from cStringIO import StringIO
10 from types import StringTypes
14 # This function will become a method of the Message class
15 def walk(self):
16 """Walk over the message tree, yielding each subpart.
18 The walk is performed in depth-first order. This method is a
19 generator.
20 """
21 yield self
22 if self.is_multipart():
23 for subpart in self.get_payload():
24 for subsubpart in subpart.walk():
25 yield subsubpart
28 # Python 2.2 spells floor division //
29 def _floordiv(i, j):
30 """Do a floor division, i/j."""
31 return i // j
34 def _isstring(obj):
35 return isinstance(obj, StringTypes)
39 # These two functions are imported into the Iterators.py interface module.
40 # The Python 2.2 version uses generators for efficiency.
41 def body_line_iterator(msg):
42 """Iterate over the parts, returning string payloads line-by-line."""
43 for subpart in msg.walk():
44 payload = subpart.get_payload()
45 if _isstring(payload):
46 for line in StringIO(payload):
47 yield line
50 def typed_subpart_iterator(msg, maintype='text', subtype=None):
51 """Iterate over the subparts with a given MIME type.
53 Use `maintype' as the main MIME type to match against; this defaults to
54 "text". Optional `subtype' is the MIME subtype to match against; if
55 omitted, only the main type is matched.
56 """
57 for subpart in msg.walk():
58 if subpart.get_main_type('text') == maintype:
59 if subtype is None or subpart.get_subtype('plain') == subtype:
60 yield subpart