comparison engine/python/fife/extensions/fife_settings.py @ 614:567d53c1c010

Added the ability to specify the default settings filename. Also added the serializer get property.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 27 Sep 2010 14:02:59 +0000
parents 867aad1c01cd
children 6f3f5686a56e
comparison
equal deleted inserted replaced
613:8c9cdcc9bc4f 614:567d53c1c010
25 Settings 25 Settings
26 ================================== 26 ==================================
27 27
28 This module provides a nice framework for loading and saving game settings. 28 This module provides a nice framework for loading and saving game settings.
29 It is by no means complete but it does provide a good starting point. 29 It is by no means complete but it does provide a good starting point.
30
31 @note: Please note that you MUST provide a default settings-dist.xml file
32 in the root directory of your project for this module to function correctly.
33 """ 30 """
34 31
35 import shutil 32 import shutil
36 import os 33 import os
37 from StringIO import StringIO 34 from StringIO import StringIO
85 settings = Setting(app_name="myapp") 82 settings = Setting(app_name="myapp")
86 screen_width = settings.get("FIFE", "ScreenWidth", 1024) 83 screen_width = settings.get("FIFE", "ScreenWidth", 1024)
87 screen_height = settings.get("FIFE", "ScreenHeight", 768) 84 screen_height = settings.get("FIFE", "ScreenHeight", 768)
88 """ 85 """
89 86
90 def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True, serializer=None): 87 def __init__(self, app_name="", settings_file="", default_settings_file= "settings-dist.xml", settings_gui_xml="", changes_gui_xml="", copy_dist=True, serializer=None):
91 """ 88 """
92 Initializes the Setting object. 89 Initializes the Setting object.
93 90
94 @param app_name: The applications name. If this parameter is provided 91 @param app_name: The applications name. If this parameter is provided
95 alone it will try to read the settings file from the users home directory. 92 alone it will try to read the settings file from the users home directory.
97 @type app_name: C{string} 94 @type app_name: C{string}
98 @param settings_file: The name of the settings file. If this parameter is 95 @param settings_file: The name of the settings file. If this parameter is
99 provided it will look for the setting file as you specify it, first looking 96 provided it will look for the setting file as you specify it, first looking
100 in the working directory. It will NOT look in the users home directory. 97 in the working directory. It will NOT look in the users home directory.
101 @type settings_file: C{string} 98 @type settings_file: C{string}
99 @param default_settings_file: The name of the default settings file. If the settings_file
100 does not exist this file will be copied into the place of the settings_file. This file
101 must exist in the root directory of your project!
102 @type default_settings_file: C{string}
102 @param settings_gui_xml: If you specify this parameter you can customize the look 103 @param settings_gui_xml: If you specify this parameter you can customize the look
103 of the settings dialog box. 104 of the settings dialog box.
104 @param copy_dist: Copies the settings-dist.xml file to the settings_file location. If 105 @param copy_dist: Copies the default settings file to the settings_file location. If
105 this is False it will create a new empty xml file. 106 this is False it will create a new empty setting file.
106 @param serializer: Overrides the default XML serializer 107 @param serializer: Overrides the default XML serializer
107 @type serializer: C{SimpleSerializer} 108 @type serializer: C{SimpleSerializer}
108 109
109 """ 110 """
110 self._app_name = app_name 111 self._app_name = app_name
111 self._settings_file = settings_file 112 self._settings_file = settings_file
113 self._default_settings_file = default_settings_file
112 self._settings_gui_xml = settings_gui_xml 114 self._settings_gui_xml = settings_gui_xml
113 self._changes_gui_xml = changes_gui_xml 115 self._changes_gui_xml = changes_gui_xml
114 116
115 # Holds SettingEntries 117 # Holds SettingEntries
116 self._entries = {} 118 self._entries = {}
129 if self._changes_gui_xml == "": 131 if self._changes_gui_xml == "":
130 self._changes_gui_xml = CHANGES_REQUIRE_RESTART 132 self._changes_gui_xml = CHANGES_REQUIRE_RESTART
131 133
132 134
133 if not os.path.exists(os.path.join(self._appdata, self._settings_file)): 135 if not os.path.exists(os.path.join(self._appdata, self._settings_file)):
134 if os.path.exists('settings-dist.xml') and copy_dist: 136 if os.path.exists(self._default_settings_file) and copy_dist:
135 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) 137 shutil.copyfile(self._default_settings_file, os.path.join(self._appdata, self._settings_file))
136 138
137 #default settings 139 #default settings
138 self._resolutions = ['640x480', '800x600', '1024x768', '1280x800', '1440x900'] 140 self._resolutions = ['640x480', '800x600', '1024x768', '1280x800', '1440x900']
139 self._renderbackends = ['OpenGL', 'SDL'] 141 self._renderbackends = ['OpenGL', 'SDL']
140 142
145 if serializer: 147 if serializer:
146 self._serializer = serializer 148 self._serializer = serializer
147 else: 149 else:
148 self._serializer = SimpleXMLSerializer() 150 self._serializer = SimpleXMLSerializer()
149 151
152 self.initSerializer()
153
154 self._initDefaultSettingEntries()
155
156 def initSerializer(self):
150 self._serializer.load(os.path.join(self._appdata, self._settings_file)) 157 self._serializer.load(os.path.join(self._appdata, self._settings_file))
151 158
152 self._initDefaultSettingEntries()
153
154 def _initDefaultSettingEntries(self): 159 def _initDefaultSettingEntries(self):
155 """Initializes the default fife setting entries. Not to be called from 160 """Initializes the default fife setting entries. Not to be called from
156 outside this class.""" 161 outside this class."""
157 self.createAndAddEntry(FIFE_MODULE, "PlaySounds", "enable_sound", 162 self.createAndAddEntry(FIFE_MODULE, "PlaySounds", "enable_sound",
158 requiresrestart=True) 163 requiresrestart=True)
199 print "Updating", self._settings_file, "to the default, it is missing the entry:"\ 204 print "Updating", self._settings_file, "to the default, it is missing the entry:"\
200 , entry.name ,"for module", entry.module 205 , entry.name ,"for module", entry.module
201 self.setDefaults() 206 self.setDefaults()
202 if self.get(entry.module, entry.name) is None: 207 if self.get(entry.module, entry.name) is None:
203 print "WARNING:", entry.module, ":", entry.name, "still not found!" 208 print "WARNING:", entry.module, ":", entry.name, "still not found!"
204 print "It's probably missing in settings-dist.xml as well!" 209
205 210
206 def saveSettings(self): 211 def saveSettings(self, filename=""):
207 """ Writes the settings to the settings file """ 212 """ Writes the settings to the settings file
213
214 @param filename: Specifies the file to save the settings to. If it is not specified
215 the original settings file is used.
216 @type filename: C{string}
217 """
208 if self._serializer: 218 if self._serializer:
209 self._serializer.save() 219 if filename == "":
220 self._serializer.save(os.path.join(self._appdata, self._settings_file))
221 else:
222 self._serializer.save(filename)
210 223
211 def get(self, module, name, defaultValue=None): 224 def get(self, module, name, defaultValue=None):
212 """ Gets the value of a specified setting 225 """ Gets the value of a specified setting
213 226
214 @param module: Name of the module to get the setting from 227 @param module: Name of the module to get the setting from
329 """ 342 """
330 self._resolutions = reslist 343 self._resolutions = reslist
331 344
332 def setDefaults(self): 345 def setDefaults(self):
333 """ 346 """
334 Overwrites the setting file with the default settings-dist.xml file. 347 Overwrites the setting file with the default settings file.
335 """ 348 """
336 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) 349 shutil.copyfile(self._default_settings_file, os.path.join(self._appdata, self._settings_file))
337 self.changesRequireRestart = True 350 self.changesRequireRestart = True
338 self.initSerializer() 351 self.initSerializer()
339 #self._showChangeRequireRestartDialog() 352 #self._showChangeRequireRestartDialog()
340 353
341 #if self.OptionsDlg: 354 #if self.OptionsDlg:
344 def _getEntries(self): 357 def _getEntries(self):
345 return self._entries 358 return self._entries
346 359
347 def _setEntries(self, entries): 360 def _setEntries(self, entries):
348 self._entries = entries 361 self._entries = entries
362
363 def _getSerializer(self):
364 return self._serializer
349 365
350 entries = property(_getEntries, _setEntries) 366 entries = property(_getEntries, _setEntries)
351 367 serializer = property(_getSerializer)
352
353 368
354 class SettingEntry(object): 369 class SettingEntry(object):
355 370
356 def __init__(self, module, name, widgetname, applyfunction=None, initialdata=None, requiresrestart=False): 371 def __init__(self, module, name, widgetname, applyfunction=None, initialdata=None, requiresrestart=False):
357 """ 372 """