| Joel Uckelman on Mon, 6 Dec 2004 12:30:57 -0600 (CST) |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| [nimh-dev] inc |
I was thinking about inc this morning, and rather than be blindsided by
finding a Python module which already does most of what we want, I poked
around in the library reference first. Here's what I found:
The module we want is 'mailbox'. I imagine that we want (at least) to read
two formats: mbox and Maildir. mailbox defaults to producing rfc822.Message
objects, but you can feed it a message factory function which will return
arbitrary things.
So, here's a sketch, adapted from the docs for the mailbox module:
import email
import email.Errors
import mailbox
# NB: we can make this return whatever we want;
# maybe files would be better than email.Messages (or our subclass)?
def make_email(fp):
try:
return email.message_from_file(fp)
except email.Errors.MessageParseError:
# Don't return None since that will
# stop the mailbox iterator
return ''
# TODO: read MH stuff
# TODO: figure out what to inc from (read .mh_profile:MailDrop)
# TODO: figure out what kind MailDrop is (proposal: Maildirs end in /)
# TODO: lock the spool (?) if we're reading from the spool
# TODO: figure out what folder to inc to
if mbox:
fp = file(boxpath, "r+")
box = mailbox.UnixMailbox(fp)
elif maildir:
box = mailbox.Maildir(boxpath)
while 1: # it irriates me that you can't do assignment
# in a while loop condition
m = box.next()
if m is None: break
else:
# TODO: write m into its new home
# TODO: print the scan line for m
# TODO: for Maildirs, delete m from the Maildir on successful write
# TODO: do something to truncate the spool if we're reading from it
_______________________________________________
nimh-dev mailing list
nimh-dev@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/nimh-dev