Make sleep abort request working again for Curses UI

1) Rework the sleep abort request to set the skipsleep configuration
   setting that the sleep() code checks.
2) Only output 15 rather than 50 debug messages on abort...

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-11-03 14:21:25 +01:00
parent f4a32bafd6
commit f8d5f1890c
2 changed files with 35 additions and 30 deletions

View File

@ -113,8 +113,10 @@ class CursesAccountFrame:
""" """
def __init__(self, ui, account): def __init__(self, ui, account):
"""
:param account: An Account() or None (for eg SyncrunnerThread)"""
self.children = [] self.children = []
self.account = account self.account = account if account else '*Control'
self.ui = ui self.ui = ui
self.window = None self.window = None
"""Curses window associated with this acc""" """Curses window associated with this acc"""
@ -155,18 +157,21 @@ class CursesAccountFrame:
self.children.append(tf) self.children.append(tf)
return tf return tf
def startsleep(self, sleepsecs):
self.sleeping_abort = 0
def sleeping(self, sleepsecs, remainingsecs): def sleeping(self, sleepsecs, remainingsecs):
# show how long we are going to sleep and sleep # show how long we are going to sleep and sleep
self.drawleadstr(remainingsecs) self.drawleadstr(remainingsecs)
self.ui.exec_locked(self.window.refresh) self.ui.exec_locked(self.window.refresh)
time.sleep(sleepsecs) time.sleep(sleepsecs)
return 0 return self.account.abort_signal.is_set()
def syncnow(s): def syncnow(self):
s.sleeping_abort = 1 """Request that we stop sleeping asap and continue to sync"""
# if this belongs to an Account (and not *Control), set the
# skipsleep pref
if isinstance(self.account, offlineimap.accounts.Account):
self.ui.info("Requested synchronization for acc: %s" % self.account)
self.account.config.set('Account %s' % self.account.name,
'skipsleep', '1')
class CursesThreadFrame: class CursesThreadFrame:
""" """
@ -442,37 +447,37 @@ class Blinkenlights(UIBase, CursesUtil):
super(Blinkenlights, self).warn(msg) super(Blinkenlights, self).warn(msg)
def threadExited(self, thread): def threadExited(self, thread):
acc_name = self.getthreadaccount(thread) acc = self.getthreadaccount(thread)
with self.tframe_lock: with self.tframe_lock:
if thread in self.threadframes[acc_name]: if thread in self.threadframes[acc]:
tf = self.threadframes[acc_name][thread] tf = self.threadframes[acc][thread]
tf.setcolor('black') tf.setcolor('black')
self.availablethreadframes[acc_name].append(tf) self.availablethreadframes[acc].append(tf)
del self.threadframes[acc_name][thread] del self.threadframes[acc][thread]
super(Blinkenlights, self).threadExited(thread) super(Blinkenlights, self).threadExited(thread)
def gettf(self): def gettf(self):
"""Return the ThreadFrame() of the current thread""" """Return the ThreadFrame() of the current thread"""
cur_thread = currentThread() cur_thread = currentThread()
acc_name = self.getthreadaccount() acc = self.getthreadaccount() #Account() or None
with self.tframe_lock: with self.tframe_lock:
# Ideally we already have self.threadframes[accountname][thread] # Ideally we already have self.threadframes[accountname][thread]
try: try:
if cur_thread in self.threadframes[acc_name]: if cur_thread in self.threadframes[acc]:
return self.threadframes[acc_name][cur_thread] return self.threadframes[acc][cur_thread]
except KeyError: except KeyError:
# Ensure threadframes already has an account dict # Ensure threadframes already has an account dict
self.threadframes[acc_name] = {} self.threadframes[acc] = {}
self.availablethreadframes[acc_name] = deque() self.availablethreadframes[acc] = deque()
# If available, return a ThreadFrame() # If available, return a ThreadFrame()
if len(self.availablethreadframes[acc_name]): if len(self.availablethreadframes[acc]):
tf = self.availablethreadframes[acc_name].popleft() tf = self.availablethreadframes[acc].popleft()
tf.std_color() tf.std_color()
else: else:
tf = self.getaccountframe(acc_name).get_new_tframe() tf = self.getaccountframe(acc).get_new_tframe()
self.threadframes[acc_name][cur_thread] = tf self.threadframes[acc][cur_thread] = tf
return tf return tf
def on_keypressed(self, key): def on_keypressed(self, key):
@ -501,14 +506,14 @@ class Blinkenlights(UIBase, CursesUtil):
def sleep(self, sleepsecs, account): def sleep(self, sleepsecs, account):
self.gettf().setcolor('red') self.gettf().setcolor('red')
self.info("Next sync in %d:%02d" % (sleepsecs / 60, sleepsecs % 60)) self.info("Next sync in %d:%02d" % (sleepsecs / 60, sleepsecs % 60))
self.getaccountframe().startsleep(sleepsecs)
return super(Blinkenlights, self).sleep(sleepsecs, account) return super(Blinkenlights, self).sleep(sleepsecs, account)
def sleeping(self, sleepsecs, remainingsecs): def sleeping(self, sleepsecs, remainingsecs):
if not sleepsecs: if not sleepsecs:
# reset color to default if we are done sleeping. # reset color to default if we are done sleeping.
self.gettf().setcolor('white') self.gettf().setcolor('white')
return self.getaccountframe().sleeping(sleepsecs, remainingsecs) accframe = self.getaccountframe(self.getthreadaccount())
return accframe.sleeping(sleepsecs, remainingsecs)
def resizeterm(self): def resizeterm(self):
"""Resize the current windows""" """Resize the current windows"""
@ -593,10 +598,10 @@ class Blinkenlights(UIBase, CursesUtil):
self.logwin.addstr("\n" + line, color) self.logwin.addstr("\n" + line, color)
self.logwin.noutrefresh() self.logwin.noutrefresh()
def getaccountframe(self, acc_name = None): def getaccountframe(self, acc_name):
"""Return an AccountFrame()""" """Return an AccountFrame() corresponding to acc_name
if acc_name == None:
acc_name = self.getthreadaccount() Note that the *control thread uses acc_name `None`."""
with self.aflock: with self.aflock:
# 1) Return existing or 2) create a new CursesAccountFrame. # 1) Return existing or 2) create a new CursesAccountFrame.
if acc_name in self.accframes: return self.accframes[acc_name] if acc_name in self.accframes: return self.accframes[acc_name]
@ -617,7 +622,7 @@ class Blinkenlights(UIBase, CursesUtil):
# finally call parent terminate which prints out exceptions etc # finally call parent terminate which prints out exceptions etc
super(Blinkenlights, self).terminate(*args, **kwargs) super(Blinkenlights, self).terminate(*args, **kwargs)
def threadException(s, thread): def threadException(self, thread):
#self._log_con_handler.stop() #self._log_con_handler.stop()
UIBase.threadException(s, thread) UIBase.threadException(self, thread)

View File

@ -48,7 +48,7 @@ class UIBase(object):
"""list of debugtypes we are supposed to log""" """list of debugtypes we are supposed to log"""
self.debugmessages = {} self.debugmessages = {}
"""debugmessages in a deque(v) per thread(k)""" """debugmessages in a deque(v) per thread(k)"""
self.debugmsglen = 50 self.debugmsglen = 15
self.threadaccounts = {} self.threadaccounts = {}
"""dict linking active threads (k) to account names (v)""" """dict linking active threads (k) to account names (v)"""
self.acct_startimes = {} self.acct_startimes = {}