-
Notifications
You must be signed in to change notification settings - Fork 20
/
BackgroundJobMgr.py
180 lines (148 loc) · 5.83 KB
/
BackgroundJobMgr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import re
import datetime
import threading
from queue import Queue
import wx
import wx.lib.mixins.listctrl as listmix
class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self, parent, id = wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, id, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
class BackgroundJobMgr( wx.Dialog ):
timeoutSecs = 10*60
def __init__( self, parent, id = wx.ID_ANY, style=0 ):
super().__init__( parent, id, _('Background Job Mgr'),
style=style|wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL|wx.RESIZE_BORDER )
self.jobList = AutoWidthListCtrl( self, style = wx.LC_REPORT|wx.LC_HRULES )
for col, (k, name, align) in enumerate((
('num', ' ', wx.LIST_FORMAT_RIGHT),
('name', _('Name'), wx.LIST_FORMAT_LEFT),
('start', _('Start'), wx.LIST_FORMAT_RIGHT),
('end', _('End'), wx.LIST_FORMAT_RIGHT),
('dur', _('Dur'), wx.LIST_FORMAT_RIGHT),
('status', _('Status'), wx.LIST_FORMAT_LEFT),
('message', _('Message'), wx.LIST_FORMAT_LEFT),
)):
self.jobList.AppendColumn( name, align )
setattr( self, k + 'Col', col )
btnSizer = self.CreateButtonSizer( wx.OK|wx.CANCEL )
self.Bind( wx.EVT_BUTTON, self.onOK, id=wx.ID_OK )
vs = wx.BoxSizer( wx.VERTICAL )
vs.Add( self.jobList, 1, flag=wx.ALL|wx.EXPAND, border=8 )
if btnSizer:
vs.Add( btnSizer, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, border=8 )
self.jobs = {}
self.q = Queue()
self.q_processor = threading.Thread( target=self.processQ )
self.q_processor.daemon = True
self.q_processor.start()
self.callLater = None
self.SetSizerAndFit( vs )
def refresh( self ):
if not self.IsShownOnScreen():
return
def formatTime( t ):
return t.strftime('%H:%M:%S') if t is not None else ''
def formatDiff( end, start ):
secs = int((end - start).total_seconds())
hh = secs // (60*60)
mm = (secs // 60) % 60
ss = secs % 60
return '{:02d}:{:02d}:{:02d}'.format( hh, mm, ss )
self.jobList.DeleteAllItems()
now = datetime.datetime.now()
delta = datetime.timedelta( seconds=self.timeoutSecs )
self.jobs = {k:v for k,v in self.jobs.items() if now - v.get('end',now) < delta}
colours = (wx.GREEN, wx.YELLOW, wx.RED)
allDone = True
highlight = {}
for row, v in enumerate( sorted( self.jobs.values(), key=lambda v: (v['start'], v.get('end', now)) ) ):
self.jobList.Append( [
'{}.'.format(row+1),
v.get('name',''),
formatTime( v.get('start',None) ),
formatTime( v.get('end',None) ),
formatDiff( v.get('end', now), v.get('start',now) ),
v.get('status',''),
v.get('message',''),
])
if v['code'] != 0 or 'end' in v:
highlight[row] = colours[v['code']]
if 'end' not in v:
allDone = False
for row, colour in highlight.items():
self.jobList.SetItemBackgroundColour( row, colour )
if colour == wx.RED:
self.jobList.SetItemTextColour( row, wx.WHITE )
for i in range(self.jobList.GetColumnCount()):
self.jobList.SetColumnWidth( i, wx.LIST_AUTOSIZE_USEHEADER )
if not allDone:
if not self.callLater:
self.callLater = wx.CallLater( 1000, self.refresh )
elif not self.callLater.IsRunning():
self.callLater.Start( 1000 )
def ShowModal( self, *args, **kwargs ):
wx.CallAfter( self.refresh )
return super().ShowModal( *args, **kwargs )
def processQ( self ):
while True:
msg = self.q.get()
cmd, id = msg.get('cmd', None), msg.get('id', None)
# recognized messages are 'start', 'update' and 'end'
# messages must include a unique 'id' field.
# messages can also include other fields including 'name', 'status' and 'message'
# code can have values of 0, 1 and 2. 0 = OK, 1 = Caution, 2 = Error
if cmd == 'start':
msg['start'] = datetime.datetime.now()
msg['code'] = msg.get('code',0)
self.jobs[id] = msg
elif cmd == 'update':
self.jobs[id].update( msg )
elif cmd == 'end':
msg['end'] = datetime.datetime.now()
self.jobs[id].update( msg )
wx.CallAfter( self.refresh )
def send( self, msg ):
self.q.put( msg )
def onOK( self, event ):
self.Show( False )
def onCopyToClipboard( self, event ):
def csvEscape( s ):
s = s.strip()
if not re.match( '[",\n]', s):
return s
return '"{}"'.format( s.replace('"', '""') ) # double-up quotes to escape.
rowsToCopy = []
for row in range(self.jobList.GetItemCount()):
for col in range(1, self.jobList.GetColumnCount()):
rowsToCopy.append(','.join(csvEscape(self.jobList.GetItemText(row, col))))
if wx.TheClipboard.Open():
wx.TheClipboard.SetData( wx.TextDataObject( '\n'.join(rowsToCopy) ) )
wx.TheClipboard.Close()
if __name__ == '__main__':
import time
import uuid
import random
def doSomething( q ):
id = uuid.uuid4().hex
def put( kwargs ):
kwargs['id'] = id
q.put( kwargs )
put( {'cmd':'start', 'name':'doSomething', 'message':'In progress', 'status':'OK'} )
time.sleep( random.randrange(5, 20) )
if random.randrange(0,2):
put( {'cmd':'update', 'message':'Still Processing', 'status':'More work than expected', 'code':random.randrange(1,3)} )
time.sleep( random.randrange(5, 30) )
if random.randrange(0,2):
put( {'cmd':'update', 'message':'Unexpected Processing', 'status':'That was unexpected!', 'code':2} )
time.sleep( random.randrange(5, 30) )
put( {'cmd':'end', 'message':'Done.', 'status':'Success', 'code':0} )
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(1024,600))
bjm = BackgroundJobMgr(mainWin)
mainWin.Show()
wx.CallLater( 500, bjm.ShowModal )
for i in range(15):
threading.Thread( target=doSomething, args=(bjm.q,) ).start()
app.MainLoop()