Nochmal Nachschlag, was die Fragen angeht...
Code: Alles auswählen
class ProfileManager(wx.Frame):
"""Profile Mananger frame."""
def __init__(self,profileName=None):
"""Initialize.
profileName -- current profile name (or None) - currently unused"""
#--Data
self.profileIni = os.path.join(settings['mwDir'],'profiles.ini')
self.hiddenProfileDir = os.path.join(settings['mwDir'],'Hidden Profiles')
self.saveDir = os.path.join(settings['mwDir'],'Saves')
self.config = ConfigParser.ConfigParser()
#--Hidden profile dir
if not os.path.exists(self.hiddenProfileDir):
os.mkdir(self.hiddenProfileDir)
#--Window
pos = settings['wrye.mash.modDocs.pos']
size = settings['wrye.mash.modDocs.size']
wx.Frame.__init__(self, mashFrame, -1, _('Profile Manager'), pos, size,
style=wx.DEFAULT_FRAME_STYLE)
self.SetBackgroundColour(wx.NullColour)
self.SetSizeHints(250,250)
#--Icons
self.SetIcons(images['wrye.mash.icons2'].GetIconBundle())
#--Doc fields
mainSizer = wx.BoxSizer(wx.VERTICAL)
#--Button Sizer
buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(buttonSizer,0,wx.GROW|wx.ALL^wx.BOTTOM,4)
#--Profile Text
self.profileText = wx.StaticText(self, -1, "Profile: ", wx.DefaultPosition,(-1,-1),
style=wx.ALIGN_LEFT)
buttonSizer.Add(self.profileText,0,wx.GROW)
#--Profile Name
self.profileNameBox = wx.ComboBox(self,ID_SELECT,'',wx.DefaultPosition,(-1,-1),
self.profileNames(),style=wx.CB_DROPDOWN|wx.CB_SORT|wx.ALIGN_LEFT)
wx.EVT_COMBOBOX(self.profileNameBox,ID_SELECT,self.listSaves)
buttonSizer.Add(self.profileNameBox,1,wx.GROW|wx.ALL)
#--Activate Profile
self.activateButton = wx.Button(self,ID_ACTIVATE_PROFILE,_("Set profile as active"))
wx.EVT_BUTTON(self.activateButton,ID_ACTIVATE_PROFILE,self.ActivateProfile)
buttonSizer.Add(self.activateButton,0,wx.GROW)
#--Saves sizer
savesSizer = wx.GridSizer(0,0)
mainSizer.Add(savesSizer,1,wx.GROW,0)
#--Saves display
self.savesList = wx.ListBox(self, -1, wx.DefaultPosition,(-1,-1),
style=wx.LB_SINGLE|wx.LB_NEEDED_SB)
savesSizer.Add(self.savesList,0,wx.GROW|wx.ALL^wx.BOTTOM)
#--Bottom status bar sizer
bottomStateSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(bottomStateSizer,0,wx.GROW|wx.BOTTOM,4)
#--Bottom status bar
self.bottomStateBar = wx.StatusBar(self, -1, style=wx.SB_NORMAL)
bottomStateSizer.Add(self.bottomStateBar,0,wx.GROW)
#--Show window
self.SetSizer(mainSizer)
self.Show()
self.CheckIniFile()
def CheckIniFile(self,event=None):
"""Compare data in profiles.ini to actual save games and update as needed."""
savesList=[]
if (not os.path.exists(self.profileIni)) or (os.path.isdir(self.profileIni)):
temp = open(self.profileIni,'w')
temp.close()
del temp
self.config.read(self.profileIni)
#--add stuff to profiles.ini
for item in os.listdir(self.saveDir):
if '.ess' in item:
savesList.append(item)
for item in savesList:
save = open(os.path.join(settings['mwDir'],'Saves',item),'rb')
savestr = save.read()
countnpc = savestr.count('NPC_')
counter = 0
start = 0
start2 = 0
while counter<countnpc:
start = savestr.find('NPC_',start,len(savestr))+20
save.seek(start)
length = struct.unpack('i',save.read(4))[0]
npcid = save.read(length)
if 'player' in npcid:
start2 = savestr.find('FNAM',start,len(savestr))+4
save.seek(start2)
length = struct.unpack('i',save.read(4))[0]
npcname = save.read(length-1)
if not self.config.has_section(npcname):
self.config.add_section(npcname)
self.config.set(npcname, 'status', 'not initialized')
options = self.config.items(npcname)
valueList = []
for pair in options:
if pair[0] != 'status':
valueList.append(pair[1])
if not item in valueList:
self.config.set(npcname,'save file '+str(len(valueList)),item)
counter+=1
#--remove stuff from profiles.ini
for profile in self.config.sections():
for option in self.config.options(profile):
if self.config.get(profile,'status') == 'inactive':
if option != 'status':
if (not os.path.exists(os.path.join(self.hiddenProfileDir,profile,self.config.get(profile,option)))) and (not os.path.exists(os.path.join(self.saveDir,profile,self.config.get(profile,option)))):
self.config.remove_option(profile,option)
if len(self.config.options(profile)) == 1:
self.config.remove_section(profile)
self.config.write(open(self.profileIni,'w'))
def ActivateProfile(self,event):
"""Handle "Activate Profile" button click."""
self.config.read(self.profileIni)
if not self.profileNameBox.GetValue():
ErrorMessage(self,'You need to select a profile to activate it.','Error: No profile selected!')
return
profile = self.profileNameBox.GetValue()
if self.config.get(profile,'status') == 'active':
ErrorMessage(self,'This profile is already active.','Error: Profile already active.')
return
for section in self.config.sections():
if str(profile) is not str(section):
savesList = []
for option in self.config.options(section):
if option == 'status':
pass
else:
savesList.append(self.config.get(section,option))
if not os.path.exists(os.path.join(self.hiddenProfileDir,section)):
os.mkdir(os.path.join(self.hiddenProfileDir,section))
for save in savesList:
if os.path.exists(os.path.join(self.saveDir,save)):
shutil.move(os.path.join(self.saveDir,save),os.path.join(self.hiddenProfileDir,section))
self.config.set(section,'status','inactive')
else:
if not os.path.exists(self.hiddenProfileDir):
break
if not os.path.exists(os.path.join(self.hiddenProfileDir,section)):
break
for save in os.listdir(os.path.join(self.hiddenProfileDir,section)):
shutil.move(os.path.join(self.hiddenProfileDir,section,save),self.saveDir)
self.config.set(section,'status','active')
self.config.write(open(self.profileIni,'w'))
self.listSaves()
def profileNames(self):
self.CheckIniFile()
self.config.read(self.profileIni)
return self.config.sections()
def listSaves(self,event=None):
self.config.read(self.profileIni)
saveList = []
for option in self.config.options(self.profileNameBox.GetValue()):
if option != 'status':
saveList.append(self.config.get(self.profileNameBox.GetValue(),
option))
self.savesList.Set(saveList)
self.bottomStateBar.SetStatusText('Profile "'+self.profileNameBox.GetValue()+'" is '+self.config.get(self.profileNameBox.GetValue(),
'status')+'.')
Das Problem liegt bei der Funktion ActivateProfile - in die INI-Datei wird immer 'inactive' beim status reingeschrieben - irgendeine Ahnung, warum? Ich hab's schon zigmal durchgelesen, mir fällt aber nichts auf. Danke schonmal.