Welcome, guest | Sign In | My Account | Store | Cart
'''
A simple mbox read-only mailbox generator (Python >= 2.2).

Usage:
import sys
mbox = mbox_generator(sys.stdin)
for msg in mbox:
        print msg['Subject']
'''
# uncomment the following line on Python 2.2
#from __future__ import generators

import email.Parser

def mbox_generator(input):
        '''Yield each message found in 'input'.'''
        assert type(input) is file
        parsestr = email.Parser.Parser().parsestr
        data = []
        while True:
                line = input.readline()
                if line[:5] == 'From ' or line == '':
                        if data:
                                yield parsestr(''.join(data))
                                data = []
                        elif line == '':
                                raise StopIteration
                else:   
                        data.append(line)

History