From de618d84f3cbced9c6281330b65ace02ac30d986 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Wed, 5 Jan 2011 17:00:57 +0100 Subject: [PATCH] Save ui as self.ui in folder/Base class Rather than regetting the ui with UIBase.getglobalui() all the time, we get it once in the base class and let all derivative classes just make use of self.ui rather than refetching the ui all the time, this makes for a bit less code and shorter lines. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/folder/Base.py | 28 ++++++++++-------- offlineimap/folder/IMAP.py | 56 ++++++++++++++++------------------- offlineimap/folder/Maildir.py | 11 ++++--- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py index 0f38bc3..a79dcf8 100644 --- a/offlineimap/folder/Base.py +++ b/offlineimap/folder/Base.py @@ -19,13 +19,15 @@ from threading import * from offlineimap import threadutil from offlineimap.threadutil import InstanceLimitedThread -from offlineimap.ui import UIBase -import os.path, re +from offlineimap.ui import getglobalui +import os.path +import re import sys class BaseFolder: def __init__(self): self.uidlock = Lock() + self.ui = getglobalui() def getname(self): """Returns name""" @@ -202,8 +204,8 @@ class BaseFolder: def syncmessagesto_neguid_msg(self, uid, dest, applyto, register = 1): if register: - UIBase.getglobalui().registerthread(self.getaccountname()) - UIBase.getglobalui().copyingmessage(uid, self, applyto) + self.ui.registerthread(self.getaccountname()) + self.ui.copyingmessage(uid, self, applyto) successobject = None successuid = None message = self.getmessage(uid) @@ -269,8 +271,8 @@ class BaseFolder: # really needed. try: if register: - UIBase.getglobalui().registerthread(self.getaccountname()) - UIBase.getglobalui().copyingmessage(uid, self, applyto) + self.ui.registerthread(self.getaccountname()) + self.ui.copyingmessage(uid, self, applyto) message = '' # If any of the destinations actually stores the message body, # load it up. @@ -289,7 +291,7 @@ class BaseFolder: self.deletemessage(uid) uid = newuid except: - UIBase.getglobalui().warn("ERROR attempting to copy message " + str(uid) \ + self.ui.warn("ERROR attempting to copy message " + str(uid) \ + " for account " + self.getaccountname() + ":" + str(sys.exc_info()[1])) @@ -334,7 +336,7 @@ class BaseFolder: if not uid in self_messagelist: deletelist.append(uid) if len(deletelist): - UIBase.getglobalui().deletingmessages(deletelist, applyto) + self.ui.deletingmessages(deletelist, applyto) for object in applyto: object.deletemessages(deletelist) @@ -375,10 +377,10 @@ class BaseFolder: for object in applyto: for flag in addflaglist.keys(): - UIBase.getglobalui().addingflags(addflaglist[flag], flag, [object]) + self.ui.addingflags(addflaglist[flag], flag, [object]) object.addmessagesflags(addflaglist[flag], [flag]) for flag in delflaglist.keys(): - UIBase.getglobalui().deletingflags(delflaglist[flag], flag, [object]) + self.ui.deletingflags(delflaglist[flag], flag, [object]) object.deletemessagesflags(delflaglist[flag], [flag]) def syncmessagesto(self, dest, applyto = None): @@ -394,7 +396,7 @@ class BaseFolder: try: self.syncmessagesto_neguid(dest, applyto) except: - UIBase.getglobalui().warn("ERROR attempting to handle negative uids " \ + self.ui.warn("ERROR attempting to handle negative uids " \ + "for account " + self.getaccountname() + ":" + str(sys.exc_info()[1])) #all threads launched here are in try / except clauses when they copy anyway... @@ -403,7 +405,7 @@ class BaseFolder: try: self.syncmessagesto_delete(dest, applyto) except: - UIBase.getglobalui().warn("ERROR attempting to delete messages " \ + self.ui.warn("ERROR attempting to delete messages " \ + "for account " + self.getaccountname() + ":" + str(sys.exc_info()[1])) # Now, the message lists should be identical wrt the uids present. @@ -413,7 +415,7 @@ class BaseFolder: try: self.syncmessagesto_flags(dest, applyto) except: - UIBase.getglobalui().warn("ERROR attempting to sync flags " \ + self.ui.warn("ERROR attempting to sync flags " \ + "for account " + self.getaccountname() + ":" + str(sys.exc_info()[1])) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 927c5f4..2836bd3 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -19,7 +19,6 @@ from Base import BaseFolder import imaplib from offlineimap import imaputil, imaplibutil -from offlineimap.ui import UIBase from offlineimap.version import versionstr import rfc822, time, string, random, binascii, re from StringIO import StringIO @@ -41,6 +40,7 @@ class IMAPFolder(BaseFolder): self.repository = repository self.randomgenerator = random.Random() BaseFolder.__init__(self) + #self.ui is set in BaseFolder def selectro(self, imapobj): """Select this folder when we do not need write access. @@ -188,7 +188,7 @@ class IMAPFolder(BaseFolder): messagestr = string.split(messagestr, maxsplit = 1)[1] options = imaputil.flags2hash(messagestr) if not options.has_key('UID'): - UIBase.getglobalui().warn('No UID in message with options %s' %\ + self.ui.warn('No UID in message with options %s' %\ str(options), minor = 1) else: @@ -201,12 +201,11 @@ class IMAPFolder(BaseFolder): return self.messagelist def getmessage(self, uid): - ui = UIBase.getglobalui() imapobj = self.imapserver.acquireconnection() try: imapobj.select(self.getfullname(), readonly = 1) initialresult = imapobj.uid('fetch', '%d' % uid, '(BODY.PEEK[])') - ui.debug('imap', 'Returned object from fetching %d: %s' % \ + self.ui.debug('imap', 'Returned object from fetching %d: %s' % \ (uid, str(initialresult))) return initialresult[1][0][1].replace("\r\n", "\n") @@ -230,31 +229,29 @@ class IMAPFolder(BaseFolder): return (headername, headervalue) def savemessage_addheader(self, content, headername, headervalue): - ui = UIBase.getglobalui() - ui.debug('imap', + self.ui.debug('imap', 'savemessage_addheader: called to add %s: %s' % (headername, headervalue)) insertionpoint = content.find("\r\n") - ui.debug('imap', 'savemessage_addheader: insertionpoint = %d' % insertionpoint) + self.ui.debug('imap', 'savemessage_addheader: insertionpoint = %d' % insertionpoint) leader = content[0:insertionpoint] - ui.debug('imap', 'savemessage_addheader: leader = %s' % repr(leader)) + self.ui.debug('imap', 'savemessage_addheader: leader = %s' % repr(leader)) if insertionpoint == 0 or insertionpoint == -1: newline = '' insertionpoint = 0 else: newline = "\r\n" newline += "%s: %s" % (headername, headervalue) - ui.debug('imap', 'savemessage_addheader: newline = ' + repr(newline)) + self.ui.debug('imap', 'savemessage_addheader: newline = ' + repr(newline)) trailer = content[insertionpoint:] - ui.debug('imap', 'savemessage_addheader: trailer = ' + repr(trailer)) + self.ui.debug('imap', 'savemessage_addheader: trailer = ' + repr(trailer)) return leader + newline + trailer def savemessage_searchforheader(self, imapobj, headername, headervalue): if imapobj.untagged_responses.has_key('APPENDUID'): return long(imapobj.untagged_responses['APPENDUID'][-1].split(' ')[1]) - ui = UIBase.getglobalui() - ui.debug('imap', 'savemessage_searchforheader called for %s: %s' % \ + self.ui.debug('imap', 'savemessage_searchforheader called for %s: %s' % \ (headername, headervalue)) # Now find the UID it got. headervalue = imapobj._quote(headervalue) @@ -262,16 +259,16 @@ class IMAPFolder(BaseFolder): matchinguids = imapobj.uid('search', 'HEADER', headername, headervalue)[1][0] except imapobj.error, err: # IMAP server doesn't implement search or had a problem. - ui.debug('imap', "savemessage_searchforheader: got IMAP error '%s' while attempting to UID SEARCH for message with header %s" % (err, headername)) + self.ui.debug('imap', "savemessage_searchforheader: got IMAP error '%s' while attempting to UID SEARCH for message with header %s" % (err, headername)) return 0 - ui.debug('imap', 'savemessage_searchforheader got initial matchinguids: ' + repr(matchinguids)) + self.ui.debug('imap', 'savemessage_searchforheader got initial matchinguids: ' + repr(matchinguids)) if matchinguids == '': - ui.debug('imap', "savemessage_searchforheader: UID SEARCH for message with header %s yielded no results" % headername) + self.ui.debug('imap', "savemessage_searchforheader: UID SEARCH for message with header %s yielded no results" % headername) return 0 matchinguids = matchinguids.split(' ') - ui.debug('imap', 'savemessage_searchforheader: matchinguids now ' + \ + self.ui.debug('imap', 'savemessage_searchforheader: matchinguids now ' + \ repr(matchinguids)) if len(matchinguids) != 1 or matchinguids[0] == None: raise ValueError, "While attempting to find UID for message with header %s, got wrong-sized matchinguids of %s" % (headername, str(matchinguids)) @@ -280,13 +277,12 @@ class IMAPFolder(BaseFolder): def savemessage(self, uid, content, flags, rtime): imapobj = self.imapserver.acquireconnection() - ui = UIBase.getglobalui() - ui.debug('imap', 'savemessage: called') + self.ui.debug('imap', 'savemessage: called') try: try: imapobj.select(self.getfullname()) # Needed for search except imapobj.readonly: - ui.msgtoreadonly(self, uid, content, flags) + self.ui.msgtoreadonly(self, uid, content, flags) # Return indicating message taken, but no UID assigned. # Fudge it. return 0 @@ -324,17 +320,17 @@ class IMAPFolder(BaseFolder): # but some IMAP servers nonetheless choke on 1902. date = imaplib.Time2Internaldate(time.localtime()) - ui.debug('imap', 'savemessage: using date ' + str(date)) + self.ui.debug('imap', 'savemessage: using date ' + str(date)) content = re.sub("(?