Perl lists turned to python dictionaries for performance and code readability
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Thu, 5 Jul 2012 15:37:06 +0000 (17:37 +0200)
committerPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Thu, 5 Jul 2012 15:37:06 +0000 (17:37 +0200)
file_iterator.py
mail_iterator.py
restore_mail_inject.py

index 265caf9..3c3c975 100644 (file)
@@ -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
     #
index 0465f11..3bf06b1 100644 (file)
@@ -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):
index de15a1b..1cca29a 100644 (file)
@@ -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: