Building a Tree: IMAP Mailbox list

B

Brian Jones

Hello, I am trying to build a dictionary tree from a mailbox list I have
returned using the imaplib module. This just boils down to general
inexperience, but any help would be appreciated.

I am attempting to take the following list:
boxlist = ['INBOX.save.subsave', 'INBOX.Drafts', 'INBOX.Sent', 'INBOX.save',
'INBOX.web','INBOX.Trash', 'INBOX']

And turn it into something like:

newlist = {
'INBOX': {
'INBOX.Drafts': None,
'INBOX.Sent': None,
'INBOX.save': {
'INBOX.save.subsave': None
},
'INBOX.web': None,
'INBOX.Trash': None,
}
}

I am hoping this is just trivial, and I am on the right track.

The following is the code I have written. It doesn't actually build the
tree however, because that is the part I am uncertain about:

################
# Code
################

import re
import string

class mailboxsort:

nodetree = {}

def __init__(self, list):
self.masterlist = list

def buildTree(self, node):
children = self.getChildren(node)
if len(children) > 0:
self.lastnode = node
for num in range(len(children)):
child = children[num]
self.buildTree(child)

def getChildren(self, node):
children = []
for mailbox in self.masterlist:
renode = node + '.'
if re.compile(renode).match(mailbox):
nodelen = len(node.split('.'))
childlen = len(mailbox.split('.'))
if childlen == (nodelen + 1):
children.append(mailbox)
return children

if __name__ == '__main__':
boxlist = ['INBOX.save.subsave', 'INBOX.Drafts', 'INBOX.Sent',
'INBOX.save', 'INBOX.web','INBOX.Trash', 'INBOX']
boxsort = mailboxsort(boxlist)
boxsort.buildTree('INBOX')
print boxsort.nodetree



Brian "bojo" Jones
bojo at gvea dot com
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top