scyther/gui/Gui/Preference.py

169 lines
3.8 KiB
Python
Raw Normal View History

2006-08-02 13:59:57 +01:00
#!/usr/bin/python
"""
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
import sys
2006-08-02 13:59:57 +01:00
from time import localtime,strftime
#---------------------------------------------------------------------------
""" Import scyther-gui components """
#---------------------------------------------------------------------------
""" Globals """
# Do we have the Python Imaging library?
havePIL = True
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"
preflocs = []
2006-08-02 13:59:57 +01:00
#---------------------------------------------------------------------------
def usePIL():
"""
Determine whether or not we should use the PIL library
"""
global havePIL
# Only if we have it, and it is windows.
if havePIL:
if sys.platform.startswith("lin"):
return True
return False
#---------------------------------------------------------------------------
2006-08-02 13:59:57 +01:00
class Preferences(dict):
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")
#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:
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):
print "Preferences:"
for k in self.keys():
print "%s=%s" % (k, self[k])
def save(self):
print "Saving preferences"
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
"""
global prefs,preflocs
sp = wx.StandardPaths.Get()
confdir = sp.GetConfigDir()
confdir += "/scyther"
#print confdir
userconfdir = sp.GetUserConfigDir()
userconfdir += "/"
if sys.platform.startswith("lin"):
userconfdir += "."
userconfdir += "scyther"
#print userconfdir
preflocs = [confdir,userconfdir]
2006-08-02 13:59:57 +01:00
prefs = Preferences()
prefs.load("")
def get(key,alt=None):
global prefs
if prefs.has_key(key):
return prefs[key]
else:
return alt
def set(key,value):
global prefs
prefs[key]=value
return
def save():
global prefs
prefs.save()