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