body_line_iterator(): Accept optional decode argument, pass through to

Message.get_payload().
This commit is contained in:
Barry Warsaw 2003-03-11 04:41:35 +00:00
parent 52b39f5b47
commit 12dc230c00
2 changed files with 12 additions and 6 deletions

View file

@ -37,11 +37,14 @@ def _isstring(obj):
# These two functions are imported into the Iterators.py interface module. # These two functions are imported into the Iterators.py interface module.
# The Python 2.2 version uses generators for efficiency. # The Python 2.2 version uses generators for efficiency.
def body_line_iterator(msg): def body_line_iterator(msg, decode=False):
"""Iterate over the parts, returning string payloads line-by-line.""" """Iterate over the parts, returning string payloads line-by-line.
Optional decode (default False) is passed through to .get_payload().
"""
lines = [] lines = []
for subpart in msg.walk(): for subpart in msg.walk():
payload = subpart.get_payload() payload = subpart.get_payload(decode=decode)
if _isstring(payload): if _isstring(payload):
for line in StringIO(payload).readlines(): for line in StringIO(payload).readlines():
lines.append(line) lines.append(line)

View file

@ -38,10 +38,13 @@ def _isstring(obj):
# These two functions are imported into the Iterators.py interface module. # These two functions are imported into the Iterators.py interface module.
# The Python 2.2 version uses generators for efficiency. # The Python 2.2 version uses generators for efficiency.
def body_line_iterator(msg): def body_line_iterator(msg, decode=False):
"""Iterate over the parts, returning string payloads line-by-line.""" """Iterate over the parts, returning string payloads line-by-line.
Optional decode (default False) is passed through to .get_payload().
"""
for subpart in msg.walk(): for subpart in msg.walk():
payload = subpart.get_payload() payload = subpart.get_payload(decode=decode)
if _isstring(payload): if _isstring(payload):
for line in StringIO(payload): for line in StringIO(payload):
yield line yield line