Reformat offlineimap/folder/IMAP.py
Add some spaces, remove lines,... now format is better (lintian).
This commit is contained in:
parent
2f97dda6d9
commit
da1788db53
@ -27,7 +27,6 @@ from offlineimap import imaputil, imaplibutil, emailutil, OfflineImapError
|
||||
from offlineimap import globals
|
||||
from offlineimap.virtual_imaplib2 import MonthNames
|
||||
|
||||
|
||||
# Globals
|
||||
CRLF = '\r\n'
|
||||
MSGCOPY_NAMESPACE = 'MSGCOPY_'
|
||||
@ -103,7 +102,7 @@ class IMAPFolder(BaseFolder):
|
||||
singlethreadperfolder_default = True
|
||||
|
||||
onethread = self.config.getdefaultboolean(
|
||||
"Repository %s"% self.repository.getname(),
|
||||
"Repository %s" % self.repository.getname(),
|
||||
"singlethreadperfolder", singlethreadperfolder_default)
|
||||
if onethread is True:
|
||||
return False
|
||||
@ -114,7 +113,7 @@ class IMAPFolder(BaseFolder):
|
||||
self.imapserver.connectionwait()
|
||||
|
||||
def getmaxage(self):
|
||||
if self.config.getdefault("Account %s"%
|
||||
if self.config.getdefault("Account %s" %
|
||||
self.accountname, "maxage", None):
|
||||
six.reraise(OfflineImapError,
|
||||
OfflineImapError(
|
||||
@ -213,12 +212,12 @@ class IMAPFolder(BaseFolder):
|
||||
res_type, res_data = imapobj.search(None, search_conditions)
|
||||
if res_type != 'OK':
|
||||
raise OfflineImapError("SEARCH in folder [%s]%s failed. "
|
||||
"Search string was '%s'. Server responded '[%s] %s'"% (
|
||||
"Search string was '%s'. Server responded '[%s] %s'" % (
|
||||
self.getrepository(), self, search_cond, res_type, res_data),
|
||||
OfflineImapError.ERROR.FOLDER)
|
||||
except Exception as e:
|
||||
raise OfflineImapError("SEARCH in folder [%s]%s failed. "
|
||||
"Search string was '%s'. Error: %s"% (
|
||||
"Search string was '%s'. Error: %s" % (
|
||||
self.getrepository(), self, search_cond, str(e)),
|
||||
OfflineImapError.ERROR.FOLDER)
|
||||
# Davmail returns list instead of list of one element string.
|
||||
@ -239,20 +238,20 @@ class IMAPFolder(BaseFolder):
|
||||
conditions = []
|
||||
# 1. min_uid condition.
|
||||
if min_uid != None:
|
||||
conditions.append("UID %d:*"% min_uid)
|
||||
conditions.append("UID %d:*" % min_uid)
|
||||
# 2. date condition.
|
||||
elif min_date != None:
|
||||
# Find out what the oldest message is that we should look at.
|
||||
conditions.append("SINCE %02d-%s-%d"% (
|
||||
conditions.append("SINCE %02d-%s-%d" % (
|
||||
min_date[2], MonthNames[min_date[1]], min_date[0]))
|
||||
# 3. maxsize condition.
|
||||
maxsize = self.getmaxsize()
|
||||
if maxsize != None:
|
||||
conditions.append("SMALLER %d"% maxsize)
|
||||
conditions.append("SMALLER %d" % maxsize)
|
||||
|
||||
if len(conditions) >= 1:
|
||||
# Build SEARCH command.
|
||||
search_cond = "(%s)"% ' '.join(conditions)
|
||||
search_cond = "(%s)" % ' '.join(conditions)
|
||||
search_result = search(search_cond)
|
||||
return imaputil.uid_sequence(search_result)
|
||||
|
||||
@ -263,7 +262,6 @@ class IMAPFolder(BaseFolder):
|
||||
def msglist_item_initializer(self, uid):
|
||||
return {'uid': uid, 'flags': set(), 'time': 0}
|
||||
|
||||
|
||||
# Interface from BaseFolder
|
||||
def cachemessagelist(self, min_date=None, min_uid=None):
|
||||
self.ui.loadmessagelist(self.repository, self)
|
||||
@ -278,15 +276,16 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
# Get the flags and UIDs for these. single-quotes prevent
|
||||
# imaplib2 from quoting the sequence.
|
||||
fetch_msg = "%s"% msgsToFetch
|
||||
self.ui.debug('imap', "calling imaplib2 fetch command: %s %s"%
|
||||
fetch_msg = "%s" % msgsToFetch
|
||||
self.ui.debug('imap', "calling imaplib2 fetch command: %s %s" %
|
||||
(fetch_msg, '(FLAGS UID INTERNALDATE)'))
|
||||
res_type, response = imapobj.fetch(
|
||||
fetch_msg, '(FLAGS UID INTERNALDATE)')
|
||||
if res_type != 'OK':
|
||||
raise OfflineImapError("FETCHING UIDs in folder [%s]%s failed. "
|
||||
"Server responded '[%s] %s'"% (self.getrepository(), self,
|
||||
res_type, response), OfflineImapError.ERROR.FOLDER)
|
||||
"Server responded '[%s] %s'" % (self.getrepository(), self,
|
||||
res_type, response),
|
||||
OfflineImapError.ERROR.FOLDER)
|
||||
finally:
|
||||
self.imapserver.releaseconnection(imapobj)
|
||||
|
||||
@ -298,7 +297,7 @@ class IMAPFolder(BaseFolder):
|
||||
messagestr = messagestr.decode('utf-8').split(' ', 1)[1]
|
||||
options = imaputil.flags2hash(messagestr)
|
||||
if 'UID' not in options:
|
||||
self.ui.warn('No UID in message with options %s'%
|
||||
self.ui.warn('No UID in message with options %s' %
|
||||
str(options), minor=1)
|
||||
else:
|
||||
uid = int(options['UID'])
|
||||
@ -330,11 +329,11 @@ class IMAPFolder(BaseFolder):
|
||||
data = data[0][1].replace(CRLF, "\n")
|
||||
|
||||
if len(data) > 200:
|
||||
dbg_output = "%s...%s"% (str(data)[:150], str(data)[-50:])
|
||||
dbg_output = "%s...%s" % (str(data)[:150], str(data)[-50:])
|
||||
else:
|
||||
dbg_output = data
|
||||
|
||||
self.ui.debug('imap', "Returned object from fetching %d: '%s'"%
|
||||
self.ui.debug('imap', "Returned object from fetching %d: '%s'" %
|
||||
(uid, dbg_output))
|
||||
|
||||
return data
|
||||
@ -374,13 +373,12 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
# Compute unsigned crc32 of 'content' as unique hash.
|
||||
# NB: crc32 returns unsigned only starting with python 3.0.
|
||||
headervalue = str( binascii.crc32(content) & 0xffffffff ) + '-'
|
||||
headervalue += str(self.randomgenerator.randint(0,9999999999))
|
||||
headervalue = str(binascii.crc32(content) & 0xffffffff) + '-'
|
||||
headervalue += str(self.randomgenerator.randint(0, 9999999999))
|
||||
return (headername, headervalue)
|
||||
|
||||
|
||||
def __savemessage_searchforheader(self, imapobj, headername, headervalue):
|
||||
self.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)
|
||||
@ -391,14 +389,14 @@ class IMAPFolder(BaseFolder):
|
||||
# IMAP server doesn't implement search or had a problem.
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: got IMAP "
|
||||
"error '%s' while attempting to UID SEARCH for message with "
|
||||
"header %s"% (err, headername))
|
||||
"header %s" % (err, headername))
|
||||
return 0
|
||||
self.ui.debug('imap', "__savemessage_searchforheader got initial "
|
||||
"matchinguids: " + repr(matchinguids))
|
||||
|
||||
if matchinguids == '':
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: UID SEARCH "
|
||||
"for message with header %s yielded no results"% headername)
|
||||
"for message with header %s yielded no results" % headername)
|
||||
return 0
|
||||
|
||||
matchinguids = matchinguids.split(' ')
|
||||
@ -407,7 +405,7 @@ class IMAPFolder(BaseFolder):
|
||||
if len(matchinguids) != 1 or matchinguids[0] is None:
|
||||
raise OfflineImapError(
|
||||
"While attempting to find UID for message with "
|
||||
"header %s, got wrong-sized matchinguids of %s"%
|
||||
"header %s, got wrong-sized matchinguids of %s" %
|
||||
(headername, str(matchinguids)),
|
||||
OfflineImapError.ERROR.MESSAGE
|
||||
)
|
||||
@ -436,7 +434,7 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
Returns UID when found, 0 when not found."""
|
||||
|
||||
self.ui.debug('imap', '__savemessage_fetchheaders called for %s: %s'% \
|
||||
self.ui.debug('imap', '__savemessage_fetchheaders called for %s: %s' % \
|
||||
(headername, headervalue))
|
||||
|
||||
# Run "fetch X:* rfc822.header".
|
||||
@ -456,9 +454,9 @@ class IMAPFolder(BaseFolder):
|
||||
# with the range X:*. So we use bytearray to stop imaplib from getting
|
||||
# in our way.
|
||||
|
||||
result = imapobj.uid('FETCH', bytearray('%d:*'% start), 'rfc822.header')
|
||||
result = imapobj.uid('FETCH', bytearray('%d:*' % start), 'rfc822.header')
|
||||
if result[0] != 'OK':
|
||||
raise OfflineImapError('Error fetching mail headers: %s'%
|
||||
raise OfflineImapError('Error fetching mail headers: %s' %
|
||||
'. '.join(result[1]), OfflineImapError.ERROR.MESSAGE)
|
||||
|
||||
# result is like:
|
||||
@ -474,7 +472,7 @@ class IMAPFolder(BaseFolder):
|
||||
for item in result:
|
||||
if found is None and type(item) == tuple:
|
||||
# Walk just tuples.
|
||||
if re.search("(?:^|\\r|\\n)%s:\s*%s(?:\\r|\\n)"% (headername, headervalue),
|
||||
if re.search("(?:^|\\r|\\n)%s:\s*%s(?:\\r|\\n)" % (headername, headervalue),
|
||||
item[1], flags=re.IGNORECASE):
|
||||
found = item[0]
|
||||
elif found is not None:
|
||||
@ -493,12 +491,12 @@ class IMAPFolder(BaseFolder):
|
||||
if uid:
|
||||
return int(uid.group(1))
|
||||
|
||||
self.ui.warn("Can't parse FETCH response, can't find UID in %s"%
|
||||
self.ui.warn("Can't parse FETCH response, can't find UID in %s" %
|
||||
item
|
||||
)
|
||||
self.ui.debug('imap', "Got: %s"% repr(result))
|
||||
self.ui.debug('imap', "Got: %s" % repr(result))
|
||||
else:
|
||||
self.ui.warn("Can't parse FETCH response, we awaited string: %s"%
|
||||
self.ui.warn("Can't parse FETCH response, we awaited string: %s" %
|
||||
repr(item)
|
||||
)
|
||||
|
||||
@ -557,13 +555,13 @@ class IMAPFolder(BaseFolder):
|
||||
# will rause a ValueError if the year is 0102 but not 1902,
|
||||
# but some IMAP servers nonetheless choke on 1902.
|
||||
self.ui.debug('imap', "Message with invalid date %s. "
|
||||
"Server will use local time."% datetuple)
|
||||
"Server will use local time." % datetuple)
|
||||
return None
|
||||
|
||||
# Produce a string representation of datetuple that works as
|
||||
# INTERNALDATE.
|
||||
num2mon = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun',
|
||||
7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
|
||||
num2mon = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
|
||||
7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
|
||||
|
||||
# tm_isdst coming from email.parsedate is not usable, we still use it
|
||||
# here, mhh.
|
||||
@ -571,9 +569,9 @@ class IMAPFolder(BaseFolder):
|
||||
zone = -time.altzone
|
||||
else:
|
||||
zone = -time.timezone
|
||||
offset_h, offset_m = divmod(zone//60, 60)
|
||||
offset_h, offset_m = divmod(zone // 60, 60)
|
||||
|
||||
internaldate = '"%02d-%s-%04d %02d:%02d:%02d %+03d%02d"'% \
|
||||
internaldate = '"%02d-%s-%04d %02d:%02d:%02d %+03d%02d"' % \
|
||||
(datetuple.tm_mday, num2mon[datetuple.tm_mon], datetuple.tm_year, \
|
||||
datetuple.tm_hour, datetuple.tm_min, datetuple.tm_sec, offset_h, offset_m)
|
||||
|
||||
@ -635,15 +633,15 @@ class IMAPFolder(BaseFolder):
|
||||
# Insert a random unique header that we can fetch later.
|
||||
(headername, headervalue) = self.__generate_randomheader(
|
||||
content)
|
||||
self.ui.debug('imap', 'savemessage: header is: %s: %s'%
|
||||
self.ui.debug('imap', 'savemessage: header is: %s: %s' %
|
||||
(headername, headervalue))
|
||||
content = self.addmessageheader(content, CRLF, headername, headervalue)
|
||||
|
||||
if len(content) > 200:
|
||||
dbg_output = "%s...%s"% (content[:150], content[-50:])
|
||||
dbg_output = "%s...%s" % (content[:150], content[-50:])
|
||||
else:
|
||||
dbg_output = content
|
||||
self.ui.debug('imap', "savemessage: date: %s, content: '%s'"%
|
||||
self.ui.debug('imap', "savemessage: date: %s, content: '%s'" %
|
||||
(date, dbg_output))
|
||||
|
||||
try:
|
||||
@ -670,7 +668,7 @@ class IMAPFolder(BaseFolder):
|
||||
# and continue with the next account.
|
||||
msg = \
|
||||
"Saving msg (%s) in folder '%s', repository '%s' failed (abort). " \
|
||||
"Server responded: %s %s\n"% \
|
||||
"Server responded: %s %s\n" % \
|
||||
(msg_id, self, self.getrepository(), typ, dat)
|
||||
raise OfflineImapError(msg, OfflineImapError.ERROR.REPO)
|
||||
retry_left = 0 # Mark as success.
|
||||
@ -683,7 +681,7 @@ class IMAPFolder(BaseFolder):
|
||||
six.reraise(OfflineImapError,
|
||||
OfflineImapError("Saving msg (%s) in folder '%s', "
|
||||
"repository '%s' failed (abort). Server responded: %s\n"
|
||||
"Message content was: %s"%
|
||||
"Message content was: %s" %
|
||||
(msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
OfflineImapError.ERROR.MESSAGE),
|
||||
exc_info()[2])
|
||||
@ -698,13 +696,13 @@ class IMAPFolder(BaseFolder):
|
||||
six.reraise(OfflineImapError,
|
||||
OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
|
||||
"failed (error). Server responded: %s\nMessage content was: "
|
||||
"%s"% (msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
"%s" % (msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
OfflineImapError.ERROR.MESSAGE),
|
||||
exc_info()[2])
|
||||
# Checkpoint. Let it write out stuff, etc. Eg searches for
|
||||
# just uploaded messages won't work if we don't do this.
|
||||
(typ,dat) = imapobj.check()
|
||||
assert(typ == 'OK')
|
||||
(typ, dat) = imapobj.check()
|
||||
assert (typ == 'OK')
|
||||
|
||||
# Get the new UID, do we use UIDPLUS?
|
||||
if use_uidplus:
|
||||
@ -717,19 +715,19 @@ class IMAPFolder(BaseFolder):
|
||||
resp = imapobj._get_untagged_response('APPENDUID')
|
||||
if resp == [None] or resp is None:
|
||||
self.ui.warn("Server supports UIDPLUS but got no APPENDUID "
|
||||
"appending a message. Got: %s."% str(resp))
|
||||
"appending a message. Got: %s." % str(resp))
|
||||
return 0
|
||||
try:
|
||||
uid = int(resp[-1].split(' ')[1])
|
||||
except ValueError as e:
|
||||
uid = 0 # Definetly not what we should have.
|
||||
except Exception as e:
|
||||
raise OfflineImapError("Unexpected response: %s"% str(resp),
|
||||
raise OfflineImapError("Unexpected response: %s" % str(resp),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
if uid == 0:
|
||||
self.ui.warn("savemessage: Server supports UIDPLUS, but"
|
||||
" we got no usable UID back. APPENDUID reponse was "
|
||||
"'%s'"% str(resp))
|
||||
"'%s'" % str(resp))
|
||||
else:
|
||||
try:
|
||||
# We don't use UIDPLUS.
|
||||
@ -744,13 +742,13 @@ class IMAPFolder(BaseFolder):
|
||||
headervalue)
|
||||
self.ui.warn("savemessage: Searching mails for new "
|
||||
"Message-ID failed. Could not determine new UID "
|
||||
"on %s."% self.getname())
|
||||
"on %s." % self.getname())
|
||||
# Something wrong happened while trying to get the UID. Explain
|
||||
# the error might be about the 'get UID' process not necesseraly
|
||||
# the APPEND.
|
||||
except Exception:
|
||||
self.ui.warn("%s: could not determine the UID while we got "
|
||||
"no error while appending the email with '%s: %s'"%
|
||||
"no error while appending the email with '%s: %s'" %
|
||||
(self.getname(), headername, headervalue)
|
||||
)
|
||||
raise
|
||||
@ -762,10 +760,9 @@ class IMAPFolder(BaseFolder):
|
||||
self.messagelist[uid] = self.msglist_item_initializer(uid)
|
||||
self.messagelist[uid]['flags'] = flags
|
||||
|
||||
self.ui.debug('imap', 'savemessage: returning new UID %d'% uid)
|
||||
self.ui.debug('imap', 'savemessage: returning new UID %d' % uid)
|
||||
return uid
|
||||
|
||||
|
||||
def _fetch_from_imap(self, uids, retry_num=1):
|
||||
"""Fetches data from IMAP server.
|
||||
|
||||
@ -777,7 +774,7 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
imapobj = self.imapserver.acquireconnection()
|
||||
try:
|
||||
query = "(%s)"% (" ".join(self.imap_query))
|
||||
query = "(%s)" % (" ".join(self.imap_query))
|
||||
fails_left = retry_num # Retry on dropped connection.
|
||||
while fails_left:
|
||||
try:
|
||||
@ -789,13 +786,13 @@ class IMAPFolder(BaseFolder):
|
||||
# self.ui.error() will show the original traceback.
|
||||
if fails_left <= 0:
|
||||
message = ("%s, while fetching msg %r in folder %r."
|
||||
" Max retry reached (%d)"%
|
||||
" Max retry reached (%d)" %
|
||||
(e, uids, self.name, retry_num))
|
||||
severity = OfflineImapError.ERROR.MESSAGE
|
||||
raise OfflineImapError(message,
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
self.ui.error("%s. While fetching msg %r in folder %r."
|
||||
" Query: %s Retrying (%d/%d)"% (
|
||||
" Query: %s Retrying (%d/%d)" % (
|
||||
e, uids, self.name, query,
|
||||
retry_num - fails_left, retry_num
|
||||
)
|
||||
@ -820,13 +817,13 @@ class IMAPFolder(BaseFolder):
|
||||
# data for the UID FETCH command.
|
||||
if data == [None] or res_type != 'OK' or len(data) != 1:
|
||||
severity = OfflineImapError.ERROR.MESSAGE
|
||||
reason = "IMAP server '%s' failed to fetch messages UID '%s'."\
|
||||
" Server responded: %s %s"% (self.getrepository(), uids,
|
||||
reason = "IMAP server '%s' failed to fetch messages UID '%s'." \
|
||||
" Server responded: %s %s" % (self.getrepository(), uids,
|
||||
res_type, data)
|
||||
if data == [None] or len(data) < 1:
|
||||
# IMAP server did not find a message with this UID.
|
||||
reason = "IMAP server '%s' does not have a message "\
|
||||
"with UID '%s'"% (self.getrepository(), uids)
|
||||
reason = "IMAP server '%s' does not have a message " \
|
||||
"with UID '%s'" % (self.getrepository(), uids)
|
||||
raise OfflineImapError(reason, severity)
|
||||
|
||||
# Convert bytes to str
|
||||
@ -836,7 +833,6 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
return ndata
|
||||
|
||||
|
||||
def _store_to_imap(self, imapobj, uid, field, data):
|
||||
"""Stores data to IMAP server
|
||||
|
||||
@ -850,8 +846,8 @@ class IMAPFolder(BaseFolder):
|
||||
res_type, retdata = imapobj.uid('store', uid, field, data)
|
||||
if res_type != 'OK':
|
||||
severity = OfflineImapError.ERROR.MESSAGE
|
||||
reason = "IMAP server '%s' failed to store %s for message UID '%d'."\
|
||||
"Server responded: %s %s"% (
|
||||
reason = "IMAP server '%s' failed to store %s for message UID '%d'." \
|
||||
"Server responded: %s %s" % (
|
||||
self.getrepository(), field, uid, res_type, retdata)
|
||||
raise OfflineImapError(reason, severity)
|
||||
return retdata[0]
|
||||
@ -916,7 +912,7 @@ class IMAPFolder(BaseFolder):
|
||||
imaputil.flagsmaildir2imap(flags))
|
||||
if response[0] != 'OK':
|
||||
raise OfflineImapError(
|
||||
'Error with store: %s'% '. '.join(response[1]),
|
||||
'Error with store: %s' % '. '.join(response[1]),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
response = response[1]
|
||||
finally:
|
||||
@ -947,7 +943,6 @@ class IMAPFolder(BaseFolder):
|
||||
elif operation == '-':
|
||||
self.messagelist[uid]['flags'] -= flags
|
||||
|
||||
|
||||
def __processmessagesflags(self, operation, uidlist, flags):
|
||||
# Hack for those IMAP servers with a limited line length.
|
||||
batch_size = 100
|
||||
@ -956,7 +951,6 @@ class IMAPFolder(BaseFolder):
|
||||
uidlist[i:i + batch_size], flags)
|
||||
return
|
||||
|
||||
|
||||
# Interface from BaseFolder
|
||||
def change_message_uid(self, uid, new_uid):
|
||||
"""Change the message from existing uid to new_uid
|
||||
@ -964,7 +958,7 @@ class IMAPFolder(BaseFolder):
|
||||
If the backend supports it. IMAP does not and will throw errors."""
|
||||
|
||||
raise OfflineImapError('IMAP backend cannot change a messages UID from '
|
||||
'%d to %d'% (uid, new_uid), OfflineImapError.ERROR.MESSAGE)
|
||||
'%d to %d' % (uid, new_uid), OfflineImapError.ERROR.MESSAGE)
|
||||
|
||||
# Interface from BaseFolder
|
||||
def deletemessage(self, uid):
|
||||
@ -987,7 +981,7 @@ class IMAPFolder(BaseFolder):
|
||||
self.ui.deletereadonly(self, uidlist)
|
||||
return
|
||||
if self.expunge:
|
||||
assert(imapobj.expunge()[0] == 'OK')
|
||||
assert (imapobj.expunge()[0] == 'OK')
|
||||
finally:
|
||||
self.imapserver.releaseconnection(imapobj)
|
||||
for uid in uidlist:
|
||||
|
Loading…
Reference in New Issue
Block a user