From 38f15e573035038a9e1c838811522c99e0d0a7f7 Mon Sep 17 00:00:00 2001 From: Plamen Dimitrov Date: Mon, 9 Jul 2012 13:21:28 +0200 Subject: [PATCH] First pylint validation --- file_iterator.py | 30 ++++++++++++++++-------------- mail_iterator.py | 40 ++++++++++++++++++++-------------------- restore_mail_inject.py | 15 ++++++++++----- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/file_iterator.py b/file_iterator.py index f15888c..b8f60ef 100644 --- a/file_iterator.py +++ b/file_iterator.py @@ -31,7 +31,8 @@ class FileIterator: """Closes the connection and the user session.""" return - def _message_read(self, filename): + @classmethod + def _message_read(cls, filename): """Retrieves a message from the message file.""" try: with open(filename, "r") as msgfile: @@ -41,7 +42,9 @@ class FileIterator: raise return message - def load_mailbox_list(self, mboxlistfile = ""): + @classmethod + def load_mailbox_list(cls, mboxlistfile = ""): + """Load the list of mailboxes and acl rights for each from file.""" mboxdb = {} if mboxlistfile != "": try: @@ -49,9 +52,8 @@ class FileIterator: for line in acl_file: acls = {} linedata = MBOXFILE_LINE.match(line).groups() - #!!! test this condition if len(linedata) == 0: - logging.warning("Illegal line in mailbox list dump: %s" % line) + logging.warning("Illegal line in mailbox list dump: %s", line) continue key = linedata[0] aclstr = linedata[1] @@ -60,26 +62,26 @@ class FileIterator: while(aclstr != ""): acldata = ACL_STRING.match(aclstr).groups() if len(acldata) == 0: - logging.error("Illegal acl string in mailbox list dump: %s" % line) + logging.error("Illegal acl string in mailbox list dump: %s", line) continue aclstr = acldata[2] - acls[acldata[0]]=acldata[1] + acls[acldata[0]] = acldata[1] mboxdb[key] = acls except IOError: - logging.warning("Could not open mboxlist file %s" % mboxlistfile) + logging.warning("Could not open mboxlist file %s", mboxlistfile) return mboxdb def load_mails(self, filepath, mailpath): """Loads all e-mails from file hierarchy. This recursive generator always returns a tuple of the next found (e-mail, mailbox to store, internaldate).""" - logging.debug("Entered directory %s -> %s" % (filepath, mailpath)) + logging.debug("Entered directory %s -> %s", filepath, mailpath) try: filepath = os.path.abspath(filepath) os.chdir(filepath) except OSError: - logging.warning("Can't open the directory %s" % filepath) + logging.warning("Can't open the directory %s", filepath) return # mark mailboxes that should be created self.created_mailboxes.append(mailpath) @@ -90,28 +92,28 @@ class FileIterator: new_filepath = filepath + "/" + subpath if (os.path.isfile(new_filepath)): if os.path.getsize(new_filepath) == 0: - logging.info("Skipping empty file %s" % subpath) + logging.info("Skipping empty file %s", subpath) else: if MAIL_FILENAME.match(subpath): - logging.info("Injecting file %s" % subpath) + logging.info("Injecting file %s", subpath) try: message = self._message_read(new_filepath) # suggest file modification date for internaldate yield (message, mailpath, os.path.getmtime(new_filepath)) except IOError: - logging.warning("Could not retrieve mail from the file %s" % new_filepath) + logging.warning("Could not retrieve mail from the file %s", new_filepath) else: if os.path.isdir(new_filepath): # cyrus ^ vs . storage replacement subpath = subpath.replace("^", ".") new_mailpath = mailpath + "/" + subpath - logging.debug("Inserting mails from directory %s into mailbox %s" % (new_filepath, new_mailpath)) + logging.debug("Inserting mails from directory %s into mailbox %s", new_filepath, new_mailpath) # load_mails($mboxdbref, $origuser, $targetuser) rcrs_generator = self.load_mails(new_filepath, new_mailpath) # you enter the generator in the for loop for rcr in rcrs_generator: yield rcr - logging.debug("Done with directory %s and mailbox %s" % (new_filepath, new_mailpath)) + logging.debug("Done with directory %s and mailbox %s", new_filepath, new_mailpath) # mark mailboxes that need acl update self.acl_mailboxes.append(mailpath) return \ No newline at end of file diff --git a/mail_iterator.py b/mail_iterator.py index 518a8d1..9aebca2 100644 --- a/mail_iterator.py +++ b/mail_iterator.py @@ -24,7 +24,7 @@ class MailIterator: # logged in status logged_in = None - def __init__(self, username): + def __init__(self, username, server = "/var/imap/socket/imap"): """Creates a connection and a user session.""" # connect to server @@ -32,12 +32,12 @@ class MailIterator: self.mail_con = imaplib.IMAP4("intranator.m.i2n") # MODIFIED imap_socket = socket.socket(socket.AF_UNIX) - imap_socket.connect("/var/imap/socket/imap") + imap_socket.connect(server) self.mail_con.sock = imap_socket self.mail_con.file = self.mail_con.sock.makefile('rb') - logging.info("Connected to mail server.") - except (self.mail_con.error, socket.error) as ex: - logging.error("Could not connect to host: %s" % (ex)) + logging.info("Connected to mail server %s", server) + except (socket.error, self.mail_con.error) as ex: + logging.error("Could not connect to host: %s", ex) sys.exit() # log in @@ -45,17 +45,17 @@ class MailIterator: self.mail_con.login("cyrus", "geheim") self.logged_in = True #self.mail_con.proxyauth(username) - logging.info("Logged in as %s." % username) + logging.info("Logged in as %s.", username) except self.mail_con.error as ex: self.logged_in = False - logging.error("Could not log in as user %s: %s" % (username, ex)) + logging.error("Could not log in as user %s: %s", username, ex) sys.exit() # list mailboxes try: _result, mailboxes = self.mail_con.list() except self.mail_con.error as ex: - logging.warning("Could not retrieve mailboxes for user %s: %s" % (username, ex)) + logging.warning("Could not retrieve mailboxes for user %s: %s", username, ex) self.mailboxes = [] for mailbox in mailboxes: mailbox = MAILBOX_RESP.match(mailbox.decode('iso-8859-1')).groups() @@ -75,17 +75,17 @@ class MailIterator: try: _result, inbox_acls = self.mail_con.getacl("INBOX") except self.mail_con.error as ex: - logging.warning("Could not get the acls of INBOX: %s" % ex) + logging.warning("Could not get the acls of INBOX: %s", ex) return inbox_acls = ACLS_RESP.findall(inbox_acls[0][6:]) - logging.debug("Retrieved acls from INBOX are %s" % inbox_acls) + logging.debug("Retrieved acls from INBOX are %s", inbox_acls) for acl_ref in inbox_acls: if acl_ref[0] != user: try: self.mail_con.deleteacl("INBOX", acl_ref[0]) - logging.debug("Reset acls on INBOX for user %s" % acl_ref[0].decode('iso-8859-1')) + logging.debug("Reset acls on INBOX for user %s", acl_ref[0].decode('iso-8859-1')) except self.mail_con.error as ex: - logging.warning("Could not reset acls on INBOX for user %s: %s" % (acl_ref[0], ex)) + logging.warning("Could not reset acls on INBOX for user %s: %s", acl_ref[0], ex) return def add_acls(self, mailbox, mailbox_list, original_user, target_user): @@ -106,9 +106,9 @@ class MailIterator: if acl_user != target_user and acl_user != original_user: try: self.mail_con.setacl(mailbox, acl_user, mbox_acls[acl_user]) - logging.debug("Set acls %s for user %s on mailbox %s" % (mbox_acls[acl_user], acl_user, mailbox)) + logging.debug("Set acls %s for user %s on mailbox %s", mbox_acls[acl_user], acl_user, mailbox) except self.mail_con.error as ex: - logging.warning("Could not set acls %s for user %s on mailbox %s: %s" % (mbox_acls[acl_user], acl_user, mailbox, ex)) + logging.warning("Could not set acls %s for user %s on mailbox %s: %s", mbox_acls[acl_user], acl_user, mailbox, ex) return @@ -122,9 +122,9 @@ class MailIterator: if re.compile(pattern).match(mailbox[2]): result, data = self.mail_con.delete(mailbox[2]) if result == "OK": - logging.debug("Deleted mailbox %s" % mailbox[2]) + logging.debug("Deleted mailbox %s", mailbox[2]) else: - logging.warning("Could not delete mailbox %s: %s" % (mailbox[2], data[0])) + logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0]) return def create_mailbox(self, mailbox): @@ -132,16 +132,16 @@ class MailIterator: if mailbox != "INBOX": result, data = self.mail_con.create(mailbox) if result == "OK": - logging.debug("Creating mailbox %s" % mailbox) + logging.debug("Creating mailbox %s", mailbox) else: - logging.warning("Could not create mailbox %s: %s" % (mailbox, data[0])) + logging.warning("Could not create mailbox %s: %s", mailbox, data[0]) return def inject_message(self, message, mailbox, internal_date): """Inject a message into a mailbox.""" result, data = self.mail_con.append(mailbox, "\\Seen", internal_date, message.encode()) if result == "OK": - logging.debug("Appending message to mailbox %s" % mailbox) + logging.debug("Appending message to mailbox %s", mailbox) else: - logging.warning("Could not append the e-mail %s: %s" % (message, data[0])) + logging.warning("Could not append the e-mail %s: %s", message, data[0]) return \ No newline at end of file diff --git a/restore_mail_inject.py b/restore_mail_inject.py index 11b9471..9623ead 100644 --- a/restore_mail_inject.py +++ b/restore_mail_inject.py @@ -25,11 +25,14 @@ def main(): # prepare configuration args = configure_args() warnings_handler = prepare_logger() - logging.info("The module restore_mail_inject.py started with user %s, folder %s and source %s." % - (args.user, args.folder, args.srcdir)) - - # connect - session = MailIterator(args.user) + logging.info("The module restore_mail_inject.py started with user %s, folder %s and source %s.", + args.user, args.folder, args.srcdir) + + # connect to unix socket or server + if(args.unix_socket_disabled): + session = MailIterator(args.user) + else: + session = MailIterator(args.user) #session = MailIterator("/var/imap/socket/imap", "cyrus", "geheim") storage = FileIterator() @@ -63,7 +66,7 @@ def main(): for acl_mailbox in storage.acl_mailboxes: session.add_acls(acl_mailbox, mailbox_list, args.ouser, args.user) - logging.info("Finished injecting mails. Exiting with code %s." % warnings_handler.detected_problems) + logging.info("Finished injecting mails. Exiting with code %s.", warnings_handler.detected_problems) return warnings_handler.detected_problems def configure_args(): @@ -82,6 +85,8 @@ def configure_args(): default="", help='name of the original user (=username if not specified)') parser.add_argument('-a', '--append', dest='append', action='store_true', default=False, help="append mails, don't delete anything") + parser.add_argument('-n', '--normal', dest='unix_socket_disabled', action='store_true', + default=False, help='disable unix socket usage for the IMAP connection') args = parser.parse_args() if (args.folder != "INBOX"): -- 1.7.1