mailbox module, Maildir, and flags

Discussion in 'Python' started by, Sep 11, 2007.

  1. Guest

    Dear all,

    I'm trying to figure out if mailbox will let me get the flags of a
    Maildir message without running get_message on it, and reading the
    whole message. It seems like it should work by doing this:

    >>> import mailbox
    >>> mybox = mailbox.Maildir('~/Maildir/', mailbox.MaildirMessage)
    >>> myfolder = mybox.get_folder('School')
    >>> len(myfolder)

    >>> msg =

    But then,when I try to get_flags on msg, I get nothing

    >>> msg.get_flags()


    Instead, I have to ener

    >>> msg=myfolder.get_message(key)

    which reads the whole message. Then the flags show up (RS or
    whatever). Given that there's 2102 messages in the folder, and some of
    them have attachments, I'd rather run get_message on all of them.

    To explain my motivation: I'm writing a curses email client as a
    hobby. I started out with IMAP, and implemented a simple caching
    behaviour, using a pickled cache. When it logs in to the IMAP server,
    it checks only the uids and flags on the server, compares those to the
    ones in the cache, and from that can figure out what has been added,
    deleted, or changed. From that, it can figure out what headers to
    download. Saves a *lot* of time at startup.

    I'd like to implement something like this for Maildir. Figure out
    which msgs it needs to get a new Message instance for, and only those.
    The rest are in the cache. So after the first time, starting up on a
    large maildir folder will be pretty quick. Following the same logic, I
    just need the unique names and the flags. Now, I know the flags are in
    the name, but I can't find anyway in the mailbox module to get them.

    So, should I just run some text parsing on the filenames, using
    os.listdir? Given:


    it's easy enough to apply string.split(":2,"). But then I have to deal
    with all the nastiness of the maildir standard. This is probably what
    I'll end up doing, but I was wondering if the mailbox module had a way
    to do it.

    One other question. Given a mailbox.Maildir folder like the one above,
    is there a way to run through it and just get the headers? Basically,
    the opposite of get_payload? I know I can scew around with msg.keys(),
    but is there something like

    for msg in folder:
    hdr = msg.get_header()


    Thanks for all your help. Sorry if this was unclear or long-winded.

    , Sep 11, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matej Cepl examples (Maildir)

    Matej Cepl, Aug 6, 2003, in forum: Python
    David M. Cooke
    Aug 6, 2003
  2. Replies:
  3. Replies:
  4. Replies:
  5. Replies:

Share This Page