Reformat offlineimap/accounts.py

Add some spaces, remove lines,... now format is better (lintian).
This commit is contained in:
Rodolfo García Peñas (kix) 2020-08-29 20:05:18 +02:00
parent 58596767ef
commit 9f15c05520

View File

@ -39,7 +39,8 @@ except:
try: try:
import fcntl import fcntl
except: except:
pass # Ok if this fails, we can do without. pass # Ok if this fails, we can do without.
# FIXME: spaghetti code alert! # FIXME: spaghetti code alert!
def getaccountlist(customconfig): def getaccountlist(customconfig):
@ -80,8 +81,8 @@ class Account(CustomConfig.ConfigHelperMixin):
self.dryrun = self.config.getboolean('general', 'dry-run') self.dryrun = self.config.getboolean('general', 'dry-run')
self.quicknum = 0 self.quicknum = 0
if self.refreshperiod < 0: if self.refreshperiod < 0:
self.ui.warn("autorefresh for %s is negative, fixing it to 0."% self.ui.warn("autorefresh for %s is negative, fixing it to 0." %
name) name)
self.refreshperiod = 0.0 self.refreshperiod = 0.0
if self.refreshperiod == 0.0: if self.refreshperiod == 0.0:
self.refreshperiod = None self.refreshperiod = None
@ -150,7 +151,7 @@ class Account(CustomConfig.ConfigHelperMixin):
if skipsleep: if skipsleep:
self.config.set(self.getsection(), "skipsleep", '0') self.config.set(self.getsection(), "skipsleep", '0')
return skipsleep or Account.abort_soon_signal.is_set() or \ return skipsleep or Account.abort_soon_signal.is_set() or \
Account.abort_NOW_signal.is_set() Account.abort_NOW_signal.is_set()
def _sleeper(self): def _sleeper(self):
"""Sleep if the account is set to autorefresh. """Sleep if the account is set to autorefresh.
@ -192,11 +193,11 @@ class Account(CustomConfig.ConfigHelperMixin):
"""Output diagnostics for all involved repositories.""" """Output diagnostics for all involved repositories."""
remote_repo = Repository(self, 'remote') remote_repo = Repository(self, 'remote')
local_repo = Repository(self, 'local') local_repo = Repository(self, 'local')
#status_repo = Repository(self, 'status') # status_repo = Repository(self, 'status')
self.ui.serverdiagnostics(remote_repo, 'Remote') self.ui.serverdiagnostics(remote_repo, 'Remote')
self.ui.serverdiagnostics(local_repo, 'Local') self.ui.serverdiagnostics(local_repo, 'Local')
#self.ui.serverdiagnostics(statusrepos, 'Status') # self.ui.serverdiagnostics(statusrepos, 'Status')
def deletefolder(self, foldername): def deletefolder(self, foldername):
remote_repo = Repository(self, 'remote') remote_repo = Repository(self, 'remote')
@ -204,10 +205,10 @@ class Account(CustomConfig.ConfigHelperMixin):
try: try:
if self.dryrun: if self.dryrun:
self.ui.info("would try to remove '%s' on remote of '%s' " self.ui.info("would try to remove '%s' on remote of '%s' "
"account"% (foldername, self)) "account" % (foldername, self))
else: else:
remote_repo.deletefolder(foldername) remote_repo.deletefolder(foldername)
self.ui.info("Folder '%s' deleted."% foldername) self.ui.info("Folder '%s' deleted." % foldername)
return 0 return 0
except Exception as e: except Exception as e:
self.ui.error(e) self.ui.error(e)
@ -228,7 +229,7 @@ class SyncableAccount(Account):
Account.__init__(self, *args, **kwargs) Account.__init__(self, *args, **kwargs)
self._lockfd = None self._lockfd = None
self._lockfilepath = os.path.join( self._lockfilepath = os.path.join(
self.config.getmetadatadir(), "%s.lock"% self) self.config.getmetadatadir(), "%s.lock" % self)
def __lock(self): def __lock(self):
"""Lock the account, throwing an exception if it is locked already.""" """Lock the account, throwing an exception if it is locked already."""
@ -239,22 +240,22 @@ class SyncableAccount(Account):
except NameError: except NameError:
# portalocker not available for Windows. # portalocker not available for Windows.
try: try:
fcntl.lockf(self._lockfd, fcntl.LOCK_EX|fcntl.LOCK_NB) fcntl.lockf(self._lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except NameError: except NameError:
pass # fnctl not available, disable file locking... :( pass # fnctl not available, disable file locking... :(
except IOError: except IOError:
self._lockfd.close() self._lockfd.close()
six.reraise(OfflineImapError, six.reraise(OfflineImapError,
OfflineImapError( OfflineImapError(
"Could not lock account %s. Is another " "Could not lock account %s. Is another "
"instance using this account?"% self, "instance using this account?" % self,
OfflineImapError.ERROR.REPO), OfflineImapError.ERROR.REPO),
exc_info()[2]) exc_info()[2])
def _unlock(self): def _unlock(self):
"""Unlock the account, deleting the lock file""" """Unlock the account, deleting the lock file"""
#If we own the lock file, delete it # If we own the lock file, delete it
if self._lockfd and not self._lockfd.closed: if self._lockfd and not self._lockfd.closed:
try: try:
portalocker.unlock(self._lockfd) portalocker.unlock(self._lockfd)
@ -264,7 +265,7 @@ class SyncableAccount(Account):
try: try:
os.unlink(self._lockfilepath) os.unlink(self._lockfilepath)
except OSError: except OSError:
pass # Failed to delete for some reason. pass # Failed to delete for some reason.
def syncrunner(self): def syncrunner(self):
"""The target for both single and multi-threaded modes.""" """The target for both single and multi-threaded modes."""
@ -276,7 +277,7 @@ class SyncableAccount(Account):
os.mkdir(accountmetadata, 0o700) os.mkdir(accountmetadata, 0o700)
self.remoterepos = Repository(self, 'remote') self.remoterepos = Repository(self, 'remote')
self.localrepos = Repository(self, 'local') self.localrepos = Repository(self, 'local')
self.statusrepos = Repository(self, 'status') self.statusrepos = Repository(self, 'status')
except OfflineImapError as e: except OfflineImapError as e:
self.ui.error(e, exc_info()[2]) self.ui.error(e, exc_info()[2])
@ -303,7 +304,7 @@ class SyncableAccount(Account):
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], msg= self.ui.error(e, exc_info()[2], msg=
"While attempting to sync account '%s'"% self) "While attempting to sync account '%s'" % self)
else: else:
# After success sync, reset the looping counter to 3. # After success sync, reset the looping counter to 3.
if self.refreshperiod: if self.refreshperiod:
@ -319,8 +320,7 @@ class SyncableAccount(Account):
return self.localrepos.getfolder( return self.localrepos.getfolder(
remotefolder.getvisiblename(). remotefolder.getvisiblename().
replace(self.remoterepos.getsep(), self.localrepos.getsep())) replace(self.remoterepos.getsep(), self.localrepos.getsep()))
# The syncrunner will loop on this method. This means it is called more than # The syncrunner will loop on this method. This means it is called more than
# once during the run. # once during the run.
@ -338,12 +338,12 @@ class SyncableAccount(Account):
if self.utf_8_support and self.remoterepos.getdecodefoldernames(): if self.utf_8_support and self.remoterepos.getdecodefoldernames():
raise OfflineImapError("Configuration mismatch in account " + raise OfflineImapError("Configuration mismatch in account " +
"'%s'. "% self.getname() + "'%s'. " % self.getname() +
"\nAccount setting 'utf8foldernames' and repository " + "\nAccount setting 'utf8foldernames' and repository " +
"setting 'decodefoldernames'\nmay not be used at the " + "setting 'decodefoldernames'\nmay not be used at the " +
"same time. This account has not been synchronized.\n" + "same time. This account has not been synchronized.\n" +
"Please check the configuration and documentation.", "Please check the configuration and documentation.",
OfflineImapError.ERROR.REPO) OfflineImapError.ERROR.REPO)
quickconfig = self.getconfint('quick', 0) quickconfig = self.getconfint('quick', 0)
if quickconfig < 0: if quickconfig < 0:
@ -382,33 +382,33 @@ class SyncableAccount(Account):
if not remotefolder.sync_this: if not remotefolder.sync_this:
self.ui.debug('', "Not syncing filtered folder '%s'" self.ui.debug('', "Not syncing filtered folder '%s'"
"[%s]"% (remotefolder.getname(), remoterepos)) "[%s]" % (remotefolder.getname(), remoterepos))
continue # Ignore filtered folder. continue # Ignore filtered folder.
# The remote folder names must not have the local sep char in # The remote folder names must not have the local sep char in
# their names since this would cause troubles while converting # their names since this would cause troubles while converting
# the name back (from local to remote). # the name back (from local to remote).
sep = localrepos.getsep() sep = localrepos.getsep()
if (sep != os.path.sep and if (sep != os.path.sep and
sep != remoterepos.getsep() and sep != remoterepos.getsep() and
sep in remotefolder.getname()): sep in remotefolder.getname()):
self.ui.warn('', "Ignoring folder '%s' due to unsupported " self.ui.warn('', "Ignoring folder '%s' due to unsupported "
"'%s' character serving as local separator."% "'%s' character serving as local separator." %
(remotefolder.getname(), localrepos.getsep())) (remotefolder.getname(), localrepos.getsep()))
continue # Ignore unsupported folder name. continue # Ignore unsupported folder name.
localfolder = self.get_local_folder(remotefolder) localfolder = self.get_local_folder(remotefolder)
if not localfolder.sync_this: if not localfolder.sync_this:
self.ui.debug('', "Not syncing filtered folder '%s'" self.ui.debug('', "Not syncing filtered folder '%s'"
"[%s]"% (localfolder.getname(), localfolder.repository)) "[%s]" % (localfolder.getname(), localfolder.repository))
continue # Ignore filtered folder. continue # Ignore filtered folder.
if not globals.options.singlethreading: if not globals.options.singlethreading:
thread = InstanceLimitedThread( thread = InstanceLimitedThread(
limitNamespace="%s%s"% ( limitNamespace="%s%s" % (
FOLDER_NAMESPACE, self.remoterepos.getname()), FOLDER_NAMESPACE, self.remoterepos.getname()),
target=syncfolder, target=syncfolder,
name="Folder %s [acc: %s]"% ( name="Folder %s [acc: %s]" % (
remotefolder.getexplainedname(), self), remotefolder.getexplainedname(), self),
args=(self, remotefolder, quick) args=(self, remotefolder, quick)
) )
@ -421,7 +421,7 @@ class SyncableAccount(Account):
for thr in folderthreads: for thr in folderthreads:
thr.join() thr.join()
if startedThread is True: if startedThread is True:
mbnames.writeIntermediateFile(self.name) # Write out mailbox names. mbnames.writeIntermediateFile(self.name) # Write out mailbox names.
else: else:
msg = "Account {}: no folder to sync (folderfilter issue?)".format(self) msg = "Account {}: no folder to sync (folderfilter issue?)".format(self)
raise OfflineImapError(msg, OfflineImapError.ERROR.REPO) raise OfflineImapError(msg, OfflineImapError.ERROR.REPO)
@ -455,15 +455,15 @@ class SyncableAccount(Account):
stdin=PIPE, stdout=PIPE, stderr=PIPE, stdin=PIPE, stdout=PIPE, stderr=PIPE,
close_fds=True) close_fds=True)
r = p.communicate() r = p.communicate()
self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n"% r) self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n" % r)
self.ui.callhook("Hook return code: %d"% p.returncode) self.ui.callhook("Hook return code: %d" % p.returncode)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
raise raise
except Exception as e: except Exception as e:
self.ui.error(e, exc_info()[2], msg="Calling hook") self.ui.error(e, exc_info()[2], msg="Calling hook")
#XXX: This function should likely be refactored. This should not be passed the # XXX: This function should likely be refactored. This should not be passed the
# account instance. # account instance.
def syncfolder(account, remotefolder, quick): def syncfolder(account, remotefolder, quick):
"""Synchronizes given remote folder for the specified account. """Synchronizes given remote folder for the specified account.
@ -483,7 +483,7 @@ def syncfolder(account, remotefolder, quick):
# The localfolder full name is good to uniquely identify the sync # The localfolder full name is good to uniquely identify the sync
# transaction. # transaction.
if SYNC_MUTEXES[account_name].get(localfolder_name) is None: if SYNC_MUTEXES[account_name].get(localfolder_name) is None:
#XXX: This lock could be an external file lock so we can remove # XXX: This lock could be an external file lock so we can remove
# the lock at the account level. # the lock at the account level.
SYNC_MUTEXES[account_name][localfolder_name] = Lock() SYNC_MUTEXES[account_name][localfolder_name] = Lock()
@ -517,7 +517,7 @@ def syncfolder(account, remotefolder, quick):
"""Returns messages with uid > min(uids of messages newer than date).""" """Returns messages with uid > min(uids of messages newer than date)."""
remotefolder.cachemessagelist( remotefolder.cachemessagelist(
min_date=time.gmtime(time.mktime(date) + 24*60*60)) min_date=time.gmtime(time.mktime(date) + 24 * 60 * 60))
uids = remotefolder.getmessageuidlist() uids = remotefolder.getmessageuidlist()
localfolder.dropmessagelistcache() localfolder.dropmessagelistcache()
if len(uids) > 0: if len(uids) > 0:
@ -561,12 +561,12 @@ def syncfolder(account, remotefolder, quick):
new.cachemessagelist() new.cachemessagelist()
min_uid = partial.retrieve_min_uid() min_uid = partial.retrieve_min_uid()
if min_uid == None: # min_uid file didn't exist if min_uid == None: # min_uid file didn't exist
if len(new.getmessageuidlist()) > 0: if len(new.getmessageuidlist()) > 0:
raise OfflineImapError("To use startdate on Repository %s, " raise OfflineImapError("To use startdate on Repository %s, "
"Repository %s must be empty"% "Repository %s must be empty" %
(partial.repository.name, new.repository.name), (partial.repository.name, new.repository.name),
OfflineImapError.ERROR.MESSAGE) OfflineImapError.ERROR.MESSAGE)
else: else:
partial.cachemessagelist(min_date=date) partial.cachemessagelist(min_date=date)
# messagelist.keys() instead of getuidmessagelist() because in # messagelist.keys() instead of getuidmessagelist() because in
@ -581,7 +581,6 @@ def syncfolder(account, remotefolder, quick):
else: else:
partial.cachemessagelist(min_uid=min_uid) partial.cachemessagelist(min_uid=min_uid)
remoterepos = account.remoterepos remoterepos = account.remoterepos
localrepos = account.localrepos localrepos = account.localrepos
statusrepos = account.statusrepos statusrepos = account.statusrepos
@ -597,11 +596,11 @@ def syncfolder(account, remotefolder, quick):
# Add the folder to the mbnames mailboxes. # Add the folder to the mbnames mailboxes.
mbnames.add(account.name, localrepos.getlocalroot(), mbnames.add(account.name, localrepos.getlocalroot(),
localfolder.getname()) localfolder.getname())
# Load status folder. # Load status folder.
statusfolder = statusrepos.getfolder(remotefolder.getvisiblename(). statusfolder = statusrepos.getfolder(remotefolder.getvisiblename().
replace(remoterepos.getsep(), statusrepos.getsep())) replace(remoterepos.getsep(), statusrepos.getsep()))
statusfolder.openfiles() statusfolder.openfiles()
statusfolder.cachemessagelist() statusfolder.cachemessagelist()
@ -616,32 +615,32 @@ def syncfolder(account, remotefolder, quick):
if (maxage != None) + (localstart != None) + (remotestart != None) > 1: if (maxage != None) + (localstart != None) + (remotestart != None) > 1:
six.reraise(OfflineImapError, six.reraise(OfflineImapError,
OfflineImapError("You can set at most one of the " OfflineImapError("You can set at most one of the "
"following: maxage, startdate (for the local " "following: maxage, startdate (for the local "
"folder), startdate (for the remote folder)", "folder), startdate (for the remote folder)",
OfflineImapError.ERROR.REPO), OfflineImapError.ERROR.REPO),
exc_info()[2]) exc_info()[2])
if (maxage != None or localstart or remotestart) and quick: if (maxage != None or localstart or remotestart) and quick:
# IMAP quickchanged isn't compatible with options that # IMAP quickchanged isn't compatible with options that
# involve restricting the messagelist, since the "quick" # involve restricting the messagelist, since the "quick"
# check can only retrieve a full list of UIDs in the folder. # check can only retrieve a full list of UIDs in the folder.
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(maxage) cachemessagelists_upto_date(maxage)
check_uid_validity() check_uid_validity()
elif localstart != None: elif localstart != None:
cachemessagelists_startdate(remotefolder, localfolder, cachemessagelists_startdate(remotefolder, localfolder,
localstart) localstart)
check_uid_validity() check_uid_validity()
elif remotestart != None: elif remotestart != None:
cachemessagelists_startdate(localfolder, remotefolder, cachemessagelists_startdate(localfolder, remotefolder,
remotestart) remotestart)
check_uid_validity() check_uid_validity()
else: else:
localfolder.cachemessagelist() localfolder.cachemessagelist()
if quick: if quick:
if (not localfolder.quickchanged(statusfolder) and if (not localfolder.quickchanged(statusfolder) and
not remotefolder.quickchanged(statusfolder)): not remotefolder.quickchanged(statusfolder)):
ui.skippingfolder(remotefolder) ui.skippingfolder(remotefolder)
localrepos.restore_atime() localrepos.restore_atime()
return return
@ -653,16 +652,16 @@ def syncfolder(account, remotefolder, quick):
ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder) ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder)
remotefolder.syncmessagesto(localfolder, statusfolder) remotefolder.syncmessagesto(localfolder, statusfolder)
else: else:
ui.debug('', "Not syncing to read-only repository '%s'"% ui.debug('', "Not syncing to read-only repository '%s'" %
localrepos.getname()) localrepos.getname())
# Synchronize local changes. # Synchronize local changes.
if not remoterepos.getconfboolean('readonly', False): if not remoterepos.getconfboolean('readonly', False):
ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder) ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder)
localfolder.syncmessagesto(remotefolder, statusfolder) localfolder.syncmessagesto(remotefolder, statusfolder)
else: else:
ui.debug('', "Not syncing to read-only repository '%s'"% ui.debug('', "Not syncing to read-only repository '%s'" %
remoterepos.getname()) remoterepos.getname())
statusfolder.save() statusfolder.save()
localrepos.restore_atime() localrepos.restore_atime()
@ -674,10 +673,10 @@ def syncfolder(account, remotefolder, quick):
raise raise
else: else:
ui.error(e, exc_info()[2], msg="Aborting sync, folder '%s' " ui.error(e, exc_info()[2], msg="Aborting sync, folder '%s' "
"[acc: '%s']"% (localfolder, account)) "[acc: '%s']" % (localfolder, account))
except Exception as e: except Exception as e:
ui.error(e, msg="ERROR in syncfolder for %s folder %s: %s"% ui.error(e, msg="ERROR in syncfolder for %s folder %s: %s" %
(account, remotefolder.getvisiblename(), traceback.format_exc())) (account, remotefolder.getvisiblename(), traceback.format_exc()))
finally: finally:
for folder in ["statusfolder", "localfolder", "remotefolder"]: for folder in ["statusfolder", "localfolder", "remotefolder"]:
if folder in locals(): if folder in locals():