From b0169e561aafa03db89c2131619e139df3a4da30 Mon Sep 17 00:00:00 2001 From: Plamen Dimitrov Date: Thu, 5 Jul 2012 17:37:06 +0200 Subject: [PATCH] Perl lists turned to python dictionaries for performance and code readability --- file_iterator.py | 30 +++++++++++------------------- mail_iterator.py | 32 ++++++++++++++++---------------- restore_mail_inject.py | 2 +- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/file_iterator.py b/file_iterator.py index 265caf9..3c3c975 100644 --- a/file_iterator.py +++ b/file_iterator.py @@ -42,38 +42,31 @@ class FileIterator: return message def load_mailbox_list(self, mboxlistfile = ""): - mboxdb = [] + mboxdb = {} if mboxlistfile != "": try: with open(mboxlistfile, 'r') as acl_file: for line in acl_file: - lineparts = [] - acls = [] - + acls = {} linedata = MBOXFILE_LINE.match(line).groups() - #print(linedata) - #check this condition + #!!! test this condition if len(linedata) == 0: print("Illegal line in mailbox list dump: %s" % line) sys.exit() - - lineparts.append(linedata[0]) + key = linedata[0] aclstr = linedata[1] + + # loop through acl rights string and build dictionary of users and rights while(aclstr != ""): - this_acl = [] acldata = ACL_STRING.match(aclstr).groups() if len(acldata) == 0: print("Illegal line in mailbox list dump: %s" % line) sys.exit() aclstr = acldata[2] - this_acl.append(acldata[0]) - this_acl.append(acldata[1]) - acls.append(this_acl) - - lineparts.append(acls) - mboxdb.append(lineparts) - - #print(mboxdb) + acls[acldata[0]]=acldata[1] + + mboxdb[key] = acls + except IOError: print("Could not open mboxlist file %s." % mboxlistfile) return mboxdb @@ -108,7 +101,7 @@ class FileIterator: # suggest file modification date for internaldate yield (message, mailpath, os.path.getmtime(new_filepath)) except: - print("Could not retrieve mail from file: %s", new_filepath) + print("Could not retrieve mail from file: %s" % new_filepath) else: if os.path.isdir(new_filepath): # cyrus ^ vs . storage replacement @@ -122,7 +115,6 @@ class FileIterator: yield rcr #print("Done with directory %s and mailbox %s." % (new_filepath, new_mailpath)) # mark mailboxes that need acl update - print("acl %s" % mailpath) self.acl_mailboxes.append(mailpath) return # diff --git a/mail_iterator.py b/mail_iterator.py index 0465f11..3bf06b1 100644 --- a/mail_iterator.py +++ b/mail_iterator.py @@ -75,22 +75,22 @@ class MailIterator: mailbox = mailbox.replace("INBOX/", "user/" + original_user + "/") mailbox = mailbox.replace(".", "^") mailbox = mailbox.replace("/", ".") - - lookfor = mailbox - for current_folder in mailbox_list: - # find folder to set all acls - if current_folder[0] == lookfor: - acl_listref = current_folder[1] - for aclref in acl_listref: - print(aclref[0], target_user, original_user) - if aclref[0] != target_user and aclref[0] != original_user: - try: - #self.mail_con.setacl(mailbox, thisaclref[0], thisaclref[1]) - print("Set acls %s for user %s on mailbox %s." % (aclref[1], aclref[0], mailbox)) - except: - print("Could not set acls %s for user %s on mailbox %s." % (aclref[1], aclref[0], mailbox)) - break - + + # find folder to set all acls + try: + mbox_acls = mailbox_list[mailbox] + except KeyError: + # no rights for the mailbox were found + return + for acl_user in mbox_acls: + print(acl_user, target_user, original_user) + if acl_user != target_user and acl_user != original_user: + try: + #self.mail_con.setacl(mailbox, thisaclref[0], thisaclref[1]) + print("Set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox)) + except: + print("Could not set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox)) + return def delete_mailboxes(self, deleted_mailbox): diff --git a/restore_mail_inject.py b/restore_mail_inject.py index de15a1b..1cca29a 100644 --- a/restore_mail_inject.py +++ b/restore_mail_inject.py @@ -45,7 +45,7 @@ def main(): # mailboxes marked for creating and acl update # add acls after all subfolders or their acls will be derived from parent folder - print(storage.created_mailboxes, storage.acl_mailboxes) + #print(storage.created_mailboxes, storage.acl_mailboxes) for new_mailbox in storage.created_mailboxes: session.create_mailbox(new_mailbox) for acl_mailbox in storage.acl_mailboxes: -- 1.7.1