Add a Settings service class
This commit is contained in:
@@ -113,6 +113,7 @@ class MainFrame(wx.Frame):
|
||||
|
||||
def ExitApp(self, evt):
|
||||
try:
|
||||
service.Settings.getInstance().saveAll()
|
||||
self.Close()
|
||||
except PyDeadObjectError:
|
||||
pass
|
||||
|
||||
@@ -3,4 +3,5 @@ from service.fit import Fit
|
||||
from service.attribute import Attribute
|
||||
from service.character import Character
|
||||
from service.damagePattern import DamagePattern
|
||||
import service.prefetch
|
||||
from service.settings import Settings
|
||||
import service.prefetch
|
||||
|
||||
90
service/settings.py
Executable file
90
service/settings.py
Executable file
@@ -0,0 +1,90 @@
|
||||
#===============================================================================
|
||||
# Copyright (C) 2010 Diego Duclos
|
||||
#
|
||||
# This file is part of pyfa.
|
||||
#
|
||||
# pyfa 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 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# pyfa 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 pyfa. If not, see <http://www.gnu.org/licenses/>.
|
||||
#===============================================================================
|
||||
|
||||
import cPickle
|
||||
import os.path
|
||||
import config
|
||||
|
||||
class SettingsProvider():
|
||||
BASE_PATH = os.path.join(config.homePath, "settings")
|
||||
settings = {}
|
||||
_instance = None
|
||||
@classmethod
|
||||
def getInstance(cls):
|
||||
if cls._instance == None:
|
||||
cls.instance = Settings()
|
||||
|
||||
return cls.instance
|
||||
|
||||
def __init__(self):
|
||||
if not os.path.exists(self.BASE_PATH):
|
||||
os.mkdir(self.BASE_PATH);
|
||||
|
||||
def getSettings(self, area, defaults=None):
|
||||
s = self.settings.get(area)
|
||||
if s is None:
|
||||
p = os.path.join(self.BASE_PATH, area)
|
||||
if not os.path.exists(p):
|
||||
return
|
||||
|
||||
f = open(p, "rb")
|
||||
info = cPickle.load(f)
|
||||
self.settings[area] = s = Settings(p, info)
|
||||
|
||||
return s
|
||||
|
||||
def saveAll(self):
|
||||
for settings in self.settings.itervalues():
|
||||
settings.save()
|
||||
|
||||
class Settings():
|
||||
def __init__(self, location, info):
|
||||
self.location = location
|
||||
self.info = info
|
||||
|
||||
def save(self):
|
||||
f = open(self.location, "wb")
|
||||
cPickle.dump(self.info, f, cPickle.HIGHEST_PROTOCOL)
|
||||
|
||||
def __getitem__(self, k):
|
||||
return self.info[k]
|
||||
|
||||
def __setitem__(self, k, v):
|
||||
self.info[k] = v
|
||||
|
||||
def __iter__(self):
|
||||
return self.info.__iter__()
|
||||
|
||||
def iterkeys(self):
|
||||
return self.info.iterkeys()
|
||||
|
||||
def itervalues(self):
|
||||
return self.info.itervalues()
|
||||
|
||||
def iteritems(self):
|
||||
return self.info.iteritems()
|
||||
|
||||
def keys(self):
|
||||
return self.info.keys()
|
||||
|
||||
def values(self):
|
||||
return self.info.values()
|
||||
|
||||
def items(self):
|
||||
return self.info.items()
|
||||
Reference in New Issue
Block a user