Mercurial > fife-parpg
comparison engine/python/fife/extensions/fife_settings.py @ 612:867aad1c01cd
Added the ability to use a custom serializer with the fife.Setting class.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Wed, 22 Sep 2010 14:05:33 +0000 |
parents | 466d76db9701 |
children | 567d53c1c010 |
comparison
equal
deleted
inserted
replaced
611:678fd71c0753 | 612:867aad1c01cd |
---|---|
85 settings = Setting(app_name="myapp") | 85 settings = Setting(app_name="myapp") |
86 screen_width = settings.get("FIFE", "ScreenWidth", 1024) | 86 screen_width = settings.get("FIFE", "ScreenWidth", 1024) |
87 screen_height = settings.get("FIFE", "ScreenHeight", 768) | 87 screen_height = settings.get("FIFE", "ScreenHeight", 768) |
88 """ | 88 """ |
89 | 89 |
90 def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True): | 90 def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True, serializer=None): |
91 """ | 91 """ |
92 Initializes the Setting object. | 92 Initializes the Setting object. |
93 | 93 |
94 @param app_name: The applications name. If this parameter is provided | 94 @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. | 95 alone it will try to read the settings file from the users home directory. |
101 @type settings_file: C{string} | 101 @type settings_file: C{string} |
102 @param settings_gui_xml: If you specify this parameter you can customize the look | 102 @param settings_gui_xml: If you specify this parameter you can customize the look |
103 of the settings dialog box. | 103 of the settings dialog box. |
104 @param copy_dist: Copies the settings-dist.xml file to the settings_file location. If | 104 @param copy_dist: Copies the settings-dist.xml file to the settings_file location. If |
105 this is False it will create a new empty xml file. | 105 this is False it will create a new empty xml file. |
106 @param serializer: Overrides the default XML serializer | |
107 @type serializer: C{SimpleSerializer} | |
106 | 108 |
107 """ | 109 """ |
108 self._app_name = app_name | 110 self._app_name = app_name |
109 self._settings_file = settings_file | 111 self._settings_file = settings_file |
110 self._settings_gui_xml = settings_gui_xml | 112 self._settings_gui_xml = settings_gui_xml |
111 self._changes_gui_xml = changes_gui_xml | 113 self._changes_gui_xml = changes_gui_xml |
112 | 114 |
113 # Holds SettingEntries | 115 # Holds SettingEntries |
114 self._entries = {} | 116 self._entries = {} |
115 | 117 |
116 self._xmlserializer = None | |
117 | |
118 if self._settings_file == "": | 118 if self._settings_file == "": |
119 self._settings_file = "settings.xml" | 119 self._settings_file = "settings.xml" |
120 self._appdata = getUserDataDirectory("fife", self._app_name) | 120 self._appdata = getUserDataDirectory("fife", self._app_name) |
121 else: | 121 else: |
122 self._appdata = os.path.dirname(self._settings_file) | 122 self._appdata = os.path.dirname(self._settings_file) |
139 self._renderbackends = ['OpenGL', 'SDL'] | 139 self._renderbackends = ['OpenGL', 'SDL'] |
140 | 140 |
141 #Used to stylize the options gui | 141 #Used to stylize the options gui |
142 self._gui_style = "default" | 142 self._gui_style = "default" |
143 | 143 |
144 #Initialize the XML serializer | 144 #Initialize the serializer |
145 self.loadSettings() | 145 if serializer: |
146 | 146 self._serializer = serializer |
147 else: | |
148 self._serializer = SimpleXMLSerializer() | |
149 | |
150 self._serializer.load(os.path.join(self._appdata, self._settings_file)) | |
151 | |
147 self._initDefaultSettingEntries() | 152 self._initDefaultSettingEntries() |
148 | 153 |
149 def _initDefaultSettingEntries(self): | 154 def _initDefaultSettingEntries(self): |
150 """Initializes the default fife setting entries. Not to be called from | 155 """Initializes the default fife setting entries. Not to be called from |
151 outside this class.""" | 156 outside this class.""" |
196 self.setDefaults() | 201 self.setDefaults() |
197 if self.get(entry.module, entry.name) is None: | 202 if self.get(entry.module, entry.name) is None: |
198 print "WARNING:", entry.module, ":", entry.name, "still not found!" | 203 print "WARNING:", entry.module, ":", entry.name, "still not found!" |
199 print "It's probably missing in settings-dist.xml as well!" | 204 print "It's probably missing in settings-dist.xml as well!" |
200 | 205 |
201 def loadSettings(self): | |
202 self._xmlserializer = SimpleXMLSerializer(os.path.join(self._appdata, self._settings_file)) | |
203 | |
204 def saveSettings(self): | 206 def saveSettings(self): |
205 """ Writes the settings to the settings file """ | 207 """ Writes the settings to the settings file """ |
206 if self._xmlserializer: | 208 if self._serializer: |
207 self._xmlserializer.save() | 209 self._serializer.save() |
208 | 210 |
209 def get(self, module, name, defaultValue=None): | 211 def get(self, module, name, defaultValue=None): |
210 """ Gets the value of a specified setting | 212 """ Gets the value of a specified setting |
211 | 213 |
212 @param module: Name of the module to get the setting from | 214 @param module: Name of the module to get the setting from |
213 @param name: Setting name | 215 @param name: Setting name |
214 @param defaultValue: Specifies the default value to return if the setting is not found | 216 @param defaultValue: Specifies the default value to return if the setting is not found |
215 @type defaultValue: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} | 217 @type defaultValue: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} |
216 """ | 218 """ |
217 if self._xmlserializer: | 219 if self._serializer: |
218 return self._xmlserializer.get(module, name, defaultValue) | 220 return self._serializer.get(module, name, defaultValue) |
219 else: | 221 else: |
220 return None | 222 return None |
221 | 223 |
222 def set(self, module, name, value, extra_attrs={}): | 224 def set(self, module, name, value, extra_attrs={}): |
223 """ | 225 """ |
228 @param value: Value to assign to setting | 230 @param value: Value to assign to setting |
229 @type value: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} | 231 @type value: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} |
230 @param extra_attrs: Extra attributes to be stored in the XML-file | 232 @param extra_attrs: Extra attributes to be stored in the XML-file |
231 @type extra_attrs: C{dict} | 233 @type extra_attrs: C{dict} |
232 """ | 234 """ |
233 if self._xmlserializer: | 235 if self._serializer: |
234 self._xmlserializer.set(module, name, value, extra_attrs) | 236 self._serializer.set(module, name, value, extra_attrs) |
235 | 237 |
236 def setGuiStyle(self, style): | 238 def setGuiStyle(self, style): |
237 """ Set a custom gui style used for the option dialog. | 239 """ Set a custom gui style used for the option dialog. |
238 @param style: Pychan style to be used | 240 @param style: Pychan style to be used |
239 @type style: C{string} | 241 @type style: C{string} |
331 """ | 333 """ |
332 Overwrites the setting file with the default settings-dist.xml file. | 334 Overwrites the setting file with the default settings-dist.xml file. |
333 """ | 335 """ |
334 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) | 336 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) |
335 self.changesRequireRestart = True | 337 self.changesRequireRestart = True |
336 self.loadSettings() | 338 self.initSerializer() |
337 #self._showChangeRequireRestartDialog() | 339 #self._showChangeRequireRestartDialog() |
338 | 340 |
339 #if self.OptionsDlg: | 341 #if self.OptionsDlg: |
340 # self.OptionsDlg.hide() | 342 # self.OptionsDlg.hide() |
341 | 343 |