minor code refactoring

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht 2016-11-07 23:03:40 +01:00
parent 9606cfcfc1
commit e452c344d9
5 changed files with 18 additions and 11 deletions

View File

@ -540,6 +540,7 @@ localfolders = ~/Test
# #
#filename_use_mail_timestamp = no #filename_use_mail_timestamp = no
# This option stands in the [Repository LocalExample] section. # This option stands in the [Repository LocalExample] section.
# #
# Map IMAP [user-defined] keywords to lowercase letters, similar to Dovecot's # Map IMAP [user-defined] keywords to lowercase letters, similar to Dovecot's

View File

@ -453,15 +453,10 @@ def syncfolder(account, remotefolder, quick):
localfolder.save_uidvalidity() localfolder.save_uidvalidity()
remotefolder.save_uidvalidity() remotefolder.save_uidvalidity()
def save_min_uid(folder, min_uid): def cachemessagelists_upto_date(date):
uidfile = folder.get_min_uid_file()
fd = open(uidfile, 'wt')
fd.write(str(min_uid) + "\n")
fd.close()
def cachemessagelists_upto_date(localfolder, remotefolder, date):
"""Returns messages with uid > min(uids of messages newer than date).""" """Returns messages with uid > min(uids of messages newer than date)."""
# Warning: this makes sense only if the cached list is empty.
localfolder.cachemessagelist(min_date=date) localfolder.cachemessagelist(min_date=date)
check_uid_validity(localfolder, remotefolder, statusfolder) check_uid_validity(localfolder, remotefolder, statusfolder)
# Local messagelist had date restriction applied already. Restrict # Local messagelist had date restriction applied already. Restrict
@ -515,7 +510,7 @@ def syncfolder(account, remotefolder, quick):
min_uid = min(positive_uids) min_uid = min(positive_uids)
else: else:
min_uid = 1 min_uid = 1
save_min_uid(partial, min_uid) partial.save_min_uid(min_uid)
else: else:
partial.cachemessagelist(min_uid=min_uid) partial.cachemessagelist(min_uid=min_uid)
@ -562,7 +557,7 @@ def syncfolder(account, remotefolder, quick):
ui.warn("Quick syncs (-q) not supported in conjunction " ui.warn("Quick syncs (-q) not supported in conjunction "
"with maxage or startdate; ignoring -q.") "with maxage or startdate; ignoring -q.")
if maxage != None: if maxage != None:
cachemessagelists_upto_date(localfolder, remotefolder, maxage) cachemessagelists_upto_date(maxage)
elif localstart != None: elif localstart != None:
cachemessagelists_startdate(remotefolder, localfolder, cachemessagelists_startdate(remotefolder, localfolder,
localstart) localstart)

View File

@ -400,6 +400,12 @@ class BaseFolder(object):
os.mkdir(startuiddir, 0o700) os.mkdir(startuiddir, 0o700)
return os.path.join(startuiddir, self.getfolderbasename()) return os.path.join(startuiddir, self.getfolderbasename())
def save_min_uid(self, min_uid):
uidfile = self.get_min_uid_file()
fd = open(uidfile, 'wt')
fd.write(str(min_uid) + "\n")
fd.close()
def retrieve_min_uid(self): def retrieve_min_uid(self):
uidfile = self.get_min_uid_file() uidfile = self.get_min_uid_file()
if not os.path.exists(uidfile): if not os.path.exists(uidfile):

View File

@ -91,7 +91,6 @@ class LocalStatusSQLiteFolder(BaseFolder):
if self.filename not in LocalStatusSQLiteFolder.locks: if self.filename not in LocalStatusSQLiteFolder.locks:
LocalStatusSQLiteFolder.locks[self.filename] = DatabaseFileLock() LocalStatusSQLiteFolder.locks[self.filename] = DatabaseFileLock()
self._databaseFileLock = LocalStatusSQLiteFolder.locks[self.filename] self._databaseFileLock = LocalStatusSQLiteFolder.locks[self.filename]
self._in_transactions = 0 self._in_transactions = 0
def __enter__(self): def __enter__(self):

View File

@ -34,14 +34,16 @@ except NameError:
from offlineimap import OfflineImapError, emailutil from offlineimap import OfflineImapError, emailutil
from .Base import BaseFolder from .Base import BaseFolder
# Find the UID in a message filename # Find the UID in a message filename
re_uidmatch = re.compile(',U=(\d+)') re_uidmatch = re.compile(',U=(\d+)')
# Find a numeric timestamp in a string (filename prefix) # Find a numeric timestamp in a string (filename prefix)
re_timestampmatch = re.compile('(\d+)'); re_timestampmatch = re.compile('(\d+)')
timehash = {} timehash = {}
timelock = Lock() timelock = Lock()
def _gettimeseq(date=None): def _gettimeseq(date=None):
global timehash, timelock global timehash, timelock
timelock.acquire() timelock.acquire()
@ -56,6 +58,7 @@ def _gettimeseq(date=None):
finally: finally:
timelock.release() timelock.release()
class MaildirFolder(BaseFolder): class MaildirFolder(BaseFolder):
def __init__(self, root, name, sep, repository): def __init__(self, root, name, sep, repository):
self.sep = sep # needs to be set before super().__init__ self.sep = sep # needs to be set before super().__init__
@ -343,6 +346,7 @@ class MaildirFolder(BaseFolder):
See folder/Base for detail. Note that savemessage() does not See folder/Base for detail. Note that savemessage() does not
check against dryrun settings, so you need to ensure that check against dryrun settings, so you need to ensure that
savemessage is never called in a dryrun mode.""" savemessage is never called in a dryrun mode."""
# This function only ever saves to tmp/, # This function only ever saves to tmp/,
# but it calls savemessageflags() to actually save to cur/ or new/. # but it calls savemessageflags() to actually save to cur/ or new/.
self.ui.savemessage('maildir', uid, flags, self) self.ui.savemessage('maildir', uid, flags, self)
@ -471,6 +475,8 @@ class MaildirFolder(BaseFolder):
oldfilename = self.messagelist[uid]['filename'] oldfilename = self.messagelist[uid]['filename']
dir_prefix, filename = os.path.split(oldfilename) dir_prefix, filename = os.path.split(oldfilename)
flags = self.getmessageflags(uid) flags = self.getmessageflags(uid)
# TODO: we aren't keeping the prefix timestamp so we don't honor the
# filename_use_mail_timestamp configuration option.
newfilename = os.path.join(dir_prefix, newfilename = os.path.join(dir_prefix,
self.new_message_filename(new_uid, flags)) self.new_message_filename(new_uid, flags))
os.rename(os.path.join(self.getfullname(), oldfilename), os.rename(os.path.join(self.getfullname(), oldfilename),