minor: folder: Base: improve style and comments

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht 2016-06-27 15:03:27 +02:00
parent 1f8163b194
commit 9a7603af37

View File

@ -35,13 +35,13 @@ class BaseFolder(object):
self.ui = getglobalui() self.ui = getglobalui()
self.messagelist = {} self.messagelist = {}
# Save original name for folderfilter operations # Save original name for folderfilter operations.
self.ffilter_name = name self.ffilter_name = name
# Top level dir name is always '' # Top level dir name is always ''.
self.root = None self.root = None
self.name = name if not name == self.getsep() else '' self.name = name if not name == self.getsep() else ''
self.newmail_hook = None self.newmail_hook = None
# Only set the newmail_hook if the IMAP folder is named 'INBOX' # Only set the newmail_hook if the IMAP folder is named 'INBOX'.
if self.name == 'INBOX': if self.name == 'INBOX':
self.newmail_hook = repository.newmail_hook self.newmail_hook = repository.newmail_hook
self.have_newmail = False self.have_newmail = False
@ -69,7 +69,7 @@ class BaseFolder(object):
self.repoconfname, "sync_deletes", True) self.repoconfname, "sync_deletes", True)
# Determine if we're running static or dynamic folder filtering # Determine if we're running static or dynamic folder filtering
# and check filtering status # and check filtering status.
self._dynamic_folderfilter = self.config.getdefaultboolean( self._dynamic_folderfilter = self.config.getdefaultboolean(
self.repoconfname, "dynamic_folderfilter", False) self.repoconfname, "dynamic_folderfilter", False)
self._sync_this = repository.should_sync_folder(self.ffilter_name) self._sync_this = repository.should_sync_folder(self.ffilter_name)
@ -80,7 +80,7 @@ class BaseFolder(object):
self.ui.debug('', "Filtering out '%s'[%s] due to folderfilter"% self.ui.debug('', "Filtering out '%s'[%s] due to folderfilter"%
(self.ffilter_name, repository)) (self.ffilter_name, repository))
# Passes for syncmessagesto # Passes for syncmessagesto.
self.syncmessagesto_passes = [ self.syncmessagesto_passes = [
('copying messages' , self.__syncmessagesto_copy), ('copying messages' , self.__syncmessagesto_copy),
('deleting messages' , self.__syncmessagesto_delete), ('deleting messages' , self.__syncmessagesto_delete),
@ -188,7 +188,7 @@ class BaseFolder(object):
basename = self.name.replace('/', '.') basename = self.name.replace('/', '.')
# Replace with literal 'dot' if final path name is '.' as '.' is # Replace with literal 'dot' if final path name is '.' as '.' is
# an invalid file name. # an invalid file name.
basename = re.sub('(^|\/)\.$','\\1dot', basename) basename = re.sub('(^|\/)\.$', '\\1dot', basename)
return basename return basename
def check_uidvalidity(self): def check_uidvalidity(self):
@ -239,8 +239,8 @@ class BaseFolder(object):
newval = self.get_uidvalidity() newval = self.get_uidvalidity()
uidfilename = self._getuidfilename() uidfilename = self._getuidfilename()
with open(uidfilename + ".tmp", "wt") as file: with open(uidfilename + ".tmp", "wt") as uidfile:
file.write("%d\n"% newval) uidfile.write("%d\n"% newval)
os.rename(uidfilename + ".tmp", uidfilename) os.rename(uidfilename + ".tmp", uidfilename)
self._base_saved_uidvalidity = newval self._base_saved_uidvalidity = newval
@ -248,14 +248,16 @@ class BaseFolder(object):
"""Retrieve the current connections UIDVALIDITY value """Retrieve the current connections UIDVALIDITY value
This function needs to be implemented by each Backend This function needs to be implemented by each Backend
:returns: UIDVALIDITY as a (long) number""" :returns: UIDVALIDITY as a (long) number."""
raise NotImplementedError raise NotImplementedError
def cachemessagelist(self): def cachemessagelist(self):
"""Reads the message list from disk or network and stores it in """Cache the list of messages.
memory for later use. This list will not be re-read from disk or
memory unless this function is called again.""" Reads the message list from disk or network and stores it in memory for
later use. This list will not be re-read from disk or memory unless
this function is called again."""
raise NotImplementedError raise NotImplementedError
@ -288,7 +290,7 @@ class BaseFolder(object):
raise NotImplementedError raise NotImplementedError
def uidexists(self, uid): def uidexists(self, uid):
"""Returns True if uid exists""" """Returns True if uid exists."""
return uid in self.getmessagelist() return uid in self.getmessagelist()
@ -310,14 +312,16 @@ class BaseFolder(object):
raise NotImplementedError raise NotImplementedError
def getmaxage(self): def getmaxage(self):
""" maxage is allowed to be either an integer or a date of the """Return maxage.
form YYYY-mm-dd. This returns a time_struct. """
maxage is allowed to be either an integer or a date of the form
YYYY-mm-dd. This returns a time_struct."""
maxagestr = self.config.getdefault("Account %s"% maxagestr = self.config.getdefault("Account %s"%
self.accountname, "maxage", None) self.accountname, "maxage", None)
if maxagestr == None: if maxagestr is None:
return None return None
# is it a number? # Is it a number?
try: try:
maxage = int(maxagestr) maxage = int(maxagestr)
if maxage < 1: if maxage < 1:
@ -325,8 +329,8 @@ class BaseFolder(object):
OfflineImapError.ERROR.MESSAGE) OfflineImapError.ERROR.MESSAGE)
return time.gmtime(time.time() - 60*60*24*maxage) return time.gmtime(time.time() - 60*60*24*maxage)
except ValueError: except ValueError:
pass # maybe it was a date pass # Maybe it was a date.
# is it a date string? # Is it a date string?
try: try:
date = time.strptime(maxagestr, "%Y-%m-%d") date = time.strptime(maxagestr, "%Y-%m-%d")
if date[0] < 1900: if date[0] < 1900:
@ -441,8 +445,9 @@ class BaseFolder(object):
raise NotImplementedError raise NotImplementedError
def addmessageflags(self, uid, flags): def addmessageflags(self, uid, flags):
"""Adds the specified flags to the message's flag set. If a given """Adds the specified flags to the message's flag set.
flag is already present, it will not be duplicated.
If a given flag is already present, it will not be duplicated.
Note that this function does not check against dryrun settings, Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a so you need to ensure that it is never called in a
@ -463,12 +468,14 @@ class BaseFolder(object):
self.addmessageflags(uid, flags) self.addmessageflags(uid, flags)
def deletemessageflags(self, uid, flags): def deletemessageflags(self, uid, flags):
"""Removes each flag given from the message's flag set. If a given """Removes each flag given from the message's flag set.
flag is already removed, no action will be taken for that flag.
Note that this function does not check against dryrun settings, Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a so you need to ensure that it is never called in a
dryrun mode.""" dryrun mode.
If a given flag is already removed, no action will be taken for that
flag."""
newflags = self.getmessageflags(uid) - flags newflags = self.getmessageflags(uid) - flags
self.savemessageflags(uid, newflags) self.savemessageflags(uid, newflags)
@ -518,8 +525,10 @@ class BaseFolder(object):
self.addmessagelabels(uid, labels) self.addmessagelabels(uid, labels)
def deletemessagelabels(self, uid, labels): def deletemessagelabels(self, uid, labels):
"""Removes each label given from the message's label set. If a given """Removes each label given from the message's label set.
label is already removed, no action will be taken for that label.
If a given label is already removed, no action will be taken for that
label.
Note that this function does not check against dryrun settings, Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a so you need to ensure that it is never called in a
@ -604,22 +613,26 @@ class BaseFolder(object):
if insertionpoint == -1: if insertionpoint == -1:
self.ui.debug('', 'addmessageheader: headers were missing') self.ui.debug('', 'addmessageheader: headers were missing')
else: else:
self.ui.debug('', 'addmessageheader: headers end at position %d' % insertionpoint) self.ui.debug('',
'addmessageheader: headers end at position %d'% insertionpoint)
mark = '==>EOH<==' mark = '==>EOH<=='
contextstart = max(0, insertionpoint - 100) contextstart = max(0, insertionpoint - 100)
contextend = min(len(content), insertionpoint + 100) contextend = min(len(content), insertionpoint + 100)
self.ui.debug('', 'addmessageheader: header/body transition context (marked by %s): %s' % self.ui.debug('', 'addmessageheader: header/body transition "
(mark, repr(content[contextstart:insertionpoint]) + \ context (marked by %s): %s%s%s'% (
mark + repr(content[insertionpoint:contextend]))) mark, repr(content[contextstart:insertionpoint]),
mark, repr(content[insertionpoint:contextend])
)
)
# Hoping for case #4 # Hoping for case #4.
prefix = linebreak prefix = linebreak
suffix = '' suffix = ''
# Case #2 # Case #2.
if insertionpoint == 0: if insertionpoint == 0:
prefix = '' prefix = ''
suffix = '' suffix = ''
# Either case #1 or #3 # Either case #1 or #3.
elif insertionpoint == -1: elif insertionpoint == -1:
prefix = '' prefix = ''
suffix = linebreak suffix = linebreak
@ -632,16 +645,18 @@ class BaseFolder(object):
if content[0:len(linebreak)] != linebreak: if content[0:len(linebreak)] != linebreak:
suffix = suffix + linebreak suffix = suffix + linebreak
self.ui.debug('', 'addmessageheader: insertionpoint = %d'% insertionpoint) self.ui.debug('',
'addmessageheader: insertionpoint = %d'% insertionpoint)
headers = content[0:insertionpoint] headers = content[0:insertionpoint]
self.ui.debug('', 'addmessageheader: headers = %s'% repr(headers)) self.ui.debug('', 'addmessageheader: headers = %s'% repr(headers))
new_header = prefix + ("%s: %s" % (headername, headervalue)) + suffix new_header = prefix + ("%s: %s"% (headername, headervalue)) + suffix
self.ui.debug('', 'addmessageheader: new_header = ' + repr(new_header)) self.ui.debug('', 'addmessageheader: new_header = %s'% repr(new_header))
return headers + new_header + content[insertionpoint:] return headers + new_header + content[insertionpoint:]
def __find_eoh(self, content): def __find_eoh(self, content):
""" Searches for the point where mail headers end. """Searches for the point where mail headers end.
Either double '\n', or end of string. Either double '\n', or end of string.
Arguments: Arguments:
@ -657,14 +672,15 @@ class BaseFolder(object):
def getmessageheader(self, content, name): def getmessageheader(self, content, name):
"""Searches for the first occurence of the given header and returns """Return the value of the first occurence of the given header.
its value. Header name is case-insensitive.
Header name is case-insensitive.
Arguments: Arguments:
- contents: message itself - contents: message itself
- name: name of the header to be searched - name: name of the header to be searched
Returns: header value or None if no such header was found Returns: header value or None if no such header was found.
""" """
self.ui.debug('', 'getmessageheader: called to get %s'% name) self.ui.debug('', 'getmessageheader: called to get %s'% name)
@ -673,7 +689,8 @@ class BaseFolder(object):
headers = content[0:eoh] headers = content[0:eoh]
self.ui.debug('', 'getmessageheader: headers = %s'% repr(headers)) self.ui.debug('', 'getmessageheader: headers = %s'% repr(headers))
m = re.search('^%s:(.*)$' % name, headers, flags = re.MULTILINE | re.IGNORECASE) m = re.search('^%s:(.*)$'% name, headers,
flags= re.MULTILINE | re.IGNORECASE)
if m: if m:
return m.group(1).strip() return m.group(1).strip()
else: else:
@ -681,23 +698,23 @@ class BaseFolder(object):
def getmessageheaderlist(self, content, name): def getmessageheaderlist(self, content, name):
"""Searches for the given header and returns a list of values for """Return a list of values for the given header.
that header.
Arguments: Arguments:
- contents: message itself - contents: message itself
- name: name of the header to be searched - name: name of the header to be searched
Returns: list of header values or emptylist if no such header was found Returns: list of header values or empty list if no such header was found.
""" """
self.ui.debug('', 'getmessageheaderlist: called to get %s' % name) self.ui.debug('', 'getmessageheaderlist: called to get %s'% name)
eoh = self.__find_eoh(content) eoh = self.__find_eoh(content)
self.ui.debug('', 'getmessageheaderlist: eoh = %d' % eoh) self.ui.debug('', 'getmessageheaderlist: eoh = %d'% eoh)
headers = content[0:eoh] headers = content[0:eoh]
self.ui.debug('', 'getmessageheaderlist: headers = %s' % repr(headers)) self.ui.debug('', 'getmessageheaderlist: headers = %s'% repr(headers))
return re.findall('^%s:(.*)$' % name, headers, flags = re.MULTILINE | re.IGNORECASE) return re.findall('^%s:(.*)$'%
name, headers, flags= re.MULTILINE | re.IGNORECASE)
def deletemessageheaders(self, content, header_list): def deletemessageheaders(self, content, header_list):
@ -707,14 +724,14 @@ class BaseFolder(object):
- content: message itself - content: message itself
- header_list: list of headers to be deleted or just the header name - header_list: list of headers to be deleted or just the header name
We expect our message to have '\n' as line endings. We expect our message to have '\n' as line endings."""
"""
if type(header_list) != type([]): if type(header_list) != type([]):
header_list = [header_list] header_list = [header_list]
self.ui.debug('', 'deletemessageheaders: called to delete %s'% (header_list)) self.ui.debug('', 'deletemessageheaders: called to delete %s'% (header_list))
if not len(header_list): return content if not len(header_list):
return content
eoh = self.__find_eoh(content) eoh = self.__find_eoh(content)
self.ui.debug('', 'deletemessageheaders: end of headers = %d'% eoh) self.ui.debug('', 'deletemessageheaders: end of headers = %d'% eoh)
@ -728,15 +745,16 @@ class BaseFolder(object):
if len(h) > len(trim_h) and h[0:len(trim_h)+1] == (trim_h + ":"): if len(h) > len(trim_h) and h[0:len(trim_h)+1] == (trim_h + ":"):
keep_it = False keep_it = False
break break
if keep_it: new_headers.append(h) if keep_it:
new_headers.append(h)
return ('\n'.join(new_headers) + rest) return '\n'.join(new_headers) + rest
def change_message_uid(self, uid, new_uid): def change_message_uid(self, uid, new_uid):
"""Change the message from existing uid to new_uid """Change the message from existing uid to new_uid.
If the backend supports it (IMAP does not). If the backend supports it (IMAP does not).
@ -779,7 +797,7 @@ class BaseFolder(object):
# synced to the status cache. This is only a problem with # synced to the status cache. This is only a problem with
# self.getmessage(). So, don't call self.getmessage unless # self.getmessage(). So, don't call self.getmessage unless
# really needed. # really needed.
if register: # output that we start a new thread if register: # Output that we start a new thread.
self.ui.registerthread(self.repository.account) self.ui.registerthread(self.repository.account)
try: try:
@ -802,9 +820,9 @@ class BaseFolder(object):
self.change_message_uid(uid, new_uid) self.change_message_uid(uid, new_uid)
statusfolder.deletemessage(uid) statusfolder.deletemessage(uid)
# Got new UID, change the local uid. # Got new UID, change the local uid.
# Save uploaded status in the statusfolder # Save uploaded status in the statusfolder.
statusfolder.savemessage(new_uid, message, flags, rtime) statusfolder.savemessage(new_uid, message, flags, rtime)
# Check whether the mail has been seen # Check whether the mail has been seen.
if 'S' not in flags: if 'S' not in flags:
self.have_newmail = True self.have_newmail = True
elif new_uid == 0: elif new_uid == 0:
@ -819,11 +837,11 @@ class BaseFolder(object):
raise OfflineImapError("Trying to save msg (uid %d) on folder " raise OfflineImapError("Trying to save msg (uid %d) on folder "
"%s returned invalid uid %d"% (uid, dstfolder.getvisiblename(), "%s returned invalid uid %d"% (uid, dstfolder.getvisiblename(),
new_uid), OfflineImapError.ERROR.MESSAGE) new_uid), OfflineImapError.ERROR.MESSAGE)
except (KeyboardInterrupt): # bubble up CTRL-C except (KeyboardInterrupt): # Bubble up CTRL-C.
raise raise
except OfflineImapError as e: except OfflineImapError as e:
if e.severity > OfflineImapError.ERROR.MESSAGE: if e.severity > OfflineImapError.ERROR.MESSAGE:
raise # bubble severe errors up raise # Bubble severe errors up.
self.ui.error(e, exc_info()[2]) self.ui.error(e, exc_info()[2])
except Exception as e: except Exception as e:
self.ui.error(e, exc_info()[2], self.ui.error(e, exc_info()[2],
@ -843,7 +861,7 @@ class BaseFolder(object):
This function checks and protects us from action in dryrun mode.""" This function checks and protects us from action in dryrun mode."""
# We have no new mail yet # We have no new mail yet.
self.have_newmail = False self.have_newmail = False
threads = [] threads = []
@ -855,18 +873,17 @@ class BaseFolder(object):
num_to_copy, self, self.repository, dstfolder.repository)) num_to_copy, self, self.repository, dstfolder.repository))
return return
for num, uid in enumerate(copylist): for num, uid in enumerate(copylist):
# bail out on CTRL-C or SIGTERM # Bail out on CTRL-C or SIGTERM.
if offlineimap.accounts.Account.abort_NOW_signal.is_set(): if offlineimap.accounts.Account.abort_NOW_signal.is_set():
break break
if uid > 0 and dstfolder.uidexists(uid): if uid > 0 and dstfolder.uidexists(uid):
# dst has message with that UID already, only update status # dstfolder has message with that UID already, only update status.
flags = self.getmessageflags(uid) flags = self.getmessageflags(uid)
rtime = self.getmessagetime(uid) rtime = self.getmessagetime(uid)
statusfolder.savemessage(uid, None, flags, rtime) statusfolder.savemessage(uid, None, flags, rtime)
continue continue
self.ui.copyingmessage(uid, num+1, num_to_copy, self, dstfolder) self.ui.copyingmessage(uid, num+1, num_to_copy, self, dstfolder)
# Exceptions are caught in copymessageto(). # Exceptions are caught in copymessageto().
if self.suggeststhreads(): if self.suggeststhreads():
self.waitforthread() self.waitforthread()
@ -935,23 +952,20 @@ class BaseFolder(object):
return selfflags return selfflags
knownkeywords = set(keywordmap.keys()) knownkeywords = set(keywordmap.keys())
selfkeywords = self.getmessagekeywords(uid) selfkeywords = self.getmessagekeywords(uid)
if not knownkeywords >= selfkeywords: if not knownkeywords >= selfkeywords:
#some of the message's keywords are not in the mapping, so # Some of the message's keywords are not in the mapping, so
#skip them # skip them.
skipped_keywords = list(selfkeywords - knownkeywords) skipped_keywords = list(selfkeywords - knownkeywords)
selfkeywords &= knownkeywords selfkeywords &= knownkeywords
self.ui.warn("Unknown keywords skipped: %s\n" self.ui.warn("Unknown keywords skipped: %s\n"
"You may want to change your configuration to include " "You may want to change your configuration to include "
"those\n" % (skipped_keywords)) "those\n" % (skipped_keywords))
keywordletterset = set([keywordmap[keyw] for keyw in selfkeywords]) keywordletterset = set([keywordmap[keyw] for keyw in selfkeywords])
#add the mapped keywords to the list of message flags # Add the mapped keywords to the list of message flags.
selfflags |= keywordletterset selfflags |= keywordletterset
except NotImplementedError: except NotImplementedError:
pass pass
@ -1003,14 +1017,14 @@ class BaseFolder(object):
for flag, uids in addflaglist.items(): for flag, uids in addflaglist.items():
self.ui.addingflags(uids, flag, dstfolder) self.ui.addingflags(uids, flag, dstfolder)
if self.repository.account.dryrun: if self.repository.account.dryrun:
continue #don't actually add in a dryrun continue # Don't actually add in a dryrun.
dstfolder.addmessagesflags(uids, set(flag)) dstfolder.addmessagesflags(uids, set(flag))
statusfolder.addmessagesflags(uids, set(flag)) statusfolder.addmessagesflags(uids, set(flag))
for flag,uids in delflaglist.items(): for flag,uids in delflaglist.items():
self.ui.deletingflags(uids, flag, dstfolder) self.ui.deletingflags(uids, flag, dstfolder)
if self.repository.account.dryrun: if self.repository.account.dryrun:
continue #don't actually remove in a dryrun continue # Don't actually remove in a dryrun.
dstfolder.deletemessagesflags(uids, set(flag)) dstfolder.deletemessagesflags(uids, set(flag))
statusfolder.deletemessagesflags(uids, set(flag)) statusfolder.deletemessagesflags(uids, set(flag))
@ -1050,7 +1064,7 @@ class BaseFolder(object):
""" """
for (passdesc, action) in self.syncmessagesto_passes: for (passdesc, action) in self.syncmessagesto_passes:
# bail out on CTRL-C or SIGTERM # Bail out on CTRL-C or SIGTERM.
if offlineimap.accounts.Account.abort_NOW_signal.is_set(): if offlineimap.accounts.Account.abort_NOW_signal.is_set():
break break
try: try:
@ -1064,7 +1078,7 @@ class BaseFolder(object):
except Exception as e: except Exception as e:
self.ui.error(e, exc_info()[2], "Syncing folder %s [acc: %s]" %\ self.ui.error(e, exc_info()[2], "Syncing folder %s [acc: %s]" %\
(self, self.accountname)) (self, self.accountname))
raise # raise unknown Exceptions so we can fix them raise # Raise unknown Exceptions so we can fix them.
def __eq__(self, other): def __eq__(self, other):
"""Comparisons work either on string comparing folder names or """Comparisons work either on string comparing folder names or