From e3268bb8e58ffc161831ffff86374b3b4983ff4a Mon Sep 17 00:00:00 2001 From: Cas Cremers Date: Wed, 27 Aug 2008 10:04:22 +0200 Subject: [PATCH] GUI: Added a large set of possible output formats to the right-click menu. To do: We still need better error handling. - File exists: overwrite? - Write failed popup. - Check for empty file at the end (what if dot does not support this particular output format?) --- gui/Gui/Attackwindow.py | 52 ++++++++++++++++++++++++++++++++-------- gui/Gui/Misc.py | 19 +++++++++++++++ gui/Gui/Scytherthread.py | 4 ---- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/gui/Gui/Attackwindow.py b/gui/Gui/Attackwindow.py index 5e222bc..6a5f053 100644 --- a/gui/Gui/Attackwindow.py +++ b/gui/Gui/Attackwindow.py @@ -23,6 +23,8 @@ """ Import externals """ import wx +import os +from Misc import * #--------------------------------------------------------------------------- @@ -163,8 +165,16 @@ class AttackDisplay(wx.ScrolledWindow): Pop up menu """ self.popupmenu = wx.Menu() - #item = self.popupmenu.Append(-1,"Export image") - #self.Bind(wx.EVT_MENU, self.OnExportImage, item) + item = self.popupmenu.Append(-1,"Export image (.png)") + self.Bind(wx.EVT_MENU, self.OnExportPng, item) + item = self.popupmenu.Append(-1,"Export image (.ps)") + self.Bind(wx.EVT_MENU, self.OnExportPs, item) + item = self.popupmenu.Append(-1,"Export image (.pdf)") + self.Bind(wx.EVT_MENU, self.OnExportPdf, item) + item = self.popupmenu.Append(-1,"Export image (.svg)") + self.Bind(wx.EVT_MENU, self.OnExportSvg, item) + item = self.popupmenu.Append(-1,"Export image (.fig)") + self.Bind(wx.EVT_MENU, self.OnExportFig, item) item = self.popupmenu.Append(-1,"Export graphviz data (.dot)") self.Bind(wx.EVT_MENU, self.OnExportDot, item) @@ -191,21 +201,43 @@ class AttackDisplay(wx.ScrolledWindow): dialog.Destroy() return res - def saveFileType(self, ext, data): + def saveFileName(self, ext): (p,r,l) = self.win.claim.triplet() prefix = "pattern-%s_%s_%s-%s" % (p,r,l,self.attack.id) suggested = "%s.%s" % (prefix,ext) res = self.askUserForFilename(style=wx.SAVE, wildcard="*.%s" % (ext), defaultFile = "%s" % (suggested)) - if res != None: - fp = open(res,'w') - fp.write(data) - fp.close() + return res - def OnExportImage(self, event): - self.saveFileType("ps",self.attack.scytherDot) + def exportImage(self, type,ext=None): + if ext == None: + ext = type + res = self.saveFileName(ext) + if res != None: + cmd = "dot -T%s" % (type) + cmdpushwrite(cmd,self.attack.scytherDot,res) + + def OnExportPng(self, event): + self.exportImage("png") + + def OnExportPs(self, event): + self.exportImage("ps") + + def OnExportPdf(self, event): + self.exportImage("pdf") + + def OnExportSvg(self, event): + self.exportImage("svg") + + def OnExportFig(self, event): + self.exportImage("fig") def OnExportDot(self, event): - self.saveFileType("dot",self.attack.scytherDot) + res = self.saveFileName("dot") + if res != None: + fp = open(res,'w') + fp.write(self.attack.scytherDot) + fp.close() + diff --git a/gui/Gui/Misc.py b/gui/Gui/Misc.py index 749322a..be161d3 100644 --- a/gui/Gui/Misc.py +++ b/gui/Gui/Misc.py @@ -62,3 +62,22 @@ def mypath(file): basedir = os.path.dirname(__file__) return os.path.join(basedir,file) +# commands: push data in, get fp.write out +def cmdpushwrite(cmd,data,fname): + """ + Feed stdin data to cmd, write the output to a freshly created file + 'fname'. The file is flushed and closed at the end. + """ + fp = open(fname,'w') + # execute command + cin,cout = os.popen2(cmd,'b') + cin.write(data) + cin.close() + for l in cout.read(): + fp.write(l) + cout.close() + fp.flush() + fp.close() + +#--------------------------------------------------------------------------- +# vim: set ts=4 sw=4 et list lcs=tab\:>-: diff --git a/gui/Gui/Scytherthread.py b/gui/Gui/Scytherthread.py index 3cdf3c4..303f7fa 100644 --- a/gui/Gui/Scytherthread.py +++ b/gui/Gui/Scytherthread.py @@ -23,11 +23,7 @@ """ Import externals """ import wx -import os -import sys -import re import threading -import StringIO #---------------------------------------------------------------------------