Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../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 cStringIO 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
for subsubpart in subpart.walk():
[28] Fix | Delete
yield subsubpart
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
[32] Fix | Delete
# These two functions are imported into the Iterators.py interface module.
[33] Fix | Delete
def body_line_iterator(msg, decode=False):
[34] Fix | Delete
"""Iterate over the parts, returning string payloads line-by-line.
[35] Fix | Delete
[36] Fix | Delete
Optional decode (default False) is passed through to .get_payload().
[37] Fix | Delete
"""
[38] Fix | Delete
for subpart in msg.walk():
[39] Fix | Delete
payload = subpart.get_payload(decode=decode)
[40] Fix | Delete
if isinstance(payload, basestring):
[41] Fix | Delete
for line in StringIO(payload):
[42] Fix | Delete
yield line
[43] Fix | Delete
[44] Fix | Delete
[45] Fix | Delete
def typed_subpart_iterator(msg, maintype='text', subtype=None):
[46] Fix | Delete
"""Iterate over the subparts with a given MIME type.
[47] Fix | Delete
[48] Fix | Delete
Use `maintype' as the main MIME type to match against; this defaults to
[49] Fix | Delete
"text". Optional `subtype' is the MIME subtype to match against; if
[50] Fix | Delete
omitted, only the main type is matched.
[51] Fix | Delete
"""
[52] Fix | Delete
for subpart in msg.walk():
[53] Fix | Delete
if subpart.get_content_maintype() == maintype:
[54] Fix | Delete
if subtype is None or subpart.get_content_subtype() == subtype:
[55] Fix | Delete
yield subpart
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
[59] Fix | Delete
def _structure(msg, fp=None, level=0, include_default=False):
[60] Fix | Delete
"""A handy debugging aid"""
[61] Fix | Delete
if fp is None:
[62] Fix | Delete
fp = sys.stdout
[63] Fix | Delete
tab = ' ' * (level * 4)
[64] Fix | Delete
print >> fp, tab + msg.get_content_type(),
[65] Fix | Delete
if include_default:
[66] Fix | Delete
print >> fp, '[%s]' % msg.get_default_type()
[67] Fix | Delete
else:
[68] Fix | Delete
print >> fp
[69] Fix | Delete
if msg.is_multipart():
[70] Fix | Delete
for subpart in msg.get_payload():
[71] Fix | Delete
_structure(subpart, fp, level+1, include_default)
[72] Fix | Delete
[73] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function