2002-06-21 07:29:12 +02:00
|
|
|
# UI base class
|
2018-08-13 01:15:01 +02:00
|
|
|
# Copyright (C) 2002-2018 John Goerzen & contributors.
|
2002-06-21 07:29:12 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-21 07:29:12 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
import logging
|
2015-10-12 19:35:57 +02:00
|
|
|
import logging.handlers
|
2011-03-06 20:03:06 +01:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import threading
|
2012-02-05 11:55:26 +01:00
|
|
|
try:
|
2020-08-28 03:32:43 +02:00
|
|
|
from queue import Queue
|
2016-06-28 23:58:03 +02:00
|
|
|
except ImportError: # python3
|
2012-02-05 11:55:26 +01:00
|
|
|
from queue import Queue
|
2011-10-26 16:47:21 +02:00
|
|
|
from collections import deque
|
2016-06-28 23:58:03 +02:00
|
|
|
|
2011-01-25 12:22:26 +01:00
|
|
|
import offlineimap
|
2016-06-28 23:58:03 +02:00
|
|
|
from offlineimap.error import OfflineImapError
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-05-01 20:18:28 +02:00
|
|
|
debugtypes = {'':'Other offlineimap related sync messages',
|
|
|
|
'imap': 'IMAP protocol debugging',
|
2003-06-02 21:06:18 +02:00
|
|
|
'maildir': 'Maildir repository debugging',
|
|
|
|
'thread': 'Threading debugging'}
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
globalui = None
|
|
|
|
def setglobalui(newui):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Set the global ui object to be used for logging."""
|
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
global globalui
|
|
|
|
globalui = newui
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def getglobalui():
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return the current ui object."""
|
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
global globalui
|
|
|
|
return globalui
|
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
class UIBase(object):
|
2015-01-01 21:41:11 +01:00
|
|
|
def __init__(self, config, loglevel=logging.INFO):
|
2011-10-26 16:47:21 +02:00
|
|
|
self.config = config
|
2011-09-15 13:24:48 +02:00
|
|
|
# Is this a 'dryrun'?
|
2012-04-03 01:13:27 +02:00
|
|
|
self.dryrun = config.getdefaultboolean('general', 'dry-run', False)
|
2011-10-26 16:47:21 +02:00
|
|
|
self.debuglist = []
|
2015-01-01 21:41:11 +01:00
|
|
|
# list of debugtypes we are supposed to log
|
2011-10-26 16:47:21 +02:00
|
|
|
self.debugmessages = {}
|
2015-01-01 21:41:11 +01:00
|
|
|
# debugmessages in a deque(v) per thread(k)
|
2011-11-03 14:21:25 +01:00
|
|
|
self.debugmsglen = 15
|
2011-10-26 16:47:21 +02:00
|
|
|
self.threadaccounts = {}
|
2015-01-01 21:41:11 +01:00
|
|
|
# dict linking active threads (k) to account names (v)
|
2011-10-26 16:47:21 +02:00
|
|
|
self.acct_startimes = {}
|
2015-01-01 21:41:11 +01:00
|
|
|
# linking active accounts with the time.time() when sync started
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logfile = None
|
|
|
|
self.exc_queue = Queue()
|
2015-01-01 21:41:11 +01:00
|
|
|
# saves all occuring exceptions, so we can output them at the end
|
2015-05-05 19:35:37 +02:00
|
|
|
self.uidval_problem = False
|
|
|
|
# at least one folder skipped due to UID validity problem
|
2011-10-26 16:47:21 +02:00
|
|
|
# create logger with 'OfflineImap' app
|
|
|
|
self.logger = logging.getLogger('OfflineImap')
|
|
|
|
self.logger.setLevel(loglevel)
|
|
|
|
self._log_con_handler = self.setup_consolehandler()
|
2015-01-14 22:58:25 +01:00
|
|
|
"""The console handler (we need access to be able to lock it)."""
|
2011-08-11 12:22:34 +02:00
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## UTILS
|
2011-10-26 16:47:21 +02:00
|
|
|
def setup_consolehandler(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Backend specific console handler.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
Sets up things and adds them to self.logger.
|
|
|
|
:returns: The logging.Handler() for console output"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
# create console handler with a higher log level
|
2012-01-07 03:04:26 +01:00
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
2011-10-26 16:47:21 +02:00
|
|
|
#ch.setLevel(logging.DEBUG)
|
|
|
|
# create formatter and add it to the handlers
|
|
|
|
self.formatter = logging.Formatter("%(message)s")
|
|
|
|
ch.setFormatter(self.formatter)
|
|
|
|
# add the handlers to the logger
|
|
|
|
self.logger.addHandler(ch)
|
|
|
|
self.logger.info(offlineimap.banner)
|
|
|
|
return ch
|
|
|
|
|
2015-09-27 02:43:09 +02:00
|
|
|
def setup_sysloghandler(self):
|
|
|
|
"""Backend specific syslog handler."""
|
|
|
|
|
2015-10-12 19:35:57 +02:00
|
|
|
# create syslog handler
|
|
|
|
ch = logging.handlers.SysLogHandler('/dev/log')
|
2015-09-27 02:43:09 +02:00
|
|
|
# create formatter and add it to the handlers
|
|
|
|
self.formatter = logging.Formatter("%(message)s")
|
|
|
|
ch.setFormatter(self.formatter)
|
|
|
|
# add the handlers to the logger
|
|
|
|
self.logger.addHandler(ch)
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def setlogfile(self, logfile):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Create file handler which logs to file."""
|
|
|
|
|
2011-11-02 10:56:42 +01:00
|
|
|
fh = logging.FileHandler(logfile, 'at')
|
Revert "Don't output initial blurb in "quiet" mode"
This reverts commit a1dc76ae917d93d0c387bbd506faea3487211a6c.
Causes a crash when using Blinkenlights UI with -l CLI option.
$ ./offlineimap.py -c offlineimap.conf.minimal -u blinkenlights -l foo
Traceback (most recent call last):
File "./offlineimap.py", line 36, in <module>
oi.run()
File "/tmp/offlineimap/offlineimap/init.py", line 50, in run
options, args = self.__parse_cmd_options()
File "/tmp/offlineimap/offlineimap/init.py", line 205, in __parse_cmd_options
self.ui.setlogfile(options.logfile)
File "/tmp/offlineimap/offlineimap/ui/UIBase.py", line 119, in setlogfile
self.logger.info(msg)
File "/usr/lib/python2.7/logging/__init__.py", line 1159, in info
self._log(INFO, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/tmp/offlineimap/offlineimap/ui/Curses.py", line 305, in emit
color = self.ui.gettf().curses_color
AttributeError: 'CursesLogHandler' object has no attribute 'ui'
Reported-by: iliastsi
Github-issue: #293
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2016-01-08 17:30:54 +01:00
|
|
|
#fh.setLevel(logging.DEBUG)
|
2011-10-26 16:47:21 +02:00
|
|
|
file_formatter = logging.Formatter("%(asctime)s %(levelname)s: "
|
2015-01-14 22:58:25 +01:00
|
|
|
"%(message)s", '%Y-%m-%d %H:%M:%S')
|
2011-10-26 16:47:21 +02:00
|
|
|
fh.setFormatter(file_formatter)
|
|
|
|
self.logger.addHandler(fh)
|
|
|
|
# write out more verbose initial info blurb on the log file
|
|
|
|
p_ver = ".".join([str(x) for x in sys.version_info[0:3]])
|
|
|
|
msg = "OfflineImap %s starting...\n Python: %s Platform: %s\n "\
|
2016-03-05 00:58:59 +01:00
|
|
|
"Args: %s"% (offlineimap.__version__, p_ver, sys.platform,
|
2011-10-26 16:47:21 +02:00
|
|
|
" ".join(sys.argv))
|
Revert "Don't output initial blurb in "quiet" mode"
This reverts commit a1dc76ae917d93d0c387bbd506faea3487211a6c.
Causes a crash when using Blinkenlights UI with -l CLI option.
$ ./offlineimap.py -c offlineimap.conf.minimal -u blinkenlights -l foo
Traceback (most recent call last):
File "./offlineimap.py", line 36, in <module>
oi.run()
File "/tmp/offlineimap/offlineimap/init.py", line 50, in run
options, args = self.__parse_cmd_options()
File "/tmp/offlineimap/offlineimap/init.py", line 205, in __parse_cmd_options
self.ui.setlogfile(options.logfile)
File "/tmp/offlineimap/offlineimap/ui/UIBase.py", line 119, in setlogfile
self.logger.info(msg)
File "/usr/lib/python2.7/logging/__init__.py", line 1159, in info
self._log(INFO, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/tmp/offlineimap/offlineimap/ui/Curses.py", line 305, in emit
color = self.ui.gettf().curses_color
AttributeError: 'CursesLogHandler' object has no attribute 'ui'
Reported-by: iliastsi
Github-issue: #293
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2016-01-08 17:30:54 +01:00
|
|
|
record = logging.LogRecord('OfflineImap', logging.INFO, __file__,
|
|
|
|
None, msg, None, None)
|
|
|
|
fh.emit(record)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def _msg(self, msg):
|
|
|
|
"""Display a message."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO: legacy function, rip out.
|
|
|
|
self.info(msg)
|
2003-06-02 21:06:18 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def info(self, msg):
|
2003-06-02 21:06:18 +02:00
|
|
|
"""Display a message."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logger.info(msg)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
def warn(self, msg, minor=0):
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logger.warning(msg)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-08-11 12:22:34 +02:00
|
|
|
def error(self, exc, exc_traceback=None, msg=None):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Log a message at severity level ERROR.
|
2011-08-11 12:22:34 +02:00
|
|
|
|
|
|
|
Log Exception 'exc' to error log, possibly prepended by a preceding
|
|
|
|
error "msg", detailing at what point the error occurred.
|
|
|
|
|
|
|
|
In debug mode, we also output the full traceback that occurred
|
2011-08-14 10:42:21 +02:00
|
|
|
if one has been passed in via sys.info()[2].
|
2011-08-11 12:22:34 +02:00
|
|
|
|
|
|
|
Also save the Exception to a stack that can be output at the end
|
|
|
|
of the sync run when offlineiamp exits. It is recommended to
|
|
|
|
always pass in exceptions if possible, so we can give the user
|
|
|
|
the best debugging info.
|
2016-06-23 00:20:57 +02:00
|
|
|
|
2013-08-27 07:41:18 +02:00
|
|
|
We are always pushing tracebacks to the exception queue to
|
|
|
|
make them to be output at the end of the run to allow users
|
|
|
|
pass sensible diagnostics to the developers or to solve
|
|
|
|
problems by themselves.
|
2011-08-11 12:22:34 +02:00
|
|
|
|
|
|
|
One example of such a call might be:
|
|
|
|
|
2011-08-14 10:42:21 +02:00
|
|
|
ui.error(exc, sys.exc_info()[2], msg="While syncing Folder %s in "
|
2015-01-14 22:58:25 +01:00
|
|
|
"repo %s")
|
2011-08-11 12:22:34 +02:00
|
|
|
"""
|
|
|
|
if msg:
|
2016-07-29 05:25:06 +02:00
|
|
|
self.logger.error("ERROR: %s\n %s"% (msg, exc))
|
2011-08-11 12:22:34 +02:00
|
|
|
else:
|
2016-07-29 05:25:06 +02:00
|
|
|
self.logger.error("ERROR: %s"% (exc))
|
2011-08-11 12:22:34 +02:00
|
|
|
|
2013-08-27 07:41:18 +02:00
|
|
|
instant_traceback = exc_traceback
|
2011-08-11 12:22:34 +02:00
|
|
|
if not self.debuglist:
|
|
|
|
# only output tracebacks in debug mode
|
2013-08-27 07:41:18 +02:00
|
|
|
instant_traceback = None
|
2011-08-11 12:22:34 +02:00
|
|
|
# push exc on the queue for later output
|
|
|
|
self.exc_queue.put((msg, exc, exc_traceback))
|
2013-08-27 07:41:18 +02:00
|
|
|
if instant_traceback:
|
2014-12-28 00:55:08 +01:00
|
|
|
self.logger.error(traceback.format_tb(instant_traceback))
|
2011-08-11 12:22:34 +02:00
|
|
|
|
2011-09-29 15:43:01 +02:00
|
|
|
def registerthread(self, account):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Register current thread as being associated with an account name."""
|
|
|
|
|
2011-09-29 15:43:01 +02:00
|
|
|
cur_thread = threading.currentThread()
|
|
|
|
if cur_thread in self.threadaccounts:
|
|
|
|
# was already associated with an old account, update info
|
|
|
|
self.debug('thread', "Register thread '%s' (previously '%s', now "
|
2016-07-29 05:25:06 +02:00
|
|
|
"'%s')"% (cur_thread.getName(),
|
|
|
|
self.getthreadaccount(cur_thread), account))
|
2011-09-29 15:43:01 +02:00
|
|
|
else:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.debug('thread', "Register new thread '%s' (account '%s')"%
|
|
|
|
(cur_thread.getName(), account))
|
2011-09-29 15:43:01 +02:00
|
|
|
self.threadaccounts[cur_thread] = account
|
|
|
|
|
|
|
|
def unregisterthread(self, thr):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Unregister a thread as being associated with an account name."""
|
|
|
|
|
2012-02-05 13:40:06 +01:00
|
|
|
if thr in self.threadaccounts:
|
2011-09-29 15:43:01 +02:00
|
|
|
del self.threadaccounts[thr]
|
2016-07-29 05:25:06 +02:00
|
|
|
self.debug('thread', "Unregister thread '%s'"% thr.getName())
|
2011-09-29 15:43:01 +02:00
|
|
|
|
2015-01-01 21:41:11 +01:00
|
|
|
def getthreadaccount(self, thr=None):
|
2011-11-03 13:45:44 +01:00
|
|
|
"""Get Account() for a thread (current if None)
|
|
|
|
|
2015-01-01 21:41:11 +01:00
|
|
|
If no account has been registered with this thread, return 'None'."""
|
|
|
|
|
2011-11-03 13:45:44 +01:00
|
|
|
if thr == None:
|
2003-01-05 13:01:17 +01:00
|
|
|
thr = threading.currentThread()
|
2011-09-29 15:43:01 +02:00
|
|
|
if thr in self.threadaccounts:
|
|
|
|
return self.threadaccounts[thr]
|
2011-11-03 13:45:44 +01:00
|
|
|
return None
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def debug(self, debugtype, msg):
|
|
|
|
cur_thread = threading.currentThread()
|
2012-02-05 13:40:06 +01:00
|
|
|
if not cur_thread in self.debugmessages:
|
2011-10-26 16:47:21 +02:00
|
|
|
# deque(..., self.debugmsglen) would be handy but was
|
|
|
|
# introduced in p2.6 only, so we'll need to work around and
|
|
|
|
# shorten our debugmsg list manually :-(
|
|
|
|
self.debugmessages[cur_thread] = deque()
|
|
|
|
self.debugmessages[cur_thread].append("%s: %s" % (debugtype, msg))
|
2007-07-05 15:49:54 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
# Shorten queue if needed
|
|
|
|
if len(self.debugmessages[cur_thread]) > self.debugmsglen:
|
|
|
|
self.debugmessages[cur_thread].popleft()
|
2007-07-05 15:49:54 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
if debugtype in self.debuglist: # log if we are supposed to do so
|
|
|
|
self.logger.debug("[%s]: %s" % (debugtype, msg))
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def add_debug(self, debugtype):
|
2002-08-08 22:03:36 +02:00
|
|
|
global debugtypes
|
|
|
|
if debugtype in debugtypes:
|
2011-10-26 16:47:21 +02:00
|
|
|
if not debugtype in self.debuglist:
|
|
|
|
self.debuglist.append(debugtype)
|
|
|
|
self.debugging(debugtype)
|
2002-08-08 22:03:36 +02:00
|
|
|
else:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.invaliddebug(debugtype)
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def debugging(self, debugtype):
|
2002-08-08 22:03:36 +02:00
|
|
|
global debugtypes
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logger.debug("Now debugging for %s: %s" % (debugtype,
|
|
|
|
debugtypes[debugtype]))
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def invaliddebug(self, debugtype):
|
|
|
|
self.warn("Invalid debug type: %s" % debugtype)
|
2007-07-05 15:49:54 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def getnicename(self, object):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return the type of a repository or Folder as string.
|
2011-05-05 15:59:25 +02:00
|
|
|
|
|
|
|
(IMAP, Gmail, Maildir, etc...)"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-05-05 15:59:25 +02:00
|
|
|
prelimname = object.__class__.__name__.split('.')[-1]
|
2007-07-05 15:49:54 +02:00
|
|
|
# Strip off extra stuff.
|
|
|
|
return re.sub('(Folder|Repository)', '', prelimname)
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def isusable(self):
|
2002-07-12 03:38:36 +02:00
|
|
|
"""Returns true if this UI object is usable in the current
|
|
|
|
environment. For instance, an X GUI would return true if it's
|
|
|
|
being run in X with a valid DISPLAY setting, and false otherwise."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
return True
|
2002-07-12 03:38:36 +02:00
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## INPUT
|
|
|
|
|
2018-06-13 14:05:13 +02:00
|
|
|
def getpass(self, username, config, errmsg = None):
|
2015-01-14 22:58:25 +01:00
|
|
|
raise NotImplementedError("Prompting for a password is not supported"
|
|
|
|
" in this UI backend.")
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2015-01-01 21:55:24 +01:00
|
|
|
def folderlist(self, folder_list):
|
|
|
|
return ', '.join(["%s[%s]"% \
|
2015-01-14 22:58:25 +01:00
|
|
|
(self.getnicename(x), x.getname()) for x in folder_list])
|
2002-06-21 08:51:21 +02:00
|
|
|
|
2002-08-08 03:57:17 +02:00
|
|
|
################################################## WARNINGS
|
2011-10-26 16:47:21 +02:00
|
|
|
def msgtoreadonly(self, destfolder, uid, content, flags):
|
|
|
|
if self.config.has_option('general', 'ignore-readonly') and \
|
2015-01-14 22:58:25 +01:00
|
|
|
self.config.getboolean('general', 'ignore-readonly'):
|
2011-10-26 16:47:21 +02:00
|
|
|
return
|
|
|
|
self.warn("Attempted to synchronize message %d to folder %s[%s], "
|
2015-01-14 22:58:25 +01:00
|
|
|
"but that folder is read-only. The message will not be "
|
|
|
|
"copied to that folder."% (
|
|
|
|
uid, self.getnicename(destfolder), destfolder))
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def flagstoreadonly(self, destfolder, uidlist, flags):
|
|
|
|
if self.config.has_option('general', 'ignore-readonly') and \
|
|
|
|
self.config.getboolean('general', 'ignore-readonly'):
|
|
|
|
return
|
|
|
|
self.warn("Attempted to modify flags for messages %s in folder %s[%s], "
|
2015-01-14 22:58:25 +01:00
|
|
|
"but that folder is read-only. No flags have been modified "
|
|
|
|
"for that message."% (
|
|
|
|
str(uidlist), self.getnicename(destfolder), destfolder))
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2012-10-16 20:20:35 +02:00
|
|
|
def labelstoreadonly(self, destfolder, uidlist, labels):
|
|
|
|
if self.config.has_option('general', 'ignore-readonly') and \
|
|
|
|
self.config.getboolean('general', 'ignore-readonly'):
|
|
|
|
return
|
|
|
|
self.warn("Attempted to modify labels for messages %s in folder %s[%s], "
|
2015-01-14 22:58:25 +01:00
|
|
|
"but that folder is read-only. No labels have been modified "
|
|
|
|
"for that message."% (
|
|
|
|
str(uidlist), self.getnicename(destfolder), destfolder))
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def deletereadonly(self, destfolder, uidlist):
|
|
|
|
if self.config.has_option('general', 'ignore-readonly') and \
|
|
|
|
self.config.getboolean('general', 'ignore-readonly'):
|
|
|
|
return
|
|
|
|
self.warn("Attempted to delete messages %s in folder %s[%s], but that "
|
2015-01-14 22:58:25 +01:00
|
|
|
"folder is read-only. No messages have been deleted in that "
|
|
|
|
"folder."% (str(uidlist), self.getnicename(destfolder),
|
|
|
|
destfolder))
|
2002-08-08 03:57:17 +02:00
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## MESSAGES
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def init_banner(self):
|
2002-07-12 03:38:36 +02:00
|
|
|
"""Called when the UI starts. Must be called before any other UI
|
|
|
|
call except isusable(). Displays the copyright banner. This is
|
|
|
|
where the UI should do its setup -- TK, for instance, would
|
|
|
|
create the application window here."""
|
2011-10-26 16:47:21 +02:00
|
|
|
pass
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2016-07-29 05:25:06 +02:00
|
|
|
def connecting(self, reposname, hostname, port):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Log 'Establishing connection to'."""
|
|
|
|
|
2012-02-06 17:40:02 +01:00
|
|
|
if not self.logger.isEnabledFor(logging.INFO): return
|
2011-09-29 17:46:46 +02:00
|
|
|
displaystr = ''
|
|
|
|
hostname = hostname if hostname else ''
|
2015-01-14 22:58:25 +01:00
|
|
|
port = "%s"% port if port else ''
|
2011-09-29 17:46:46 +02:00
|
|
|
if hostname:
|
|
|
|
displaystr = ' to %s:%s' % (hostname, port)
|
2016-07-29 05:25:06 +02:00
|
|
|
self.logger.info("Establishing connection%s (%s)"%
|
|
|
|
(displaystr, reposname))
|
2002-07-25 06:46:27 +02:00
|
|
|
|
2011-09-29 17:22:02 +02:00
|
|
|
def acct(self, account):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Output that we start syncing an account (and start counting)."""
|
|
|
|
|
2011-09-29 17:22:02 +02:00
|
|
|
self.acct_startimes[account] = time.time()
|
2016-07-29 05:25:06 +02:00
|
|
|
self.logger.info("*** Processing account %s"% account)
|
2011-09-29 17:22:02 +02:00
|
|
|
|
|
|
|
def acctdone(self, account):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Output that we finished syncing an account (in which time)."""
|
|
|
|
|
2011-09-29 17:22:02 +02:00
|
|
|
sec = time.time() - self.acct_startimes[account]
|
|
|
|
del self.acct_startimes[account]
|
2015-01-14 22:58:25 +01:00
|
|
|
self.logger.info("*** Finished account '%s' in %d:%02d"%
|
|
|
|
(account, sec // 60, sec % 60))
|
2003-01-04 05:57:46 +01:00
|
|
|
|
2011-09-30 09:21:03 +02:00
|
|
|
def syncfolders(self, src_repo, dst_repo):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Log 'Copying folder structure...'."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
if self.logger.isEnabledFor(logging.DEBUG):
|
|
|
|
self.debug('', "Copying folder structure from %s to %s" %\
|
|
|
|
(src_repo, dst_repo))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Folder syncing
|
2011-09-15 15:37:52 +02:00
|
|
|
def makefolder(self, repo, foldername):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Called when a folder is created."""
|
|
|
|
|
2011-09-15 15:37:52 +02:00
|
|
|
prefix = "[DRYRUN] " if self.dryrun else ""
|
2015-01-14 22:58:25 +01:00
|
|
|
self.info(("{0}Creating folder {1}[{2}]".format(
|
|
|
|
prefix, foldername, repo)))
|
2011-09-15 15:37:52 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def syncingfolder(self, srcrepos, srcfolder, destrepos, destfolder):
|
2002-06-21 07:29:12 +02:00
|
|
|
"""Called when a folder sync operation is started."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
|
|
|
self.logger.info("Syncing %s: %s -> %s"% (srcfolder,
|
|
|
|
self.getnicename(srcrepos),
|
|
|
|
self.getnicename(destrepos)))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def skippingfolder(self, folder):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
"""Called when a folder sync operation is started."""
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logger.info("Skipping %s (not changed)" % folder)
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def validityproblem(self, folder):
|
2015-05-05 19:35:37 +02:00
|
|
|
self.uidval_problem = True
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logger.warning("UID validity problem for folder %s (repo %s) "
|
|
|
|
"(saved %d; got %d); skipping it. Please see FAQ "
|
2015-01-01 21:41:11 +01:00
|
|
|
"and manual on how to handle this."% \
|
2011-10-26 16:47:21 +02:00
|
|
|
(folder, folder.getrepository(),
|
2012-01-19 11:50:19 +01:00
|
|
|
folder.get_saveduidvalidity(), folder.get_uidvalidity()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def loadmessagelist(self, repos, folder):
|
2015-01-14 22:58:25 +01:00
|
|
|
self.logger.debug("Loading message list for %s[%s]"% (
|
2011-10-26 16:47:21 +02:00
|
|
|
self.getnicename(repos),
|
|
|
|
folder))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def messagelistloaded(self, repos, folder, count):
|
|
|
|
self.logger.debug("Message list for %s[%s] loaded: %d messages" % (
|
|
|
|
self.getnicename(repos), folder, count))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Message syncing
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def syncingmessages(self, sr, srcfolder, dr, dstfolder):
|
|
|
|
self.logger.debug("Syncing messages %s[%s] -> %s[%s]" % (
|
|
|
|
self.getnicename(sr), srcfolder,
|
|
|
|
self.getnicename(dr), dstfolder))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2016-06-28 23:58:03 +02:00
|
|
|
def ignorecopyingmessage(self, uid, src, destfolder):
|
|
|
|
"""Output a log line stating which message is ignored."""
|
|
|
|
|
|
|
|
self.logger.info("IGNORED: Copy message UID %s %s:%s -> %s"% (
|
|
|
|
uid, src.repository, src, destfolder.repository))
|
|
|
|
|
2011-09-29 16:51:48 +02:00
|
|
|
def copyingmessage(self, uid, num, num_to_copy, src, destfolder):
|
2016-06-28 19:02:58 +02:00
|
|
|
"""Output a log line stating which message we copy."""
|
|
|
|
|
2018-02-24 00:29:44 +01:00
|
|
|
self.logger.info("Copy message UID %s (%d/%d) %s:%s -> %s:%s"% (
|
2011-10-26 16:47:21 +02:00
|
|
|
uid, num, num_to_copy, src.repository, src,
|
2018-02-24 00:29:44 +01:00
|
|
|
destfolder.repository, destfolder))
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def deletingmessages(self, uidlist, destlist):
|
|
|
|
ds = self.folderlist(destlist)
|
2012-02-17 11:43:41 +01:00
|
|
|
prefix = "[DRYRUN] " if self.dryrun else ""
|
2012-05-18 11:31:08 +02:00
|
|
|
self.info("{0}Deleting {1} messages ({2}) in {3}".format(
|
2015-01-14 22:58:25 +01:00
|
|
|
prefix, len(uidlist),
|
|
|
|
offlineimap.imaputil.uid_sequence(uidlist), ds))
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def addingflags(self, uidlist, flags, dest):
|
|
|
|
self.logger.info("Adding flag %s to %d messages on %s" % (
|
|
|
|
", ".join(flags), len(uidlist), dest))
|
|
|
|
|
|
|
|
def deletingflags(self, uidlist, flags, dest):
|
|
|
|
self.logger.info("Deleting flag %s from %d messages on %s" % (
|
|
|
|
", ".join(flags), len(uidlist), dest))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2012-10-16 20:20:35 +02:00
|
|
|
def addinglabels(self, uidlist, label, dest):
|
|
|
|
self.logger.info("Adding label %s to %d messages on %s" % (
|
|
|
|
label, len(uidlist), dest))
|
|
|
|
|
|
|
|
def deletinglabels(self, uidlist, label, dest):
|
|
|
|
self.logger.info("Deleting label %s from %d messages on %s" % (
|
|
|
|
label, len(uidlist), dest))
|
|
|
|
|
|
|
|
def settinglabels(self, uid, num, num_to_set, labels, dest):
|
|
|
|
self.logger.info("Setting labels to message %d on %s (%d of %d): %s" % (
|
|
|
|
uid, dest, num, num_to_set, ", ".join(labels)))
|
|
|
|
|
|
|
|
def collectingdata(self, uidlist, source):
|
2015-01-08 17:13:33 +01:00
|
|
|
if uidlist:
|
|
|
|
self.logger.info("Collecting data from %d messages on %s"% (
|
2012-10-16 20:20:35 +02:00
|
|
|
len(uidlist), source))
|
2015-01-08 17:13:33 +01:00
|
|
|
else:
|
|
|
|
self.logger.info("Collecting data from messages on %s"% source)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2011-06-30 15:18:03 +02:00
|
|
|
def serverdiagnostics(self, repository, type):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Connect to repository and output useful information for debugging."""
|
|
|
|
|
2011-06-30 15:18:03 +02:00
|
|
|
conn = None
|
|
|
|
self._msg("%s repository '%s': type '%s'" % (type, repository.name,
|
|
|
|
self.getnicename(repository)))
|
|
|
|
try:
|
|
|
|
if hasattr(repository, 'gethost'): # IMAP
|
2015-01-14 22:58:25 +01:00
|
|
|
self._msg("Host: %s Port: %s SSL: %s"% (repository.gethost(),
|
|
|
|
repository.getport(), repository.getssl()))
|
2011-06-30 15:18:03 +02:00
|
|
|
try:
|
|
|
|
conn = repository.imapserver.acquireconnection()
|
2012-01-07 20:28:58 +01:00
|
|
|
except OfflineImapError as e:
|
2011-06-30 15:18:03 +02:00
|
|
|
self._msg("Failed to connect. Reason %s" % e)
|
|
|
|
else:
|
|
|
|
if 'ID' in conn.capabilities:
|
|
|
|
self._msg("Server supports ID extension.")
|
|
|
|
#TODO: Debug and make below working, it hangs Gmail
|
|
|
|
#res_type, response = conn.id((
|
|
|
|
# 'name', offlineimap.__productname__,
|
2016-03-05 00:58:59 +01:00
|
|
|
# 'version', offlineimap.__version__))
|
2011-06-30 15:18:03 +02:00
|
|
|
#self._msg("Server ID: %s %s" % (res_type, response[0]))
|
|
|
|
self._msg("Server welcome string: %s" % str(conn.welcome))
|
|
|
|
self._msg("Server capabilities: %s\n" % str(conn.capabilities))
|
|
|
|
repository.imapserver.releaseconnection(conn)
|
|
|
|
if type != 'Status':
|
|
|
|
folderfilter = repository.getconf('folderfilter', None)
|
|
|
|
if folderfilter:
|
|
|
|
self._msg("folderfilter= %s\n" % folderfilter)
|
|
|
|
folderincludes = repository.getconf('folderincludes', None)
|
|
|
|
if folderincludes:
|
|
|
|
self._msg("folderincludes= %s\n" % folderincludes)
|
|
|
|
nametrans = repository.getconf('nametrans', None)
|
|
|
|
if nametrans:
|
|
|
|
self._msg("nametrans= %s\n" % nametrans)
|
|
|
|
|
|
|
|
folders = repository.getfolders()
|
2015-01-14 22:58:25 +01:00
|
|
|
foldernames = [(f.name, f.getvisiblename(), f.sync_this)
|
|
|
|
for f in folders]
|
2011-06-30 15:18:03 +02:00
|
|
|
folders = []
|
2012-01-07 21:17:36 +01:00
|
|
|
for name, visiblename, sync_this in foldernames:
|
|
|
|
syncstr = "" if sync_this else " (disabled)"
|
|
|
|
if name == visiblename: folders.append("%s%s" % (name,
|
|
|
|
syncstr))
|
|
|
|
else: folders.append("%s -> %s%s" % (name,
|
|
|
|
visiblename, syncstr))
|
|
|
|
self._msg("Folderlist:\n %s\n" % "\n ".join(folders))
|
2011-06-30 15:18:03 +02:00
|
|
|
finally:
|
|
|
|
if conn: #release any existing IMAP connection
|
|
|
|
repository.imapserver.close()
|
|
|
|
|
2011-09-16 11:44:50 +02:00
|
|
|
def savemessage(self, debugtype, uid, flags, folder):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Output a log line stating that we save a msg."""
|
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
self.debug(debugtype, "Write mail '%s:%d' with flags %s"%
|
|
|
|
(folder, uid, repr(flags)))
|
2011-09-16 11:44:50 +02:00
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
################################################## Threads
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def getThreadDebugLog(self, thread):
|
2012-02-05 13:40:06 +01:00
|
|
|
if thread in self.debugmessages:
|
2002-10-17 01:27:27 +02:00
|
|
|
message = "\nLast %d debug messages logged for %s prior to exception:\n"\
|
2011-10-26 16:47:21 +02:00
|
|
|
% (len(self.debugmessages[thread]), thread.getName())
|
|
|
|
message += "\n".join(self.debugmessages[thread])
|
2002-10-17 01:27:27 +02:00
|
|
|
else:
|
2015-01-14 22:58:25 +01:00
|
|
|
message = "\nNo debug messages were logged for %s."% \
|
|
|
|
thread.getName()
|
2002-10-17 01:27:27 +02:00
|
|
|
return message
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def delThreadDebugLog(self, thread):
|
|
|
|
if thread in self.debugmessages:
|
|
|
|
del self.debugmessages[thread]
|
2002-10-17 01:27:27 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def getThreadExceptionString(self, thread):
|
2015-01-14 22:58:25 +01:00
|
|
|
message = "Thread '%s' terminated with exception:\n%s"% \
|
|
|
|
(thread.getName(), thread.exit_stacktrace)
|
|
|
|
message += "\n" + self.getThreadDebugLog(thread)
|
2002-10-17 01:27:27 +02:00
|
|
|
return message
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def threadException(self, thread):
|
2002-07-05 04:34:39 +02:00
|
|
|
"""Called when a thread has terminated with an exception.
|
|
|
|
The argument is the ExitNotifyThread that has so terminated."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.warn(self.getThreadExceptionString(thread))
|
|
|
|
self.delThreadDebugLog(thread)
|
|
|
|
self.terminate(100)
|
2002-07-05 04:34:39 +02:00
|
|
|
|
2011-08-11 12:22:34 +02:00
|
|
|
def terminate(self, exitstatus = 0, errortitle = None, errormsg = None):
|
2002-07-05 04:34:39 +02:00
|
|
|
"""Called to terminate the application."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-08-11 12:22:34 +02:00
|
|
|
#print any exceptions that have occurred over the run
|
|
|
|
if not self.exc_queue.empty():
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn("ERROR: Exceptions occurred during the run!")
|
2015-05-05 15:34:58 +02:00
|
|
|
if exitstatus == 0:
|
|
|
|
exitstatus = 1
|
2011-08-11 12:22:34 +02:00
|
|
|
while not self.exc_queue.empty():
|
|
|
|
msg, exc, exc_traceback = self.exc_queue.get()
|
|
|
|
if msg:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn("ERROR: %s\n %s"% (msg, exc))
|
2006-12-01 11:54:12 +01:00
|
|
|
else:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn("ERROR: %s"% (exc))
|
2011-08-11 12:22:34 +02:00
|
|
|
if exc_traceback:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn("\nTraceback:\n%s"% "".join(
|
2011-08-11 12:22:34 +02:00
|
|
|
traceback.format_tb(exc_traceback)))
|
|
|
|
|
|
|
|
if errormsg and errortitle:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn('ERROR: %s\n\n%s\n'% (errortitle, errormsg))
|
2011-08-11 12:22:34 +02:00
|
|
|
elif errormsg:
|
2015-01-14 22:58:25 +01:00
|
|
|
self.warn('%s\n'% errormsg)
|
2015-05-05 19:35:37 +02:00
|
|
|
if self.uidval_problem:
|
|
|
|
self.warn('At least one folder skipped due to UID validity problem')
|
|
|
|
if exitstatus == 0:
|
|
|
|
exitstatus = 2
|
2002-07-05 04:34:39 +02:00
|
|
|
sys.exit(exitstatus)
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def threadExited(self, thread):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Called when a thread has exited normally.
|
|
|
|
|
|
|
|
Many UIs will just ignore this."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.delThreadDebugLog(thread)
|
|
|
|
self.unregisterthread(thread)
|
2002-07-05 04:34:39 +02:00
|
|
|
|
2008-10-01 07:03:04 +02:00
|
|
|
################################################## Hooks
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def callhook(self, msg):
|
2011-09-15 13:56:04 +02:00
|
|
|
if self.dryrun:
|
2012-05-18 11:31:08 +02:00
|
|
|
self.info("[DRYRUN] {0}".format(msg))
|
2011-09-15 13:56:04 +02:00
|
|
|
else:
|
|
|
|
self.info(msg)
|
2008-10-01 07:03:04 +02:00
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
################################################## Other
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def sleep(self, sleepsecs, account):
|
2002-07-03 08:50:31 +02:00
|
|
|
"""This function does not actually output anything, but handles
|
|
|
|
the overall sleep, dealing with updates as necessary. It will,
|
|
|
|
however, call sleeping() which DOES output something.
|
|
|
|
|
2011-05-07 17:40:32 +02:00
|
|
|
:returns: 0/False if timeout expired, 1/2/True if there is a
|
|
|
|
request to cancel the timer.
|
|
|
|
"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-05-07 17:40:32 +02:00
|
|
|
abortsleep = False
|
2002-07-03 08:50:31 +02:00
|
|
|
while sleepsecs > 0 and not abortsleep:
|
2011-05-07 17:40:32 +02:00
|
|
|
if account.get_abort_event():
|
2015-01-15 17:40:27 +01:00
|
|
|
abortsleep = True
|
2011-05-07 17:40:32 +02:00
|
|
|
else:
|
2011-10-26 16:47:21 +02:00
|
|
|
abortsleep = self.sleeping(10, sleepsecs)
|
|
|
|
sleepsecs -= 10
|
|
|
|
self.sleeping(0, 0) # Done sleeping.
|
2002-07-03 08:50:31 +02:00
|
|
|
return abortsleep
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def sleeping(self, sleepsecs, remainingsecs):
|
2010-12-05 15:35:01 +01:00
|
|
|
"""Sleep for sleepsecs, display remainingsecs to go.
|
2002-06-21 09:25:24 +02:00
|
|
|
|
2010-12-05 15:35:01 +01:00
|
|
|
Does nothing if sleepsecs <= 0.
|
2011-09-29 17:58:07 +02:00
|
|
|
Display a message on the screen if we pass a full minute.
|
2010-12-05 15:35:01 +01:00
|
|
|
|
|
|
|
This implementation in UIBase does not support this, but some
|
|
|
|
implementations return 0 for successful sleep and 1 for an
|
|
|
|
'abort', ie a request to sync immediately.
|
|
|
|
"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
if sleepsecs > 0:
|
2011-09-29 17:58:07 +02:00
|
|
|
if remainingsecs//60 != (remainingsecs-sleepsecs)//60:
|
2012-01-10 07:19:22 +01:00
|
|
|
self.logger.debug("Next refresh in %.1f minutes" % (
|
2011-10-26 16:47:21 +02:00
|
|
|
remainingsecs/60.0))
|
2002-06-21 09:25:24 +02:00
|
|
|
time.sleep(sleepsecs)
|
|
|
|
return 0
|