2006-08-02 13:59:57 +01:00
|
|
|
#!/usr/bin/python
|
2007-06-11 13:09:24 +01:00
|
|
|
"""
|
|
|
|
Scyther : An automatic verifier for security protocols.
|
2020-10-28 07:43:08 +00:00
|
|
|
Copyright (C) 2007-2020 Cas Cremers
|
2007-06-11 13:09:24 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
"""
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Preferences window and logic for saving and loading such things.
|
|
|
|
Thus, some default things can be set here.
|
|
|
|
|
|
|
|
init loads stuff
|
|
|
|
save save the settings after some changes
|
|
|
|
set(k,v)
|
|
|
|
get(k)
|
|
|
|
|
|
|
|
Currently used:
|
|
|
|
|
|
|
|
match
|
|
|
|
maxruns
|
|
|
|
scytheroptions
|
2006-08-09 10:26:15 +01:00
|
|
|
bindir where the scyther executables reside
|
|
|
|
splashscreen 0/1
|
2006-08-02 13:59:57 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import externals """
|
|
|
|
|
|
|
|
import wx
|
|
|
|
import os.path
|
2006-08-02 23:44:10 +01:00
|
|
|
import sys
|
2006-08-02 13:59:57 +01:00
|
|
|
from time import localtime,strftime
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import scyther-gui components """
|
2020-10-27 21:09:03 +00:00
|
|
|
from . import Makeimage
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Globals """
|
2006-08-10 12:50:57 +01:00
|
|
|
# Do we have the Python Imaging library?
|
|
|
|
havePIL = True
|
2008-08-25 13:59:42 +01:00
|
|
|
testPILOkay = None
|
2006-08-10 12:50:57 +01:00
|
|
|
try:
|
|
|
|
import Image
|
|
|
|
except ImportError:
|
|
|
|
havePIL = False
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
""" Locations of preferences. The last one is supposedly writable. """
|
|
|
|
prefname = "scythergui-config"
|
2006-08-02 23:44:10 +01:00
|
|
|
preflocs = []
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2006-08-10 12:50:57 +01:00
|
|
|
def usePIL():
|
|
|
|
"""
|
|
|
|
Determine whether or not we should use the PIL library
|
|
|
|
"""
|
2008-08-25 13:59:42 +01:00
|
|
|
global havePIL, testPILOkay
|
|
|
|
|
|
|
|
if not havePIL:
|
|
|
|
return False
|
2006-08-10 12:50:57 +01:00
|
|
|
|
|
|
|
# Only if we have it, and it is windows.
|
2008-08-25 13:59:42 +01:00
|
|
|
if not sys.platform.startswith("lin"):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Seems fine. But did we already test it?
|
|
|
|
if testPILOkay != None:
|
|
|
|
return testPILOkay
|
2006-08-10 12:50:57 +01:00
|
|
|
|
2008-08-25 13:59:42 +01:00
|
|
|
# Test the usage
|
|
|
|
testPILOkay = True
|
|
|
|
testPILOkay = testPIL()
|
|
|
|
return testPILOkay
|
2006-08-10 12:50:57 +01:00
|
|
|
|
2008-05-02 16:10:29 +01:00
|
|
|
def doNotUsePIL():
|
|
|
|
"""
|
|
|
|
Disable
|
|
|
|
"""
|
|
|
|
global havePIL
|
|
|
|
|
|
|
|
havePIL = False
|
|
|
|
|
2008-08-25 13:59:42 +01:00
|
|
|
|
|
|
|
def testPIL():
|
|
|
|
"""
|
|
|
|
Test whether PIL works as we want it.
|
|
|
|
|
|
|
|
We generate a postscript file from a dot file, and see what happens.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# depends on PIL lib
|
|
|
|
okay = True
|
|
|
|
try:
|
|
|
|
Makeimage.testImage()
|
|
|
|
# PIL seems fine
|
|
|
|
except:
|
|
|
|
# PIL broke
|
|
|
|
doNotUsePIL()
|
|
|
|
okay = False
|
|
|
|
|
|
|
|
return okay
|
|
|
|
|
|
|
|
|
2006-08-10 12:50:57 +01:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
class Preferences(dict):
|
|
|
|
|
2012-04-25 16:03:51 +01:00
|
|
|
def setDict(self,d):
|
|
|
|
"""
|
|
|
|
Copy dict into self.
|
|
|
|
"""
|
2020-10-27 21:09:03 +00:00
|
|
|
for x in list(d.keys()):
|
2012-04-25 16:03:51 +01:00
|
|
|
self[x] = d[x]
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
def parse(self,line):
|
|
|
|
line = line.strip()
|
|
|
|
|
|
|
|
""" Skip comments """
|
|
|
|
if not line.startswith("#"):
|
|
|
|
split = line.find("=")
|
|
|
|
if split != -1:
|
|
|
|
key = line[:split].strip()
|
|
|
|
data = line[(split+1):]
|
|
|
|
self[key] = data.decode("string_escape")
|
2006-08-08 16:00:20 +01:00
|
|
|
#print "Read %s=%s" % (key,self[key])
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def load(self,file=""):
|
|
|
|
if file == None:
|
|
|
|
self["test1"] = "Dit is met een ' en een \", en dan\nde eerste dinges"
|
|
|
|
self["test2"] = "En dit de tweede"
|
|
|
|
elif file == "":
|
|
|
|
"""
|
|
|
|
Test default locations
|
|
|
|
"""
|
|
|
|
for f in preflocs:
|
2006-08-02 23:44:10 +01:00
|
|
|
self.load(os.path.join(f,prefname))
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
"""
|
|
|
|
Read this file
|
|
|
|
"""
|
|
|
|
if os.path.isfile(file):
|
|
|
|
fp = open(file,"r")
|
|
|
|
for l in fp.readlines():
|
|
|
|
self.parse(l)
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def show(self):
|
2020-10-27 21:09:03 +00:00
|
|
|
print("Preferences:")
|
|
|
|
for k in list(self.keys()):
|
|
|
|
print("%s=%s" % (k, self[k]))
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
2020-10-27 21:09:03 +00:00
|
|
|
print("Saving preferences")
|
2006-08-02 23:44:10 +01:00
|
|
|
prefpath = preflocs[-1]
|
2006-08-02 13:59:57 +01:00
|
|
|
if not os.access(prefpath,os.W_OK):
|
|
|
|
os.makedirs(prefpath)
|
|
|
|
savename = os.path.join(prefpath,prefname)
|
|
|
|
fp = open(savename,"w")
|
|
|
|
|
|
|
|
fp.write("# Scyther-gui configuration file.\n#\n")
|
|
|
|
date = strftime("%c",localtime())
|
|
|
|
fp.write("# Last written on %s\n" % (date))
|
|
|
|
fp.write("# Do not edit - any changes will be overwritten by Scyther-gui\n\n")
|
|
|
|
|
|
|
|
l = list(self.keys())
|
|
|
|
l.sort()
|
|
|
|
for k in l:
|
|
|
|
fp.write("%s=%s\n" % (k, self[k].encode("string_escape")))
|
|
|
|
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def init():
|
|
|
|
"""
|
|
|
|
Load the preferences from a file, if possible
|
|
|
|
"""
|
2006-08-02 23:44:10 +01:00
|
|
|
global prefs,preflocs
|
|
|
|
|
|
|
|
sp = wx.StandardPaths.Get()
|
|
|
|
confdir = sp.GetConfigDir()
|
|
|
|
confdir += "/scyther"
|
2006-08-08 16:00:20 +01:00
|
|
|
#print confdir
|
2006-08-02 23:44:10 +01:00
|
|
|
userconfdir = sp.GetUserConfigDir()
|
|
|
|
userconfdir += "/"
|
|
|
|
if sys.platform.startswith("lin"):
|
|
|
|
userconfdir += "."
|
|
|
|
userconfdir += "scyther"
|
2006-08-08 16:00:20 +01:00
|
|
|
#print userconfdir
|
2006-08-02 23:44:10 +01:00
|
|
|
|
|
|
|
preflocs = [confdir,userconfdir]
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
prefs = Preferences()
|
|
|
|
prefs.load("")
|
|
|
|
|
|
|
|
|
|
|
|
def get(key,alt=None):
|
|
|
|
global prefs
|
|
|
|
|
2020-10-27 21:09:03 +00:00
|
|
|
if key in list(prefs.keys()):
|
2006-08-02 13:59:57 +01:00
|
|
|
return prefs[key]
|
|
|
|
else:
|
|
|
|
return alt
|
|
|
|
|
2012-04-25 16:03:51 +01:00
|
|
|
def getkeys():
|
|
|
|
global prefs
|
|
|
|
|
2020-10-27 21:09:03 +00:00
|
|
|
return list(prefs.keys())
|
2012-04-25 16:03:51 +01:00
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
def set(key,value):
|
|
|
|
global prefs
|
|
|
|
|
|
|
|
prefs[key]=value
|
|
|
|
return
|
|
|
|
|
|
|
|
def save():
|
|
|
|
global prefs
|
|
|
|
|
|
|
|
prefs.save()
|
|
|
|
|
2008-08-27 09:02:20 +01:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# vim: set ts=4 sw=4 et list lcs=tab\:>-:
|