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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
#!${PYTHON_SHEBANG}
## Program: Pype Pad
## Module: $RCSfile: pypepad.py,v $
## Language: Python
## Date: $Date: 2006/05/26 12:36:30 $
## Version: $Revision: 1.6 $
## Copyright (c) Luca Antiga, David Steinman. All rights reserved.
## See LICENCE file for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
import sys
from vmtk import pypes
from vmtk import pypeserver
from multiprocessing import Process, Manager
class TkPadOutputStream(object):
def __init__(self,tk_text_widget):
self.text_widget = tk_text_widget
self.output_to_file = False
self.output_file = None
def write(self,text):
from Tkinter import NORMAL, END, DISABLED
self.text_widget["state"] = NORMAL
if text[0] == '\r':
endline = str(int(self.text_widget.index(END).split('.')[0])-1)
self.text_widget.delete(endline+".0",endline+".end")
self.text_widget.update()
text = text[1:]
self.text_widget.insert(END,text)
self.text_widget["state"] = DISABLED
self.text_widget.see(END)
self.text_widget.update()
if self.output_to_file and self.output_file:
self.output_file.write(text)
self.output_file.flush()
def flush(self):
if self.output_to_file and self.output_file:
self.output_file.flush()
class TkPadInputStream(object):
def __init__(self,tk_entry_widget,input_stream):
self.entry_widget = tk_entry_widget
self.entry_widget.bind("<KeyPress-Return>",self.EntryReturnHandler)
self.text = ''
self.input_stream = input_stream
def EntryReturnHandler(self,event):
from Tkinter import END, DISABLED
self.text = self.entry_widget.get()
self.entry_widget.delete(0,END)
self.entry_widget.quit()
self.entry_widget["state"] = DISABLED
self.input_stream.write(self.text+'\n')
def readline(self):
from Tkinter import NORMAL
self.entry_widget["state"] = NORMAL
self.entry_widget.focus_set()
self.entry_widget.grab_set()
self.entry_widget.mainloop()
self.entry_widget.grab_release()
return self.text
class CallbackShim:
"""Create a callback shim. Based on code by Scott David Daniels
(which also handles keyword arguments).
"""
def __init__(self, callback, *firstArgs):
self.__callback = callback
self.__firstArgs = firstArgs
def __call__(self, *args):
return self.__callback (*(self.__firstArgs + args))
class PypeTkPad(object):
def __init__(self, master, queue, pypeOutput):
self.queue = queue
self.pypeOutput = pypeOutput
self.master = master
self.master.title('PypePad')
self.master.geometry("%dx%d%+d%+d" % (700, 500, 0, 0))
self.master.minsize(300, 100)
self.output_file_name = None
self.BuildMainFrame()
self.UpdateOutput()
def NewCommand(self):
self.ClearAllCommand()
def OpenCommand(self):
import tkFileDialog
from Tkinter import END
openfile = tkFileDialog.askopenfile()
if not openfile:
return
for line in openfile.readlines():
self.text_input.insert(END,line)
def SaveCommand(self):
import tkFileDialog
from Tkinter import END
saveasfile = tkFileDialog.asksaveasfile()
if not saveasfile:
return
alltext = self.text_input.get("1.0",END)
saveasfile.write(alltext)
def QuitCommand(self):
self.master.quit()
def ClearInputCommand(self):
from Tkinter import END
self.text_input.delete("1.0",END)
def ClearOutputCommand(self):
from Tkinter import NORMAL, END, DISABLED
self.text_output["state"] = NORMAL
self.text_output.delete("1.0",END)
self.text_output["state"] = DISABLED
self.text_output.see(END)
self.text_output.update()
def ClearAllCommand(self):
self.ClearInputCommand()
self.ClearOutputCommand()
def OutputFileCommand(self):
import tkFileDialog
outputfilename = tkFileDialog.asksaveasfilename()
if sys.platform == 'win32' and len(outputfilename.split()) > 1:
outputfilename = '"%s"' % outputfilename
self.output_file_name = outputfilename
def AboutCommand(self):
self.OutputText('\n')
self.OutputText('* PypePad, Copyright (c) Luca Antiga, David Steinman. *\n')
self.OutputText('\n')
def UpdateOutput(self):
if self.pypeOutput:
text = self.pypeOutput.pop(0)
self.output_stream.write(text)
self.master.after(10,self.UpdateOutput)
def RunPype(self,arguments):
if not arguments:
return
if self.output_to_file.get() is not 'n' and self.output_file_name:
self.output_stream.output_to_file = True
self.output_stream.output_file = open(self.output_file_name,self.output_to_file.get())
else:
self.output_stream.output_to_file = False
self.queue.append(arguments)
def GetWordUnderCursor(self):
from Tkinter import CURRENT
splitindex = self.text_input.index(CURRENT).split('.')
line = self.text_input.get(splitindex[0]+".0",splitindex[0]+".end")
wordstart = line.rfind(' ',0,int(splitindex[1])-1)+1
wordend = line.find(' ',int(splitindex[1]))
if wordend == -1:
wordend = len(line)
word = line[wordstart:wordend]
return word
def GetWordIndex(self):
startindex = self.text_input.index("insert-1c wordstart")
endindex = self.text_input.index("insert-1c wordend")
if self.text_input.get(startindex+'-1c') == '-' and self.text_input.get(startindex+'-2c') == '-':
startindex = self.text_input.index("insert-1c wordstart -2c")
elif self.text_input.get(startindex+'-1c') == '-' and self.text_input.get(startindex+'-2c') == ' ':
startindex = self.text_input.index("insert-1c wordstart -1c")
self.wordIndex[0] = startindex
self.wordIndex[1] = endindex
word = self.text_input.get(self.wordIndex[0],self.wordIndex[1])
return word
def GetLogicalLine(self,physicallineid):
indexes, lines = self.GetLogicalLines()
return lines[indexes[physicallineid]]
def GetLogicalLineRange(self,physicallinefirstid,physicallinelastid):
indexes, lines = self.GetLogicalLines()
return lines[indexes[physicallinefirstid]:indexes[physicallinelastid]+1]
def GetAllLogicalLines(self):
return self.GetLogicalLines()[1]
def GetLogicalLines(self):
from Tkinter import END
physicallines = self.text_input.get("1.0",END).split('\n')
lines = []
indexes = [0] * len(physicallines)
lineid = 0
previousline = ""
join = 0
for line in physicallines:
if line.startswith('#'):
if join:
indexes[lineid] = indexes[lineid-1]
elif join:
if line.endswith('\\'):
lines[-1] = lines[-1] + " " + line[:-1]
join = 1
else:
lines[-1] = lines[-1] + " " + line
join = 0
indexes[lineid] = indexes[lineid-1]
else:
if line.endswith('\\'):
join = 1
lines.append(line[:-1])
else:
lines.append(line)
join = 0
if lineid > 0:
indexes[lineid] = indexes[lineid-1]+1
lineid += 1
return indexes, lines
def GetLineUnderCursor(self):
from Tkinter import INSERT
currentlineid = int(self.text_input.index(INSERT).split('.')[0]) - 1
return self.GetLogicalLine(currentlineid)
def RunAllCommand(self):
lines = self.GetAllLogicalLines()
for line in lines:
if line and line.strip():
self.RunPype(line)
def RunLineCommand(self):
line = self.GetLineUnderCursor()
if line and line.strip():
self.RunPype(line)
def RunSelectionCommand(self):
from Tkinter import TclError, SEL_FIRST, SEL_LAST
try:
firstlineid = int(self.text_input.index(SEL_FIRST).split('.')[0]) - 1
lastlineid = int(self.text_input.index(SEL_LAST).split('.')[0]) - 1
lines = self.GetLogicalLineRange(firstlineid,lastlineid)
for line in lines:
self.RunPype(line)
except TclError:
pass
def GetSuggestionsList(self,word):
list = []
try:
exec('import vmtkscripts')
except ImportError:
return None
if word.startswith('--'):
list = ['--pipe','--help']
elif word.startswith('-'):
optionlist = []
scriptindex = self.text_input.search('vmtk',self.wordIndex[0],backwards=1)
moduleName = self.text_input.get( scriptindex,scriptindex+' wordend' )
try:
exec('import '+moduleName)
exec('scriptObjectClassName = '+moduleName+'.'+moduleName)
exec ('scriptObject = '+moduleName+'.'+scriptObjectClassName +'()')
members = scriptObject.InputMembers + scriptObject.OutputMembers
for member in members:
optionlist.append('-'+member.OptionName)
exec('list = [option for option in optionlist if option.count(word)]')
except:
return list
else:
exec('list = [scriptname for scriptname in vmtkscripts.__all__ if scriptname.count(word) ]')
return list
def FillSuggestionsList(self,word):
from Tkinter import END
self.suggestionslist.delete(0,END)
suggestions = self.GetSuggestionsList(word)
for suggestion in suggestions:
self.suggestionslist.insert(END,suggestion)
def ReplaceTextCommand(self,word):
self.text_input.delete(self.wordIndex[0],self.wordIndex[1])
self.text_input.insert(self.wordIndex[0],word)
self.text_input.focus_set()
def ShowHelpCommand(self):
word = self.GetWordUnderCursor()
self.OutputText(word)
if word:
self.RunPype(word+' --help')
else:
self.OutputText('Enter your vmtk Pype above and Run.\n')
def AutoCompleteCommand(self):
word = self.GetWordIndex()
self.suggestionswindow.withdraw()
if word:
self.FillSuggestionsList(word)
self.suggestionswindow.geometry("%dx%d%+d%+d" % (400, 150, self.text_output.winfo_rootx(),self.text_output.winfo_rooty()))
self.suggestionswindow.deiconify()
self.suggestionswindow.lift()
def InsertScriptName(self,scriptname):
from Tkinter import INSERT
self.text_input.insert(INSERT,scriptname+' ')
def InsertFileName(self):
from Tkinter import INSERT
import tkFileDialog
openfilename = tkFileDialog.askopenfilename()
if not openfilename:
return
if len(openfilename.split()) > 1:
openfilename = '"%s"' % openfilename
self.text_input.insert(INSERT,openfilename+' ')
def KeyPressHandler(self,event):
if event.keysym == "Tab" :
self.AutoCompleteCommand()
self.suggestionslist.focus_set()
self.suggestionslist.selection_set(0)
return "break"
else:
self.text_input.focus_set()
def TopKeyPressHandler(self,event):
from Tkinter import ACTIVE, INSERT
if event.keysym in ['Down','Up'] :
self.suggestionslist.focus_set()
elif event.keysym == "Return":
word = self.suggestionslist.get(ACTIVE)
self.ReplaceTextCommand(word)
self.suggestionswindow.withdraw()
self.text_input.focus_set()
elif len(event.keysym) == 1 :
self.suggestionswindow.withdraw()
self.text_input.insert(INSERT,event.keysym)
self.text_input.focus_set()
else :
self.suggestionswindow.withdraw()
self.text_input.focus_set()
def NewHandler(self,event):
self.NewCommand()
def OpenHandler(self,event):
self.OpenCommand()
def SaveHandler(self,event):
self.SaveCommand()
def InsertFileNameHandler(self,event):
self.InsertFileName()
return "break"
def QuitHandler(self,event):
self.QuitCommand()
def ShowHelpHandler(self,event):
self.ShowHelpCommand()
def RunKeyboardHandler(self,event):
from Tkinter import SEL_FIRST, TclError
try:
self.text_input.index(SEL_FIRST)
self.RunSelectionCommand()
except TclError:
self.RunLineCommand()
return "break"
def RunAllHandler(self,event):
self.RunAllCommand()
def PopupHandler(self,event):
try:
self.popupmenu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popupmenu.grab_release()
def OutputText(self,text):
from Tkinter import NORMAL, END, DISABLED
self.text_output["state"] = NORMAL
self.text_output.insert(END,text)
self.text_output["state"] = DISABLED
def BuildScriptMenu(self,parentmenu,modulename):
from Tkinter import Menu
menu = Menu(parentmenu,bd=1,activeborderwidth=0)
try:
exec('import '+ modulename)
except ImportError:
return None
scriptnames = []
exec ('scriptnames = [scriptname for scriptname in '+modulename+'.__all__]')
menulength = 20
for i in range(len(scriptnames)/menulength+1):
subscriptnames = scriptnames[i*menulength:(i+1)*menulength]
if not subscriptnames:
break
submenu = Menu(menu,bd=1,activeborderwidth=0)
menu.add_cascade(label=subscriptnames[0]+"...",menu=submenu)
for scriptname in subscriptnames:
callback = CallbackShim(self.InsertScriptName,scriptname)
submenu.add_command(label=scriptname,command=callback)
return menu
def BuildMainFrame(self):
from Tkinter import Menu, IntVar, StringVar, Toplevel, Listbox, Frame, PanedWindow, Text, Scrollbar, Entry
from Tkinter import X, N, S, W, E, VERTICAL, TOP, END, DISABLED, RAISED
menu = Menu(self.master,activeborderwidth=0,bd=0)
self.master.config(menu=menu)
filemenu = Menu(menu,tearoff=0,bd=1,activeborderwidth=0)
menu.add_cascade(label="File", underline=0, menu=filemenu)
filemenu.add_command(label="New", accelerator='Ctrl+N',command=self.NewCommand)
filemenu.add_command(label="Open...",accelerator='Ctrl+O', command=self.OpenCommand)
filemenu.add_command(label="Save as...",accelerator='Ctrl+S', command=self.SaveCommand)
filemenu.add_separator()
filemenu.add_command(label="Quit",accelerator='Ctrl+Q', command=self.QuitCommand)
self.log_on = IntVar()
self.log_on.set(1)
self.output_to_file = StringVar()
self.output_to_file.set('n')
scriptmenu = Menu(menu,tearoff=0,bd=1,activeborderwidth=0)
modulenames = ['vmtkscripts']
for modulename in modulenames:
scriptsubmenu = self.BuildScriptMenu(menu,modulename)
if scriptsubmenu:
scriptmenu.add_cascade(label=modulename,menu=scriptsubmenu)
editmenu = Menu(menu,tearoff=0,bd=1,activeborderwidth=0)
menu.add_cascade(label="Edit",underline=0, menu=editmenu)
editmenu.add_cascade(label="Insert script",menu=scriptmenu)
editmenu.add_command(label="Insert file name", accelerator='Ctrl+F',command=self.InsertFileName)
editmenu.add_separator()
editmenu.add_command(label="Clear input", command=self.ClearInputCommand)
editmenu.add_command(label="Clear output", command=self.ClearOutputCommand)
editmenu.add_command(label="Clear all", command=self.ClearAllCommand)
editmenu.add_separator()
editmenu.add_checkbutton(label="Log", variable=self.log_on)
editmenu.add_separator()
editmenu.add_radiobutton(label="No output to file", variable=self.output_to_file,value='n')
editmenu.add_radiobutton(label="Write output to file", variable=self.output_to_file,value='w')
editmenu.add_radiobutton(label="Append output to file", variable=self.output_to_file,value='a')
editmenu.add_command(label="Output file...", command=self.OutputFileCommand)
runmenu = Menu(menu,tearoff=0,bd=1,activeborderwidth=0)
menu.add_cascade(label="Run", underline=0, menu=runmenu)
runmenu.add_command(label="Run all", command=self.RunAllCommand)
runmenu.add_command(label="Run current line", command=self.RunLineCommand)
runmenu.add_command(label="Run selection", command=self.RunSelectionCommand)
helpmenu = Menu(menu,tearoff=0,bd=1,activeborderwidth=0)
menu.add_cascade(label="Help", underline=0, menu=helpmenu)
helpmenu.add_command(label="Help", underline=0, accelerator='F1',command=self.ShowHelpCommand)
helpmenu.add_command(label="About", underline=0, command=self.AboutCommand)
self.master.bind("<Control-KeyPress-q>", self.QuitHandler)
self.master.bind("<Control-KeyPress-n>", self.NewHandler)
self.master.bind("<Control-KeyPress-o>", self.OpenHandler)
self.master.bind("<Control-KeyPress-s>", self.SaveHandler)
self.master.bind("<Control-KeyPress-f>", self.InsertFileNameHandler)
self.master.bind("<KeyPress-F1>", self.ShowHelpHandler)
self.master.bind("<KeyPress>", self.KeyPressHandler)
self.wordIndex = ['1.0','1.0']
self.suggestionswindow = Toplevel(bg='#ffffff',bd=0,height=50,width=600,highlightthickness=0,takefocus=True)
self.suggestionswindow.overrideredirect(1)
self.suggestionslist = Listbox(self.suggestionswindow,bg='#ffffff',bd=1,fg='#336699',activestyle='none',highlightthickness=0,height=9)
self.suggestionslist.insert(END,"foo")
self.suggestionslist.pack(side=TOP,fill=X)
self.suggestionswindow.bind("<KeyPress>", self.TopKeyPressHandler)
self.suggestionswindow.withdraw()
self.master.rowconfigure(0,weight=1)
self.master.columnconfigure(0,weight=1)
content = Frame(self.master,bd=0,padx=2,pady=2)
content.grid(row=0,column=0,sticky=N+S+W+E)
content.rowconfigure(0,weight=1,minsize=50)
content.rowconfigure(1,weight=0)
content.columnconfigure(0,weight=1)
panes = PanedWindow(content,orient=VERTICAL,bd=1,sashwidth=8,sashpad=0,sashrelief=RAISED,showhandle=True)
panes.grid(row=0,column=0,sticky=N+S+W+E)
frame1 = Frame(panes,bd=0)
frame1.grid(row=0,column=0,sticky=N+S+W+E)
frame1.columnconfigure(0,weight=1)
frame1.columnconfigure(1,weight=0)
frame1.rowconfigure(0,weight=1)
panes.add(frame1,height=300,minsize=20)
frame2 = Frame(panes,bd=0)
frame2.grid(row=1,column=0,sticky=N+S+W+E)
frame2.columnconfigure(0,weight=1)
frame2.columnconfigure(1,weight=0)
frame2.rowconfigure(0,weight=1)
panes.add(frame2,minsize=20)
self.text_input = Text(frame1, bg='#ffffff',bd=1,highlightthickness=0)
self.text_input.bind("<KeyPress>", self.KeyPressHandler)
self.text_input.bind("<Button-3>", self.PopupHandler)
self.text_input.bind("<Control-Return>", self.RunKeyboardHandler)
self.input_scrollbar = Scrollbar(frame1,orient=VERTICAL,command=self.text_input.yview)
self.text_input["yscrollcommand"] = self.input_scrollbar.set
self.text_output = Text(frame2,state=DISABLED,bd=1,bg='#ffffff',highlightthickness=0)
self.output_scrollbar = Scrollbar(frame2,orient=VERTICAL,command=self.text_output.yview)
self.text_output["yscrollcommand"] = self.output_scrollbar.set
self.text_entry = Entry(content,bd=1,bg='#ffffff',state=DISABLED,highlightthickness=0)
self.text_input.focus_set()
self.text_input.grid(row=0,column=0,sticky=N+S+W+E)
self.input_scrollbar.grid(row=0,column=1,sticky=N+S+W+E)
self.text_output.grid(row=0,column=0,sticky=N+S+W+E)
self.output_scrollbar.grid(row=0,column=1,sticky=N+S+W+E)
self.text_entry.grid(row=1,column=0,sticky=N+S+W+E)
self.popupmenu = Menu(self.text_input, tearoff=1, bd=0)
self.popupmenu.add_command(label="Context help", command=self.ShowHelpCommand)
self.popupmenu.add_cascade(label="Insert script",menu=scriptmenu)
self.popupmenu.add_command(label="Insert file name...", command=self.InsertFileName)
self.popupmenu.add_separator()
self.popupmenu.add_command(label="Run all", command=self.RunAllCommand)
self.popupmenu.add_command(label="Run current line", command=self.RunLineCommand)
self.popupmenu.add_command(label="Run selection", command=self.RunSelectionCommand)
self.output_stream = TkPadOutputStream(self.text_output)
self.input_stream = TkPadInputStream(self.text_entry,self.output_stream)
def RunPypeTkPad():
manager = Manager()
queue = manager.list()
output = manager.list()
pypeProcess = Process(target=pypeserver.PypeServer, args=(queue,output,output))
pypeProcess.start()
from Tkinter import Tk
root = Tk()
app = PypeTkPad(root,queue,output)
root.mainloop()
pypeProcess.terminate()
if __name__=='__main__':
RunPypeTkPad()
|