32
32
from drMenu import drMenu
34
34
def updatescripts(scriptfile, paths, titles):
37
f = open(scriptfile, 'w')
39
f.write("<path>" + paths[x] + "</path><title>" + titles[x] + "</title>\n")
37
f = open(scriptfile, 'w')
39
f.write("<path>" + paths[x] + "</path><title>" + titles[x] + "</title>\n")
43
43
class drNewShellDialog(wx.Dialog):
45
def __init__(self, parent, title):
46
wx.Dialog.__init__(self, parent, -1, title, wx.Point(50, 50), wx.Size(375, 250), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
50
self.theSizer = wx.FlexGridSizer(6, 3, 5, 10)
52
self.btnSave = wx.Button(self, self.ID_SAVE, "&Save")
56
self.txtTitle = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
57
self.txtCommand = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
58
self.txtArguments = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
59
self.txtDirectory = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
60
self.chkRunInPrompt = wx.CheckBox(self, -1, "")
61
self.chkRunInPrompt.SetValue(True)
63
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
64
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
65
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
67
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
68
self.theSizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
69
self.theSizer.Add(self.txtTitle, 0, wx.SHAPED)
71
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
72
self.theSizer.Add(wx.StaticText(self, -1, "Command:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
73
self.theSizer.Add(self.txtCommand, 0, wx.SHAPED)
75
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
76
self.theSizer.Add(wx.StaticText(self, -1, "Arguments:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
77
self.theSizer.Add(self.txtArguments, 0, wx.SHAPED)
79
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
80
self.theSizer.Add(wx.StaticText(self, -1, "Directory:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
81
self.theSizer.Add(self.txtDirectory, 0, wx.SHAPED)
83
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
84
self.theSizer.Add(wx.StaticText(self, -1, "Run In Prompt:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
85
self.theSizer.Add(self.chkRunInPrompt, 0, wx.SHAPED)
87
self.btnClose = wx.Button(self, 101, "&Close")
88
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
89
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
90
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
91
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
92
self.theSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
93
self.theSizer.Add(self.btnSave, 0, wx.SHAPED | wx.ALIGN_RIGHT)
94
self.txtTitle.SetFocus()
95
self.btnSave.SetDefault()
97
self.SetAutoLayout(True)
98
self.SetSizer(self.theSizer)
102
self.Bind(wx.EVT_BUTTON, self.OnbtnSave, id=self.ID_SAVE)
103
self.Bind(wx.EVT_BUTTON, self.OnbtnClose, id=101)
105
def GetExitStatus(self):
106
return self.exitstatus
108
def GetShellTuple(self):
109
return self.txtTitle.GetValue(), self.txtCommand.GetValue(), self.txtArguments.GetValue(), self.txtDirectory.GetValue(), int(self.chkRunInPrompt.GetValue())
111
def OnbtnClose(self, event):
115
def OnbtnSave(self, event):
119
def SetShellTuple(self, title, command, args, dir, inprompt):
120
self.txtTitle.SetValue(title)
121
self.txtCommand.SetValue(command)
122
self.txtArguments.SetValue(args)
123
self.txtDirectory.SetValue(dir)
124
self.chkRunInPrompt.SetValue(inprompt)
45
def __init__(self, parent, title):
46
wx.Dialog.__init__(self, parent, -1, title, wx.Point(50, 50), wx.Size(375, 250), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
50
self.theSizer = wx.FlexGridSizer(6, 3, 5, 10)
52
self.btnSave = wx.Button(self, self.ID_SAVE, "&Save")
56
self.txtTitle = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
57
self.txtCommand = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
58
self.txtArguments = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
59
self.txtDirectory = wx.TextCtrl(self, -1, "", wx.Point(15, 325), wx.Size(250, -1))
60
self.chkRunInPrompt = wx.CheckBox(self, -1, "")
61
self.chkRunInPrompt.SetValue(True)
63
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
64
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
65
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
67
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
68
self.theSizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
69
self.theSizer.Add(self.txtTitle, 0, wx.SHAPED)
71
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
72
self.theSizer.Add(wx.StaticText(self, -1, "Command:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
73
self.theSizer.Add(self.txtCommand, 0, wx.SHAPED)
75
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
76
self.theSizer.Add(wx.StaticText(self, -1, "Arguments:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
77
self.theSizer.Add(self.txtArguments, 0, wx.SHAPED)
79
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
80
self.theSizer.Add(wx.StaticText(self, -1, "Directory:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
81
self.theSizer.Add(self.txtDirectory, 0, wx.SHAPED)
83
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
84
self.theSizer.Add(wx.StaticText(self, -1, "Run In Prompt:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
85
self.theSizer.Add(self.chkRunInPrompt, 0, wx.SHAPED)
87
self.btnClose = wx.Button(self, 101, "&Close")
88
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
89
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
90
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
91
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
92
self.theSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
93
self.theSizer.Add(self.btnSave, 0, wx.SHAPED | wx.ALIGN_RIGHT)
94
self.txtTitle.SetFocus()
95
self.btnSave.SetDefault()
97
self.SetAutoLayout(True)
98
self.SetSizer(self.theSizer)
102
self.Bind(wx.EVT_BUTTON, self.OnbtnSave, id=self.ID_SAVE)
103
self.Bind(wx.EVT_BUTTON, self.OnbtnClose, id=101)
105
def GetExitStatus(self):
106
return self.exitstatus
108
def GetShellTuple(self):
109
return self.txtTitle.GetValue(), self.txtCommand.GetValue(), self.txtArguments.GetValue(), self.txtDirectory.GetValue(), int(self.chkRunInPrompt.GetValue())
111
def OnbtnClose(self, event):
115
def OnbtnSave(self, event):
119
def SetShellTuple(self, title, command, args, dir, inprompt):
120
self.txtTitle.SetValue(title)
121
self.txtCommand.SetValue(command)
122
self.txtArguments.SetValue(args)
123
self.txtDirectory.SetValue(dir)
124
self.chkRunInPrompt.SetValue(inprompt)
126
126
class drScriptMenu(drMenu):
127
def __init__(self, parent):
128
drMenu.__init__(self, parent)
130
self.dynamicscriptext = ""
132
self.ID_ADD_SCRIPT = 3100
133
self.ID_NEW_SCRIPT = 3101
134
self.ID_EXISTING_SCRIPT = 3102
135
self.ID_SHELL_COMMAND = 3103
137
self.ID_EDIT_SCRIPT = 3003
138
self.ID_REMOVE_SCRIPT = 3005
139
self.ID_DYNAMIC_SCRIPT = 3006
141
self.ID_EXAMPLE_SCRIPTS = 3010
143
self.ID_SCRIPT_BASE = parent.ID_SCRIPT_BASE
145
self.ID_SCRIPT_MENU = 3500
148
self.userpreferencesdirectory = parent.userpreferencesdirectory
149
self.programdirectory = parent.programdirectory
157
self.ExampleScriptCount = 0
161
def getdrscriptmenulabel(self, label):
164
if len(self.parent.DrScriptShortcuts) == 0:
167
if label in self.titles:
168
i = self.titles.index(label)
169
shortcuttext = drShortcuts.GetShortcutLabel(self.parent.DrScriptShortcuts[i])
171
if len(shortcuttext) > 1:
172
return label + '\t' + shortcuttext
176
def loadscripts(self, SyncShortcuts = 1):
178
if self.parent.prefs.drscriptloadexamples:
179
self.examplemenu = wx.Menu()
180
self.loadscriptsfromfile(self.programdirectory + "/examples/DrScript/drscript.dat", self.examplemenu, 1)
181
self.AppendMenu(self.ID_EXAMPLE_SCRIPTS, "Examples", self.examplemenu)
182
self.AppendSeparator()
184
self.ExampleScriptCount = self.scriptcount
186
self.loadscriptsfromfile(self.userpreferencesdirectory + "/drscript.dat", self)
188
#Either there is no shortcuts file, or it is out of sync.
191
if not (l == len(self.parent.DrScriptShortcuts)):
192
self.parent.DrScriptShortcuts = []
193
self.parent.DrScriptShortcutNames = []
197
self.parent.DrScriptShortcuts.append(' #')
198
self.parent.DrScriptShortcutNames.append(self.titles[x])
201
def loadscriptsfromfile(self, scriptfile, target, appendprogpath = 0):
202
if (os.path.exists(scriptfile)):
210
f = open(scriptfile, 'rb')
213
while (len(line) > 0):
215
line = line[c:].rstrip()
217
while (lastCount > c):
218
folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
219
folderindex = folderindex - 1
220
menuTitleindex = menuTitleindex - 1
221
lastCount = lastCount - 1
224
folders.append(wx.Menu())
225
menuTitles.append(line[1:])
226
folderindex = folderindex + 1
227
menuTitleindex = menuTitleindex + 1
230
line_path = ExtractPreferenceFromText(line, "path")
232
line_path = self.programdirectory + "/" + line_path.replace('\\', '/')
233
line_title = ExtractPreferenceFromText(line, "title")
234
if (os.path.exists(line_path)):
235
self.titles.append(line_title)
236
folders[folderindex].Append((self.ID_SCRIPT_BASE + self.scriptcount), self.getdrscriptmenulabel(line_title))
237
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
238
self.scripts.append(line_path)
239
self.scriptcount = self.scriptcount + 1
243
#Add any menus not yet added:
245
while (lastCount > c):
246
folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
247
folderindex = folderindex - 1
248
menuTitleindex = menuTitleindex - 1
249
lastCount = lastCount - 1
251
drScrolledMessageDialog.ShowMessage(self.parent, ("Your drscript file is a tad messed up.\n"), "Error")
253
def OnAddExistingScript(self, event):
254
scriptfile = self.userpreferencesdirectory + "/drscript.dat"
255
dlg = drFileDialog.FileDialog(self.parent, "Select Script File", self.parent.prefs.wildcard)
256
if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
258
dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
260
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
261
if (dlg.ShowModal() == wx.ID_OK):
262
filen = dlg.GetPath().replace("\\", "/")
263
d = wx.TextEntryDialog(self.parent, 'Enter Script Title:', 'Add DrScript: Title', '')
265
if (d.ShowModal() == wx.ID_OK):
269
drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
271
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
272
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
273
self.scripts.append(filen)
274
self.titles.append(title)
276
x = len(self.parent.DrScriptShortcuts)
277
self.parent.DrScriptShortcuts.append('')
278
self.parent.DrScriptShortcutNames.append(title)
280
self.scriptcount = self.scriptcount + 1
282
f = open(scriptfile, 'a')
283
f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
286
#Update the shortcuts Too.
287
shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
288
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
290
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
293
def OnAddNewScript(self, event):
294
scriptfile = self.userpreferencesdirectory + "/drscript.dat"
295
dlg = drFileDialog.FileDialog(self.parent, "Save New Script File As", self.parent.prefs.wildcard, IsASaveDialog=True)
296
if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
298
dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
300
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
301
if (dlg.ShowModal() == wx.ID_OK):
302
filen = dlg.GetPath().replace("\\", "/")
303
d = wx.TextEntryDialog(self.parent, "Enter Script Title:", "Add DrScript: Title", "")
305
if (d.ShowModal() == wx.ID_OK):
309
drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
312
cfile = file(filen, 'wb')
313
cfile.write("#drscript\n\n")
316
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
317
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
318
self.scripts.append(filen)
319
self.titles.append(title)
321
x = len(self.parent.DrScriptShortcuts)
322
self.parent.DrScriptShortcuts.append('')
323
self.parent.DrScriptShortcutNames.append(title)
325
self.scriptcount = self.scriptcount + 1
327
f = open(scriptfile, 'a')
328
f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
331
shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
332
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
334
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
336
old = self.parent.txtDocument.filename
338
if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
339
self.parent.OpenFile(filename, True)
341
self.parent.OpenFile(filename, False)
344
def OnAddShellCommand(self, event):
345
scriptfile = self.userpreferencesdirectory + "/drscript.dat"
346
dlg = drFileDialog.FileDialog(self.parent, "Save New Shell Command As", self.parent.prefs.wildcard, IsASaveDialog=True)
347
if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
349
dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
351
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
352
if (dlg.ShowModal() == wx.ID_OK):
353
filen = dlg.GetPath().replace("\\", "/")
354
d = drNewShellDialog(self.parent, "New Shell Command")
355
if self.parent.prefs.platform_is_windows:
356
d.SetSize(wx.Size(425, 350))
358
if d.GetExitStatus():
359
title, cmd, args, dir, inprompt = d.GetShellTuple()
365
drScrolledMessageDialog.ShowMessage(self.parent, ("Bad shell title.\nDrPython will politely ignore your request to add this shell command."), "Error")
368
if args.find("<Current File>") > -1:
369
args = args.replace("<Current File>", self.parent.txtDocument.filename)
370
if args.find("<Current Directory>") > -1:
371
args = args.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
372
if dir.find("<Current Directory>") > -1:
373
dir = dir.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
375
cfile = file(filen, 'wb')
376
scripttext = "#drscript shell command\n\n"
378
scripttext = scripttext + 'import os\n\nos.chdir("' + dir + '")\n\n'
381
scripttext = scripttext + 'DrFrame.Execute("' + cmd + ' ' + args + '", "Running Shell Command")\n\n'
383
scripttext = scripttext + 'wx.Shell("' + cmd + ' ' + args + '")\n\n'
384
cfile.write(scripttext)
387
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
388
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
389
self.scripts.append(filen)
390
self.titles.append(title)
392
x = len(self.parent.DrScriptShortcuts)
393
self.parent.DrScriptShortcuts.append('')
394
self.parent.DrScriptShortcutNames.append(title)
396
self.scriptcount = self.scriptcount + 1
398
f = open(scriptfile, 'a')
399
f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
402
shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
403
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
405
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
408
def OnDynamicScript(self, event):
409
from drDynamicDrScriptDialog import drDynamicDrScriptDialog
410
d = drDynamicDrScriptDialog(self.parent, self.dynamicscriptext)
412
self.dynamicscriptext = d.GetText()
415
def OnEditScript(self, event):
416
if (len(self.scripts) > 0):
423
array = map(tuplify, self.titles, self.scripts)
426
titles = map(detuplify, array)
427
d = wx.SingleChoiceDialog(self.parent, "Select the Script to Edit:", "Edit Script", titles, wx.OK|wx.CANCEL)
428
d.SetSize(wx.Size(250, 250))
429
answer = d.ShowModal()
431
if (answer == wx.ID_OK):
433
old = self.parent.txtDocument.filename
434
filename = array[i][1].replace("\\", "/")
435
alreadyopen = map(lambda x: x.filename.replace("\\", "/"), self.parent.txtDocumentArray)
436
if filename in alreadyopen:
437
self.parent.setDocumentTo(alreadyopen.index(filename))
439
if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
440
self.parent.OpenFile(filename, True, False)
442
self.parent.OpenFile(filename, False, False)
444
drScrolledMessageDialog.ShowMessage(self.parent, ("There aren't any scripts to edit.\nTry \"New Script\" or \"Add Script\" Instead."), "Error")
446
def OnScript(self, event):
447
scriptindex = event.GetId() - self.ID_SCRIPT_BASE
448
f = open(self.scripts[scriptindex], 'r')
449
scripttext = f.read()
452
if (scripttext.find("DrFilename") > -1):
453
scripttext = scripttext.replace("DrFilename", "self.parent.txtDocument.filename")
454
if (scripttext.find("DrScript") > -1):
455
scripttext = scripttext.replace("DrScript", "self.parent.DrScript")
456
if (scripttext.find("DrDocument") > -1):
457
scripttext = scripttext.replace("DrDocument", "self.parent.txtDocument")
458
if (scripttext.find("DrPrompt") > -1):
459
scripttext = scripttext.replace("DrPrompt", "self.parent.txtPrompt")
460
if (scripttext.find("DrFrame") > -1):
461
scripttext = scripttext.replace("DrFrame", "self.parent")
464
code = compile((scripttext + '\n'), self.scripts[scriptindex], 'exec')
466
drScrolledMessageDialog.ShowMessage(self.parent, ("Error compiling script."), "Error", wx.DefaultPosition, wx.Size(550,300))
472
drScrolledMessageDialog.ShowMessage(self.parent, ("Error running script."), "Error", wx.DefaultPosition, wx.Size(550,300))
476
addscriptmenu = wx.Menu()
477
addscriptmenu.Append(self.ID_NEW_SCRIPT, "&New Script...")
478
addscriptmenu.Append(self.ID_EXISTING_SCRIPT, "&Existing Script...")
479
addscriptmenu.Append(self.ID_SHELL_COMMAND, "&Shell Command...")
481
self.AppendMenu(self.ID_ADD_SCRIPT, "&Add Script", addscriptmenu)
482
self.Append(self.ID_EDIT_SCRIPT, "&Edit Script...")
483
self.Append(self.ID_DYNAMIC_SCRIPT, "&Dynamic DrScript...")
484
self.AppendSeparator()
486
self.parent.Bind(wx.EVT_MENU, self.OnAddExistingScript, id=self.ID_EXISTING_SCRIPT)
487
self.parent.Bind(wx.EVT_MENU, self.OnAddNewScript, id=self.ID_NEW_SCRIPT)
488
self.parent.Bind(wx.EVT_MENU, self.OnAddShellCommand, id=self.ID_SHELL_COMMAND)
489
self.parent.Bind(wx.EVT_MENU, self.OnEditScript, id=self.ID_EDIT_SCRIPT)
490
self.parent.Bind(wx.EVT_MENU, self.OnDynamicScript, id=self.ID_DYNAMIC_SCRIPT)
492
def reloadscripts(self, oldlength = -1, SyncShortcuts = 1):
494
oldlength = len(self.scripts)
495
mnuitems = self.GetMenuItems()
499
self.Remove(mnuitems[x].GetId())
500
#mnuitems[x].Destroy()
505
self.loadscripts(SyncShortcuts)
127
def __init__(self, parent):
128
drMenu.__init__(self, parent)
130
self.dynamicscriptext = ""
132
self.ID_ADD_SCRIPT = 3100
133
self.ID_NEW_SCRIPT = 3101
134
self.ID_EXISTING_SCRIPT = 3102
135
self.ID_SHELL_COMMAND = 3103
137
self.ID_EDIT_SCRIPT = 3003
138
self.ID_REMOVE_SCRIPT = 3005
139
self.ID_DYNAMIC_SCRIPT = 3006
141
self.ID_EXAMPLE_SCRIPTS = 3010
143
self.ID_SCRIPT_BASE = parent.ID_SCRIPT_BASE
145
self.ID_SCRIPT_MENU = 3500
148
self.preferencesdirectory = parent.preferencesdirectory
149
self.programdirectory = parent.programdirectory
150
self.shortcutsdirectory = parent.shortcutsdirectory
158
self.ExampleScriptCount = 0
162
def getdrscriptmenulabel(self, label):
165
if not self.parent.DrScriptShortcuts:
168
if label in self.titles:
169
i = self.titles.index(label)
170
self.parent.DrScriptShortcuts[i]
171
shortcuttext = drShortcuts.GetShortcutLabel(self.parent.DrScriptShortcuts[i])
173
if len(shortcuttext) > 1:
174
if shortcuttext == "Tab":
176
return label + '\t' + shortcuttext
180
def reloadscripts(self, oldlength = -1, SyncShortcuts = 1):
182
oldlength = len(self.scripts)
183
mnuitems = self.GetMenuItems()
187
self.Remove(mnuitems[x].GetId())
188
#mnuitems[x].Destroy()
193
self.loadscripts(SyncShortcuts)
194
self.parent.DrScriptShortcutNames = []
196
for l in self.titles:
197
self.parent.DrScriptShortcutNames.append(l)
199
def loadscripts(self, SyncShortcuts = 1):
201
if self.parent.prefs.drscriptloadexamples:
202
self.examplemenu = wx.Menu()
203
self.loadscriptsfromfile(self.programdirectory + "/examples/DrScript/drscript.dat", self.examplemenu, 1)
204
self.AppendMenu(self.ID_EXAMPLE_SCRIPTS, "Examples", self.examplemenu)
205
self.AppendSeparator()
207
self.ExampleScriptCount = self.scriptcount
209
self.loadscriptsfromfile(self.preferencesdirectory + "/drscript.dat", self)
211
#Either there is no shortcuts file, or it is out of sync.
214
if l != len(self.parent.DrScriptShortcuts):
215
self.parent.DrScriptShortcuts = []
216
self.parent.DrScriptShortcutNames = []
220
self.parent.DrScriptShortcuts.append(' #')
221
self.parent.DrScriptShortcutNames.append(self.titles[x])
224
def loadscriptsfromfile(self, scriptfile, target, appendprogpath = 0):
225
if os.path.exists(scriptfile):
233
f = open(scriptfile, 'rb')
238
line = line[c:].rstrip()
241
folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
242
folderindex = folderindex - 1
243
menuTitleindex = menuTitleindex - 1
244
lastCount = lastCount - 1
247
folders.append(wx.Menu())
248
menuTitles.append(line[1:])
249
folderindex = folderindex + 1
250
menuTitleindex = menuTitleindex + 1
253
line_path = ExtractPreferenceFromText(line, "path")
255
line_path = self.programdirectory + "/" + line_path.replace('\\', '/')
257
if not os.path.isabs(line_path):
258
line_path = os.path.join(self.parent.drscriptsdirectory, line_path)
259
line_title = ExtractPreferenceFromText(line, "title")
260
if os.path.exists(line_path):
261
self.titles.append(line_title)
262
folders[folderindex].Append((self.ID_SCRIPT_BASE + self.scriptcount), self.getdrscriptmenulabel(line_title))
263
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
264
self.scripts.append(line_path)
265
self.scriptcount = self.scriptcount + 1
269
#Add any menus not yet added:
272
folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
273
folderindex = folderindex - 1
274
menuTitleindex = menuTitleindex - 1
275
lastCount = lastCount - 1
277
drScrolledMessageDialog.ShowMessage(self.parent, ("Your drscript file is a tad messed up.\n"), "Error")
279
def OnAddExistingScript(self, event):
280
scriptfile = self.preferencesdirectory + "/drscript.dat"
281
dlg = drFileDialog.FileDialog(self.parent, "Select Script File", self.parent.prefs.wildcard)
282
if self.parent.drscriptsdirectory:
284
dlg.SetDirectory(self.parent.drscriptsdirectory)
286
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " +
287
self.parent.drscriptsdirectory), "DrPython Error")
288
if dlg.ShowModal() == wx.ID_OK:
289
filen = dlg.GetPath().replace("\\", "/")
290
p = os.path.basename(filen) #patch by AB (suggests filename as title), 06.03.2007, thanks
291
s = os.path.splitext(p)
292
d = wx.TextEntryDialog(self.parent, 'Enter Script Title:', 'Add DrScript: Title', s[0]) #end patch by AB
294
if d.ShowModal() == wx.ID_OK:
298
drScrolledMessageDialog.ShowMessage(self.parent,
299
("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
301
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
302
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
304
#if directory is relativ, store only filename without path
305
pth = self.parent.drscriptsdirectory.lower()
306
if filen.lower().startswith (pth):
307
filen = filen[len(pth):]
311
self.scripts.append(filen)
312
self.titles.append(title)
314
self.parent.DrScriptShortcuts.append('')
315
self.parent.DrScriptShortcutNames.append(title)
317
self.scriptcount += 1
319
f = open(scriptfile, 'a')
320
f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
323
#Update the shortcuts Too.
324
shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
325
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts,
326
self.parent.DrScriptShortcutNames, "", False)
328
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
331
def OnAddNewScript(self, event):
332
scriptfile = self.preferencesdirectory + "/drscript.dat"
333
dlg = drFileDialog.FileDialog(self.parent, "Save New Script File As", self.parent.prefs.wildcard, IsASaveDialog=True)
334
if self.parent.drscriptsdirectory:
336
dlg.SetDirectory(self.parent.drscriptsdirectory)
338
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.drscriptsdirectory), "DrPython Error")
339
if dlg.ShowModal() == wx.ID_OK:
340
filename = dlg.GetPath().replace("\\", "/")
341
if not filename.endswith('.py'):
343
s = os.path.splitext(filename)[0]
344
s = os.path.basename(s)
345
d = wx.TextEntryDialog(self.parent, "Enter Script Title:", "Add DrScript: Title", s)
347
if d.ShowModal() == wx.ID_OK:
351
drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
354
cfile = file(filename, 'wb')
355
cfile.write("#drscript\n\n")
358
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
359
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
361
#if directory is relativ, store only filename without path
362
pth = self.parent.drscriptsdirectory.lower()
364
if filename.lower().startswith (pth):
365
fname = filename[len(pth):]
369
self.scripts.append(fname)
370
self.titles.append(fname)
372
self.parent.DrScriptShortcuts.append('')
373
self.parent.DrScriptShortcutNames.append(title)
375
self.scriptcount += 1
377
f = open(scriptfile, 'a')
378
f.write("<path>" + fname + "</path><title>" + title + "</title>\n")
381
shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
382
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
384
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
386
old = self.parent.txtDocument.filename
387
if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
388
self.parent.OpenFile(filename, True)
390
self.parent.OpenFile(filename, False)
393
def OnAddShellCommand(self, event):
394
scriptfile = self.preferencesdirectory + "/drscript.dat"
395
dlg = drFileDialog.FileDialog(self.parent, "Save New Shell Command As", self.parent.prefs.wildcard, IsASaveDialog=True)
396
if self.parent.drscriptsdirectory:
398
dlg.SetDirectory(self.parent.drscriptsdirectory)
400
drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " +
401
self.parent.drscriptsdirectory), "DrPython Error")
402
if dlg.ShowModal() == wx.ID_OK:
403
filen = dlg.GetPath().replace("\\", "/")
404
d = drNewShellDialog(self.parent, "New Shell Command")
405
if self.parent.PLATFORM_IS_WIN:
406
d.SetSize(wx.Size(425, 350))
408
if d.GetExitStatus():
409
title, cmd, args, dir, inprompt = d.GetShellTuple()
415
drScrolledMessageDialog.ShowMessage(self.parent, ("Bad shell title.\nDrPython will politely ignore your request to add this shell command."), "Error")
418
if args.find("<Current File>") > -1:
419
args = args.replace("<Current File>", self.parent.txtDocument.filename)
420
if args.find("<Current Directory>") > -1:
421
args = args.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
422
if dir.find("<Current Directory>") > -1:
423
dir = dir.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
425
cfile = file(filen, 'wb')
426
scripttext = "#drscript shell command\n\n"
428
scripttext = scripttext + 'import os\n\nos.chdir("' + dir + '")\n\n'
431
scripttext = scripttext + 'DrFrame.Execute("' + cmd + ' ' + args + '", "Running Shell Command")\n\n'
433
scripttext = scripttext + 'wx.Shell("' + cmd + ' ' + args + '")\n\n'
434
cfile.write(scripttext)
437
self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
438
self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
439
self.scripts.append(filen)
440
self.titles.append(title)
442
self.parent.DrScriptShortcuts.append('')
443
self.parent.DrScriptShortcutNames.append(title)
445
self.scriptcount = self.scriptcount + 1
447
f = open(scriptfile, 'a')
448
f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
451
shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
452
drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
454
drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
457
def OnDynamicScript(self, event):
458
from drDynamicDrScriptDialog import drDynamicDrScriptDialog
459
d = drDynamicDrScriptDialog(self.parent, self.dynamicscriptext)
461
self.dynamicscriptext = d.GetText()
464
def OnEditScript(self, event):
472
array = map(tuplify, self.titles, self.scripts)
475
titles = map(detuplify, array)
476
d = wx.SingleChoiceDialog(self.parent, "Select the Script to Edit:", "Edit Script", titles, wx.CHOICEDLG_STYLE)
477
#d.SetSize(wx.Size(250, 250))
478
answer = d.ShowModal()
480
if answer == wx.ID_OK:
482
filename = array[i][1].replace("\\", "/")
483
self.parent.OpenOrSwitchToFile(filename, editRecentFiles=False)
485
drScrolledMessageDialog.ShowMessage(self.parent, ("There aren't any scripts to edit.\nTry \"New Script\" or \"Add Script\" Instead."), "Error")
487
def OnScript(self, event):
488
scriptindex = event.GetId() - self.ID_SCRIPT_BASE
490
scriptfname = self.scripts[scriptindex]
491
#relativ path? => complete script filename
492
if not os.path.isabs(scriptfname):
493
scriptfname = os.path.join(self.parent.drscriptsdirectory, scriptfname)
495
f = open(scriptfname, 'r')
496
scripttext = f.read()
499
if scripttext.find("DrFilename") > -1:
500
scripttext = scripttext.replace("DrFilename", "self.parent.txtDocument.filename")
501
if scripttext.find("DrScript") > -1:
502
scripttext = scripttext.replace("DrScript", "self.parent.DrScript")
503
if scripttext.find("DrDocument") > -1:
504
scripttext = scripttext.replace("DrDocument", "self.parent.txtDocument")
505
if scripttext.find("DrPrompt") > -1:
506
scripttext = scripttext.replace("DrPrompt", "self.parent.txtPrompt")
507
if scripttext.find("DrFrame") > -1:
508
scripttext = scripttext.replace("DrFrame", "self.parent")
511
code = compile((scripttext + '\n'), scriptfname, 'exec')
513
drScrolledMessageDialog.ShowMessage(self.parent, ("Error compiling script."),
514
"Error", wx.DefaultPosition, wx.Size(550,300))
520
drScrolledMessageDialog.ShowMessage(self.parent, ("Error running script."),
521
"Error", wx.DefaultPosition, wx.Size(550,300))
525
addscriptmenu = wx.Menu()
526
addscriptmenu.Append(self.ID_NEW_SCRIPT, "&New Script...")
527
addscriptmenu.Append(self.ID_EXISTING_SCRIPT, "&Existing Script...")
528
addscriptmenu.Append(self.ID_SHELL_COMMAND, "&Shell Command...")
530
self.AppendMenu(self.ID_ADD_SCRIPT, "&Add Script", addscriptmenu)
531
self.Append(self.ID_EDIT_SCRIPT, "&Edit Script...")
532
self.Append(self.ID_DYNAMIC_SCRIPT, "&Dynamic DrScript...")
533
self.AppendSeparator()
535
self.parent.Bind(wx.EVT_MENU, self.OnAddExistingScript, id=self.ID_EXISTING_SCRIPT)
536
self.parent.Bind(wx.EVT_MENU, self.OnAddNewScript, id=self.ID_NEW_SCRIPT)
537
self.parent.Bind(wx.EVT_MENU, self.OnAddShellCommand, id=self.ID_SHELL_COMMAND)
538
self.parent.Bind(wx.EVT_MENU, self.OnEditScript, id=self.ID_EDIT_SCRIPT)
539
self.parent.Bind(wx.EVT_MENU, self.OnDynamicScript, id=self.ID_DYNAMIC_SCRIPT)