Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
cleaning up the code related to #243:
Browse files Browse the repository at this point in the history
follow threading.Timer naming convention, cancel is the method I had called abort or stop.
also added a dots printing timer, so that you know that the thing is still busy.
a more generic cancel_threads to cancel all threads in the view.
  • Loading branch information
mfrasca committed Dec 29, 2015
1 parent 393b154 commit 7df7950
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bauble/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def __init__(self, filename, parent=None, root_widget_name=None):
self.connect(window, 'response', self.on_dialog_response)
self.box = set() # the top level, meant for warnings.

def abort_threads(self):
def cancel_threads(self):
pass

def update(self):
Expand Down
2 changes: 1 addition & 1 deletion bauble/pluginmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def __init__(self, *args, **kwargs):
"""
super(View, self).__init__(*args, **kwargs)

def abort_threads(self):
def cancel_threads(self):
pass

def update(self):
Expand Down
2 changes: 1 addition & 1 deletion bauble/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def set_view(self, view=None):
kid.set_visible(True)
else:
kid.set_visible(False)
kid.abort_threads()
kid.cancel_threads()
if must_add_this_view:
view_box.pack_start(view, True, True, 0)
view.show_all()
Expand Down
64 changes: 49 additions & 15 deletions bauble/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,42 @@ def update(self, row):
self.dynamic_box.show_all()


class AddOneDot(threading.Thread):

@staticmethod
def callback(dotno):
statusbar = bauble.gui.widgets.statusbar
sbcontext_id = statusbar.get_context_id('searchview.nresults')
statusbar.pop(sbcontext_id)
statusbar.push(sbcontext_id, _('counting results') + '.' * dotno)

def __init__(self, group=None, verbose=None, **kwargs):
super(AddOneDot, self).__init__(
group=group, target=None, name=None, verbose=verbose)
self.__stopped = threading.Event()
self.dotno = 0

def cancel(self):
self.__stopped.set()

def run(self):
while not self.__stopped.wait(1.0):
self.dotno += 1
gobject.idle_add(self.callback, self.dotno)


class CountResultsTask(threading.Thread):
def __init__(self, klass, ids,
def __init__(self, klass, ids, dots_thread,
group=None, verbose=None, **kwargs):
super(CountResultsTask, self).__init__(
group=group, target=None, name=None, verbose=verbose)
self.klass = klass
self.ids = ids
self.abort = False
self.dots_thread = dots_thread
self.__cancel = False

def cancel(self):
self.__cancel = True

def run(self):
session = db.Session()
Expand All @@ -376,7 +404,7 @@ def run(self):
d = {}
for ndx in self.ids:
item = session.query(klass).filter(klass.id == ndx).one()
if self.abort: # check whether caller asks to abort
if self.__cancel: # check whether caller asks to cancel
break
for k, v in item.top_level_count().items():
if isinstance(v, set):
Expand All @@ -390,7 +418,7 @@ def run(self):
if isinstance(v, set):
v = len(v)
result.append("%s: %d" % (k, v))
if self.abort: # check whether caller asks to abort
if self.__cancel: # check whether caller asks to cancel
break
value = _("top level count: %s") % (", ".join(result))
if bauble.gui:
Expand All @@ -399,7 +427,8 @@ def callback(text):
sbcontext_id = statusbar.get_context_id('searchview.nresults')
statusbar.pop(sbcontext_id)
statusbar.push(sbcontext_id, text)
if not self.abort: # check whether caller asks to abort
if not self.__cancel: # check whether caller asks to cancel
self.dots_thread.cancel()
gobject.idle_add(callback, value)
else:
logger.debug("showing text %s", value)
Expand Down Expand Up @@ -504,7 +533,7 @@ def __init__(self):
# be cleared when we do a new search
self.session = db.Session()
self.add_notes_page_to_bottom_notebook()
self.still_counting = None
self.running_threads = []

def add_notes_page_to_bottom_notebook(self):
'''add notebook page for notes
Expand Down Expand Up @@ -719,10 +748,10 @@ def _impl(*args):
logger.warning(
'Could not parse accelerator: %s' % (action.accelerator))

def abort_threads(self):
if self.still_counting:
self.still_counting.abort = True
self.still_counting = None
def cancel_threads(self):
for k in self.running_threads:
k.cancel()
self.running_threads = []

nresults_statusbar_context = 'searchview.nresults'

Expand All @@ -741,7 +770,7 @@ def search(self, text):
# even have session as a class attribute
self.session = db.Session()
# stop whatever it might still be doing
self.abort_threads()
self.cancel_threads()
bold = '<b>%s</b>'
results = []
try:
Expand Down Expand Up @@ -798,11 +827,16 @@ def search(self, text):
return
else:
statusbar.pop(sbcontext_id)
statusbar.push(sbcontext_id, _('counting results...'))
statusbar.push(sbcontext_id, _('counting results'))
if len(set(item.__class__ for item in results)) == 1:
self.still_counting = CountResultsTask(
results[0].__class__, [i.id for i in results])
self.still_counting.start()
dots_thread = AddOneDot()
self.running_threads.append(dots_thread)
dots_thread.start()
counting = CountResultsTask(
results[0].__class__, [i.id for i in results],
dots_thread)
self.running_threads.append(counting)
counting.start()
else:
statusbar.push(sbcontext_id,
_('size of non homogeneous result: %s') %
Expand Down

0 comments on commit 7df7950

Please sign in to comment.