[82ddff]: plugins / macro.py Maximize Restore History

Download this file

macro.py    427 lines (349 with data), 12.0 kB

  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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import os
import sys
import todo
import imp
import time
import random
import mylistmix
import keydialog
__main__ = _pype
import wx
macropath = os.path.join(_pype.runpath, 'macros')
columns = (
(0, "Macro Name", 150, 0),
(1, "Hotkey", 50, 0),
)
class macroList(todo.vTodo, mylistmix.ListSelect):
def Refresh(self):
self.SetItemCount(0)
try:
wx.Yield()
except:
pass
self.SetItemCount(len(self.data))
todo.vTodo.Refresh(self)
def OnGetItemText(self, item, col):
if col == 0:
return "%s" % (self.data[item][col],)
elif col == 1:
x = self.parent.d.get(self.data[item][1], (None, None))[1]
if hasattr(x, "hotkeydisplay"):
return "%s"%(x.hotkeydisplay,)
elif hasattr(x, "hotkeyaccept"):
return "%s"%(x.hotkeyaccept,)
return ""
def OnGetItemAttr(self, item):
if not self.parent.d[self.data[item][1]][1]:
return red
return None
template = '''
creation_date = %r
name = %r
hotkeydisplay = ""
hotkeyaccept = ""
%s
'''
hlp = '''\
#Copy and paste the following code into the
#source of the macro that you would like to
#have this hotkey bound to.
%(h)s
#for example:
#...
%(d)s
#...
#def macro(self):
# ...
'''
red = wx.ListItemAttr()
red.SetTextColour(wx.Colour(200, 0, 0))
timeout = None
def macro_timeout(a,b,c):
del a, b, c
if time.time() > timeout:
raise KeyboardInterrupt, "Macro took more than 5 seconds!"
try:
wx.Yield()
except:
pass
return macro_timeout
def start_macro():
global timeout
timeout = time.time() + 5
sys.settrace(macro_timeout)
def end_macro():
sys.settrace(None)
def rpartition(str, sep):
if not sep in str:
return str, ''
i = str.rfind(sep)
return str[:i], str[i+len(sep):]
nostart = '''\
Couldn't start macro because some other long-term action is
already being performed within PyPE. Please wait a few
moments until it is complete, and try again.'''
def load_module(name, fname):
x = imp.new_module(name)
x.__file__ = fname
x.__name__ = name
x.__builtins__ = __builtins__
execfile(fname, x.__dict__)
return x
def Button1(parent, id, which, help, extra):
bitmap = wx.ArtProvider_GetBitmap(which, wx.ART_TOOLBAR, (16,16))
z = wx.BitmapButton(parent, id, bitmap, (16,16), (26,26))
z.SetToolTipString(help)
return z
def Button2(parent, id, which, help, extra):
z = wx.Button(parent, id, extra)
z.SetToolTipString(help)
return z
bmButton = Button2
class macroPanel(wx.Panel):
def __init__(self, parent, root):
wx.Panel.__init__(self, parent)
self.root = root
self.rec1 = bmButton(self, -1, wx.ART_CDROM, "Start Recording", "Record")
self.rec2 = bmButton(self, -1, wx.ART_ERROR, "Stop Recording", "Stop!"); self.rec2.Hide()
self.edit = bmButton(self, -1, wx.ART_FOLDER_OPEN, "Edit Macro", "Edit")
self.empty = bmButton(self, -1, wx.ART_NEW, "New Empty Macro", "New")
self.hotkey = bmButton(self, -1, wx.ART_ADD_BOOKMARK, "Create Hotkey", "Hotkey")
self.play = bmButton(self, -1, wx.ART_REDO, "Run Macro", "Run")
self.de1 = bmButton(self, -1, wx.ART_DELETE, "Delete Macro", "Delete")
sz = wx.BoxSizer(wx.HORIZONTAL)
a = (1, wx.EXPAND|wx.ALL, 2)
sz.Add(self.rec1, *a)
sz.Add(self.rec2, *a)
sz.Add(self.edit, *a)
sz.Add(self.empty, *a)
sz.Add(self.hotkey, *a)
sz.Add(self.play, *a)
sz.Add(self.de1, *a)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(sz, 0, wx.EXPAND)
self.macros = macroList(self, columns)
sizer.Add(self.macros, 1, wx.EXPAND|wx.ALL, 3)
self.SetSizer(sizer)
self.m = []
self.d = {}
self.hotkeys = {}
self.macros.setData(self.m, copy=0)
self.t = wx.Timer(self)
self.accelerator = ''
self.acceleratork = ''
self.macros.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
self.rec1.Bind(wx.EVT_BUTTON, self.OnRec)
self.rec2.Bind(wx.EVT_BUTTON, self.OnRec)
self.edit.Bind(wx.EVT_BUTTON, self.OnEdit)
self.empty.Bind(wx.EVT_BUTTON, self.OnEmpty)
self.hotkey.Bind(wx.EVT_BUTTON, self.GetHotkey)
self.play.Bind(wx.EVT_BUTTON, self.OnPlay)
self.de1.Bind(wx.EVT_BUTTON, self.OnDel)
## self.t.Bind(wx.EVT_TIMER, self.CheckMacros)
self.Bind(wx.EVT_TIMER, self.CheckMacros)
self.t.Start(1000, wx.TIMER_CONTINUOUS)
wx.CallAfter(self.CheckMacros, None)
def RunMacro(self, hotkey):
if hotkey not in self.hotkeys:
return 0
which = self.hotkeys[hotkey]
self.macros.SelectI(which)
wx.CallAfter(self.OnPlay, None)
return 1
def update_button(self, stc=None):
if not stc:
stc = self.root.getNumWin(None)[1]
if not hasattr(stc, 'lines'):
return
if stc.recording:
self.rec1.Hide()
self.rec2.Show()
self.Layout()
else:
self.rec1.Show()
self.rec2.Hide()
self.Layout()
def OnItemActivated(self, e):
dc = __main__.macro_doubleclick
if dc == 0:
return
elif dc == 1:
self.OnEdit(e)
elif dc == 2:
self.OnPlay(e)
def CheckMacros(self, e):
try:
a = os.listdir(macropath)
except:
return
changed = 0
## self.m = [] #[(name, file), (name, file), ...]
## self.d = {} #file : (mtime, module)
notseen = dict.fromkeys(self.d)
for name in a:
if not (name.endswith('.py') or name.endswith('.pyw')):
if name.endswith('.pyc') or name.endswith('.pyo'):
try:
os.remove(os.path.join(macropath, name))
except:
pass
continue
file = os.path.join(macropath, name)
name, ext = rpartition(name, '.')
try:
mtime = os.stat(file).st_mtime
except:
continue
_ = notseen.pop(file, None)
if file in self.d and self.d[file][0] == mtime:
#the file hasn't changed
continue
module = None
try:
module = load_module('_'+name, file)
except:
#module load failure...
#should probably report the exception...
pass
if hasattr(module, 'name'):
name = module.name
_ = notseen.pop(file, None)
if file not in self.d:
self.m.append((name, file))
self.d[file] = (mtime, module)
## print "new", file
else:
for i, f in enumerate(self.m):
if f[1] == file:
self.m[i] = (name, file)
break
self.d[file] = (mtime, module)
## print "updated", file
changed = 1
for f in notseen:
i = 0
while i < len(self.m):
j = self.m[i]
if j[1] == f:
del self.m[i]
del self.d[j[1]]
## print "deleted", f
changed = 1
break
else:
i += 1
else:
print "what?", f
#not in either list?
pass
if changed:
self.hotkeys.clear()
for i, (n,f) in enumerate(self.m):
x = self.d[f][1]
hk = None
if hasattr(x, "hotkeyaccept"):
hk = "%s"%(x.hotkeyaccept,)
elif hasattr(x, "hotkeydisplay"):
hk = "%s"%(x.hotkeydisplay,)
if hk and hk not in self.hotkeys:
self.hotkeys[hk] = i
self.macros.Refresh()
def OnRec(self, e):
num, stc = self.root.getNumWin(None)
if not hasattr(stc, 'lines'):
return
stc.MacroToggle(None)
self.update_button()
if not stc.recording:
if stc.macro:
source = stc._macro_to_source()
self.OnEmpty(e, source)
def OnEdit(self, e):
x = self.macros.GetFirstSelected()
if x == -1:
return
self.root.OnDrop([self.m[x][1]])
def OnEmpty(self, e, source=None):
if source == None:
source = 'def macro(self):\n pass'
fname = 'macro_%i_%i.py'%(int(time.time()), random.randrange(65536))
ctime = time.asctime()
name = 'macro: %s'%ctime
open(os.path.join(macropath, fname), 'w').write(template%(ctime, name, source))
self.CheckMacros(None)
def OnPlay(self, e):
## if self.play.GetLabel() == "Stop\nMacro":
## global timeout
## timeout = None
## return
x = self.macros.GetFirstSelected()
if x == -1:
return
module = self.d[self.m[x][1]][1]
if not module:
return
if not hasattr(module, 'macro'):
return
stc = self.root.getNumWin(None)[1]
if stc.recording:
self.root.dialog("You must stop recording a macro\nin order to play a macro.", 'Sorry')
return
if not hasattr(stc, 'lines'):
return
try:
wx.Yield()
except:
self.root.dialog(nostart, 'Sorry')
return
finished = 0
## self.play.SetLabel("Stop\nMacro")
start_macro()
try:
try:
getattr(module, 'macro')(stc)
except:
end_macro()
finished = 1
self.root.exceptDialog("Failure in macro!")
finally:
if not finished:
end_macro()
def OnDel(self, e):
x = self.macros.GetFirstSelected()
if x == -1:
return
dlg = wx.MessageDialog(self,
('Are you sure you want to delete the\n'
'the macro with name: %s\n'
'with location: %s'
)%self.m[x],
'Are you sure?',
wx.YES_NO | wx.ICON_INFORMATION | wx.NO_DEFAULT
)
yesno = dlg.ShowModal()
dlg.Destroy()
if yesno != wx.ID_YES:
return
try:
os.remove(self.m[x][1])
except:
self.root.exceptDialog("Macro deletion failed")
self.CheckMacros(None)
def GetHotkey(self, e):
self.accelerator = ''
self.acceleratork = ''
dlg = keydialog.GetKeyDialog(self, '', '', '')
dlg.ShowModal()
if not (self.accelerator or self.acceleratork):
return
ha = 'hotkeyaccept = %r'%self.accelerator
hd = 'hotkeydisplay = %r'%self.acceleratork
if self.accelerator == self.acceleratork:
h = ha
d = '#'+h
else:
h = '\n'.join((ha,hd))
d = '\n'.join(('#'+ha,'#'+hd))
dlg = wx.lib.dialogs.ScrolledMessageDialog(self, hlp%locals(), "Your hotkey")
dlg.ShowModal()
dlg.Destroy()