Viewing file: mbox.py (4.09 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
"""@package mbox Abstract an mbox file.
This package contains two classes mail which abstracts a single email and mbox which abstracts an mbox file as a sequence of mails """
class mail : """ Abstract an email.
This is a simple abstraction of an email. """ def __init__(self, frm) : """ Initialize an instance setting the instance vars to defalult values. """ self.from_line = frm self.text = '' self.body_offset = -1
def accumulate(self, line) : """ Append a line to the mail.
Append the next line to the mail, and check for the break between the headers and the body """ self.text += line if ((line == '\n') | (line == '\r\n')) & (self.body_offset < 0) : self.body_offset = len(self.text) def valid(self) : """ Test for valid mail.
Return True if we have text and the text has a body """ return (self.text != '') & (self.body_offset > 0)
def fetch_header(self, header) : """ Fetch a given header from the mail.
Search the mail for the specified header and return it, unfolding it as necessary. Note that if there are multiple headers with the same name, this will only return the first occurance. """ start = 0 while True : idx = self.text.find(header, start, self.body_offset) if idx < 0 : return None if (idx == 0) | (self.text[idx-1] == '\n') : st1 = idx hdr = '' while not hdr : eidx = self.text.find('\n', st1, self.body_offset) if eidx < 0 : return None if self.text[eidx+1] != ' ': hdr = self.text[idx:eidx] else : st1 = eidx+1 hdr.replace('\r', '') hdr.replace('\n', '') return hdr else : start = idx+1
class mbox : """ Abstract an mbox file.
This is a simple abstraction of an mbox file, set up as an iterator """ def __init__(self, path) : """ Initalize a new instance
Set our instance vars and open the file.
Keyword arguments: path -- the path to the file. """ self.last_line_empty = False self.from_line = '' self.rfc822 = '' self.mbox_file = None self.mbox_file = open(path, 'r')
def next_mail(self) : """ Get the next mail from the file. Return the next mail int the file as a mail object, or None if no more mails are available. """ if self.mbox_file is None : return None while not self.from_line : line1 = self.mbox_file.readline() if not line1 : self.mbox_file.close() self.mbox_file = None return None if line1[:5] == 'From ' : self.from_line = line1 amail = mail(self.from_line)
self.from_line = '' while (not self.from_line) & (self.mbox_file is not None) : line = self.mbox_file.readline() if line : if (line[:5] == 'From ') & self.last_line_empty : self.from_line = line else : self.last_line_empty = ((line == '\n') | (line == '\r\n') ) amail.accumulate(line) else : self.mbox_file.close() self.mbox_file = None return amail
def __iter__(self) : """ Allow this object to be used as an iterator """ return self
def next(self) : """ Wrap next_mail so we can use it as an iterator """ m = self.next_mail() if m is None : raise StopIteration else : return m
def __del__(self): """ On destruction, make sure our file is closed """ if self.mbox_file is not None: self.mbox_file.close()
|