Use a wx.FileConfig to store options, and use wx.StandardPaths to know

where to put it.  Also use this dir for the cache of modified sample
files.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@45929 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2007-05-09 22:58:44 +00:00
parent 9fbac28b2d
commit 824b3e0ab8

View File

@@ -775,11 +775,19 @@ class DemoCodePanel(wx.Panel):
def opj(path): def opj(path):
"""Convert paths to the platform-specific separator""" """Convert paths to the platform-specific separator"""
str = apply(os.path.join, tuple(path.split('/'))) st = apply(os.path.join, tuple(path.split('/')))
# HACK: on Linux, a leading / gets lost... # HACK: on Linux, a leading / gets lost...
if path.startswith('/'): if path.startswith('/'):
str = '/' + str st = '/' + st
return str return st
def GetDataDir():
"""
Return the standard location on this platform for application data
"""
sp = wx.StandardPaths.Get()
return sp.GetUserDataDir()
def GetModifiedDirectory(): def GetModifiedDirectory():
@@ -787,7 +795,7 @@ def GetModifiedDirectory():
Returns the directory where modified versions of the demo files Returns the directory where modified versions of the demo files
are stored are stored
""" """
return opj(wx.GetHomeDir() + "/.wxPyDemo/modified/") return os.path.join(GetDataDir(), "modified")
def GetModifiedFilename(name): def GetModifiedFilename(name):
@@ -796,7 +804,7 @@ def GetModifiedFilename(name):
""" """
if not name.endswith(".py"): if not name.endswith(".py"):
name = name + ".py" name = name + ".py"
return GetModifiedDirectory() + name return os.path.join(GetModifiedDirectory(), name)
def GetOriginalFilename(name): def GetOriginalFilename(name):
@@ -816,6 +824,15 @@ def DoesModifiedExist(name):
return False return False
def GetConfig():
if not os.path.exists(GetDataDir()):
os.makedirs(GetDataDir())
config = wx.FileConfig(
localFilename=os.path.join(GetDataDir(), "options"))
return config
def SearchDemo(name, keyword): def SearchDemo(name, keyword):
""" Returns whether a demo contains the search keyword or not. """ """ Returns whether a demo contains the search keyword or not. """
fid = open(GetOriginalFilename(name), "rt") fid = open(GetOriginalFilename(name), "rt")
@@ -1203,8 +1220,6 @@ class wxPythonDemo(wx.Frame):
except: except:
self.tbicon = None self.tbicon = None
wx.CallAfter(self.ShowTip)
self.otherWin = None self.otherWin = None
self.Bind(wx.EVT_IDLE, self.OnIdle) self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
@@ -1349,18 +1364,17 @@ class wxPythonDemo(wx.Frame):
def ReadConfigurationFile(self): def ReadConfigurationFile(self):
self.auiConfigurations = {} self.auiConfigurations = {}
optionsFile = opj(wx.GetHomeDir() + "/.wxPyDemo/wxPythonOptions.txt") self.expansionState = [0, 1]
if not os.path.isfile(optionsFile):
self.expansionState = [0, 1]
return
fid = open(optionsFile, "rt") config = GetConfig()
self.expansionState = eval(fid.readline().strip().split(":")[1]) val = config.Read('ExpansionState')
auiConfigurations = fid.readline().strip().split(":")[1:] if val:
auiConfigurations = ":".join(auiConfigurations) self.expansionState = eval(val)
self.auiConfigurations.update(eval(auiConfigurations))
fid.close()
val = config.Read('AUIPerspectives')
if val:
self.auiConfigurations = eval(val)
def BuildMenuBar(self): def BuildMenuBar(self):
@@ -1976,11 +1990,10 @@ class wxPythonDemo(wx.Frame):
if self.tbicon is not None: if self.tbicon is not None:
self.tbicon.Destroy() self.tbicon.Destroy()
optionsFile = opj(wx.GetHomeDir() + "/.wxPyDemo/wxPythonOptions.txt") config = GetConfig()
fid = open(optionsFile, "wt") config.Write('ExpansionState', str(self.expansionState))
fid.write("ExpansionState: %s\n"%self.tree.GetExpansionState()) config.Write('AUIPerspectives', str(self.auiConfigurations))
fid.write("AUIPerspectives: %s\n"%self.auiConfigurations) config.Flush()
fid.close()
self.mgr.UnInit() self.mgr.UnInit()
del self.mgr del self.mgr
@@ -1997,18 +2010,20 @@ class wxPythonDemo(wx.Frame):
#--------------------------------------------- #---------------------------------------------
def ShowTip(self): def ShowTip(self):
try: config = GetConfig()
showTipText = open(opj("data/showTips")).read() showTipText = config.Read("tips")
if showTipText:
showTip, index = eval(showTipText) showTip, index = eval(showTipText)
except IOError: else:
showTip, index = (1, 0) showTip, index = (1, 0)
if showTip: if showTip:
tp = wx.CreateFileTipProvider(opj("data/tips.txt"), index) tp = wx.CreateFileTipProvider(opj("data/tips.txt"), index)
##tp = MyTP(0) ##tp = MyTP(0)
showTip = wx.ShowTip(self, tp) showTip = wx.ShowTip(self, tp)
index = tp.GetCurrentTip() index = tp.GetCurrentTip()
open(opj("data/showTips"), "w").write(str( (showTip, index) )) config.Write("tips", str( (showTip, index) ))
config.Flush()
#--------------------------------------------- #---------------------------------------------
def OnDemoMenu(self, event): def OnDemoMenu(self, event):
@@ -2073,6 +2088,8 @@ class MySplashScreen(wx.SplashScreen):
frame.Show() frame.Show()
if self.fc.IsRunning(): if self.fc.IsRunning():
self.Raise() self.Raise()
wx.CallAfter(frame.ShowTip)
@@ -2124,7 +2141,8 @@ class MyApp(wx.App):
""" """
wx.SystemOptions.SetOptionInt("mac.window-plain-transition", 1) wx.SystemOptions.SetOptionInt("mac.window-plain-transition", 1)
self.SetAppName("wxPyDemo")
# For debugging # For debugging
#self.SetAssertMode(wx.PYAPP_ASSERT_DIALOG) #self.SetAssertMode(wx.PYAPP_ASSERT_DIALOG)