4
from gettext import gettext as _
5
from gettext import ngettext
7
class Terminated (Exception):
8
def __init__ (self, value):
11
return repr(self.value)
13
class SuspendableThread (threading.Thread):
14
"""A SuspendableThread. We take a runnerClass which must have a
15
run, suspend, terminate, and resume method. We then launch our thread
16
and provide methods by the same name that interface with our runnerClass.
17
Before running the run() method, we call pre_hooks. Afterward we call any
18
post_hooks. (Pre and post hooks get called with this instance as their only
20
def __init__ (self, runnerClass, name=None, pre_hooks=[], post_hooks=[],
22
self.display_errors=display_errors
24
self.pre_hooks=pre_hooks
25
self.post_hooks=post_hooks
27
self.completed = False
28
debug("SuspendableThread starting thread.",2)
29
self.initialize_thread()
31
def initialize_thread (self):
32
threading.Thread.__init__(self, target=self.target_func, name=name)
34
def target_func (self):
35
self.run_hooks(self.pre_hooks)
37
debug('SuspendableThread Running %s'%self.c,3)
40
from gtk_extras import dialog_extras
41
dialog_extras.show_message(
42
label=_("%s stopped."%self.name.title()),
43
sublabel=_("%s was interrupted by user request."%self.name.title())
46
if self.display_errors:
48
from gtk_extras import dialog_extras
49
dialog_extras.show_traceback(
50
label=_("%s interrupted")%self.name.title(),
51
sublabel=_("There was an error during %s.")%self.name,
54
self.run_hooks(self.post_hooks)
58
self.run_hooks(self.post_hooks)
60
def run_hooks (self, hooks):
61
"""We hand all hooks ourselves as an argument"""
63
debug('Running %s'%h,3)
67
debug('suspending thread',0)
71
debug('resuming thread',0)
75
debug('terminating thread',0)
78
def _threads_enter (self):
81
def _threads_leave (self):
84
class SuspendableDeletions:
85
def __init__ (self, rg, recs):
86
self.suspended = False
87
self.terminated = False
89
print 'SuspendableDeletions handed ',recs
92
def check_for_sleep (self):
94
raise Terminated("Deletion Terminated!")
97
raise "Deletion Terminated!"
101
debug('running GourmetThreads.py',0)
102
rtot = len(self.recs)
105
self.check_for_sleep()
106
#r = self.rg.rd.get_rec(i)
107
self.rg.set_progress_thr(float(n)/float(rtot),
108
_("Deleting recipes from database... (%s of %s deleted)"%(n,rtot))
110
self.rg.delete_rec(r)
112
msg = ngettext('Deleted %s recipe','Deleted %s recipes',
114
self.rg.reset_prog_thr(message=msg)
115
self.rg.doing_multiple_deletions=False
117
def suspend (self): self.suspended = True
119
def terminate (self): self.terminated = True
121
def resume (self): self.suspended = False
124
return threading.Lock()
127
print 'threads_enter'
131
print 'threads_leave'
134
def gtk_threads_init ():
139
"""This is for FauxThreads to use to update the GUI periodically"""