Further wxPython4 fixes.

This commit is contained in:
Cas Cremers 2020-10-27 23:38:48 +01:00
parent 9dd73c2e38
commit 85d646b133
3 changed files with 57 additions and 49 deletions

View File

@ -201,13 +201,12 @@ class AttackDisplay(wx.ScrolledWindow):
res = self.askUserForFilename(style=wx.FD_SAVE, wildcard="*.%s" % (ext), defaultFile = "%s" % (suggested)) res = self.askUserForFilename(style=wx.FD_SAVE, wildcard="*.%s" % (ext), defaultFile = "%s" % (suggested))
return res return res
def exportImage(self, type,ext=None): def exportImage(self, imgtype, ext=None):
if ext == None: if ext == None:
ext = type ext = imgtype
res = self.saveFileName(ext) res = self.saveFileName(ext)
if res != None: if res != None:
cmd = "dot -T%s" % (type) dotOutputWrite(self.attack.scytherDot,res,["-T" + imgtype])
cmdpushwrite(cmd,self.attack.scytherDot,res)
def OnExportPng(self, event): def OnExportPng(self, event):
self.exportImage("png") self.exportImage("png")

View File

@ -118,42 +118,32 @@ def makeImageDot(dotdata,attackthread=None):
if Preference.usePIL(): if Preference.usePIL():
# If we have the PIL library, we can do postscript! great # If we have the PIL library, we can do postscript! great
# stuff. # stuff.
type = "ps" imgtype = "ps"
ext = ".ps" ext = ".ps"
else: else:
# Ye olde pnge file # Ye olde pnge file
type = "png" imgtype = "png"
ext = ".png" ext = ".png"
# Retrieve dot command path # Write dot source file into temporary file to simplify dealing with graphviz invocation across platforms
dotcommand = FindDot.findDot() (fd_in,fpname_in) = Temporary.tempcleaned(ext)
f_in = os.fdopen(fd_in,'w')
# command to write to temporary file if attackthread:
(fd2,fpname2) = Temporary.tempcleaned(ext) writeGraph(attackthread,dotdata,f_in)
f = os.fdopen(fd2,'w') else:
f_in.write(dotdata)
f_in.close()
# Set up command # Set up command
cmd = "%s -T%s" % (dotcommand,type) dotcommand = FindDot.findDot()
cmd = [dotcommand, "-T" + imgtype, fpname_in]
# execute command # execute command
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) (fd_out,fpname_out) = Temporary.tempcleaned(ext)
p = Popen(cmd, stdout=fd_out)
p.wait()
return (fpname_out, imgtype)
if attackthread:
writeGraph(attackthread,dotdata,p.stdin)
else:
p.stdin.write(dotdata)
p.stdin.close()
for l in p.stdout.read():
f.write(str(l))
p.stdout.close()
f.flush()
f.close()
return (fpname2, type)
def makeImage(attack,attackthread=None): def makeImage(attack,attackthread=None):
@ -161,10 +151,10 @@ def makeImage(attack,attackthread=None):
""" This should clearly be a method of 'attack' """ """ This should clearly be a method of 'attack' """
(name,type) = makeImageDot(attack.scytherDot,attackthread) (name,imgtype) = makeImageDot(attack.scytherDot,attackthread)
# if this is done, store and report # if this is done, store and report
attack.file = name attack.file = name
attack.filetype = type attack.filetype = imgtype
def testImage(): def testImage():

View File

@ -25,7 +25,14 @@
""" Import externals """ """ Import externals """
import os.path import os.path
from subprocess import Popen,PIPE from subprocess import Popen,PIPE,run
from shlex import quote
""" Import scyther components """
from Scyther import FindDot
""" Import scyther-gui components """
from . import Temporary
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
@ -67,24 +74,36 @@ def mypath(file):
basedir = os.path.split(cmd_file)[0] basedir = os.path.split(cmd_file)[0]
return os.path.join(basedir,file) return os.path.join(basedir,file)
# commands: push data in, get fp.write out # Write string to tempfile, return (filedescriptor,name)
def cmdpushwrite(cmd,data,fname): def stringToTempfile(data,ext="tmp"):
""" """
Feed stdin data to cmd, write the output to a freshly created file Take data (a string) and write it to a safe temporary file.
'fname'. The file is flushed and closed at the end. Return the resulting filedescriptor and name as a pair.
""" """
fp = open(fname,'w') (fd,fpname) = Temporary.tempcleaned(ext)
# execute command f = os.fdopen(fd,'w')
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) f.write(data)
(cin,cout) = (p.stdin, p.stdout) f.close()
cin.write(data) return (fd, fpname)
cin.close()
for l in cout.read():
fp.write(l) # commands: push data in as file named argument to dot, get fp.write out
cout.close() def dotOutputWrite(data,fname,cmd=[]):
fp.flush() """
fp.close() Feed stdin data to cmd array, write the output to a freshly
created file 'fname'. The file is flushed and closed at the end.
TODO: In reality, this particular dot data was already written to another temp file when rendering the attack graph. We should be reusing that file instead of writing a new one.
"""
(fd_in,fpname_in) = stringToTempfile(data,ext="dot")
dotcommand = FindDot.findDot()
execcmd = [dotcommand] + cmd + ["-o" + quote(fname), quote(fpname_in)]
print (execcmd)
# execute command
run(execcmd)
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# vim: set ts=4 sw=4 et list lcs=tab\:>-: # vim: set ts=4 sw=4 et list lcs=tab\:>-: