Mercurial > fife-parpg
changeset 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 | 678fd71c0753 |
children | 8c9cdcc9bc4f |
files | engine/python/fife/extensions/fife_settings.py engine/python/fife/extensions/serializers/simplexml.py |
diffstat | 2 files changed, 59 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/engine/python/fife/extensions/fife_settings.py Tue Sep 21 20:49:08 2010 +0000 +++ b/engine/python/fife/extensions/fife_settings.py Wed Sep 22 14:05:33 2010 +0000 @@ -87,7 +87,7 @@ screen_height = settings.get("FIFE", "ScreenHeight", 768) """ - def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True): + def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True, serializer=None): """ Initializes the Setting object. @@ -103,6 +103,8 @@ of the settings dialog box. @param copy_dist: Copies the settings-dist.xml file to the settings_file location. If this is False it will create a new empty xml file. + @param serializer: Overrides the default XML serializer + @type serializer: C{SimpleSerializer} """ self._app_name = app_name @@ -113,8 +115,6 @@ # Holds SettingEntries self._entries = {} - self._xmlserializer = None - if self._settings_file == "": self._settings_file = "settings.xml" self._appdata = getUserDataDirectory("fife", self._app_name) @@ -141,9 +141,14 @@ #Used to stylize the options gui self._gui_style = "default" - #Initialize the XML serializer - self.loadSettings() - + #Initialize the serializer + if serializer: + self._serializer = serializer + else: + self._serializer = SimpleXMLSerializer() + + self._serializer.load(os.path.join(self._appdata, self._settings_file)) + self._initDefaultSettingEntries() def _initDefaultSettingEntries(self): @@ -198,13 +203,10 @@ print "WARNING:", entry.module, ":", entry.name, "still not found!" print "It's probably missing in settings-dist.xml as well!" - def loadSettings(self): - self._xmlserializer = SimpleXMLSerializer(os.path.join(self._appdata, self._settings_file)) - def saveSettings(self): """ Writes the settings to the settings file """ - if self._xmlserializer: - self._xmlserializer.save() + if self._serializer: + self._serializer.save() def get(self, module, name, defaultValue=None): """ Gets the value of a specified setting @@ -214,8 +216,8 @@ @param defaultValue: Specifies the default value to return if the setting is not found @type defaultValue: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} """ - if self._xmlserializer: - return self._xmlserializer.get(module, name, defaultValue) + if self._serializer: + return self._serializer.get(module, name, defaultValue) else: return None @@ -230,8 +232,8 @@ @param extra_attrs: Extra attributes to be stored in the XML-file @type extra_attrs: C{dict} """ - if self._xmlserializer: - self._xmlserializer.set(module, name, value, extra_attrs) + if self._serializer: + self._serializer.set(module, name, value, extra_attrs) def setGuiStyle(self, style): """ Set a custom gui style used for the option dialog. @@ -333,7 +335,7 @@ """ shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) self.changesRequireRestart = True - self.loadSettings() + self.initSerializer() #self._showChangeRequireRestartDialog() #if self.OptionsDlg:
--- a/engine/python/fife/extensions/serializers/simplexml.py Tue Sep 21 20:49:08 2010 +0000 +++ b/engine/python/fife/extensions/serializers/simplexml.py Wed Sep 22 14:05:33 2010 +0000 @@ -34,7 +34,28 @@ </Settings> """ -class SimpleXMLSerializer(object): +class SimpleSerializer(object): + """ + Use this as a base class for custom setting loaders/savers to use with the Setting class. + """ + + def __init__(self, filename=None): + pass + + def get(self, module, name, defaultValue=None): + pass + + def set(self, module, name, value, extra_attrs={}): + pass + + def load(self, filename=None): + pass + + def save(self, filename=None): + pass + + +class SimpleXMLSerializer(SimpleSerializer): """ This class is a simple interface to get and store data in XML files. @@ -49,8 +70,7 @@ self._tree = None self._root_element = None - if self._file: - self.load(self._file) + self._initialized = False def load(self, filename=None): """ @@ -58,6 +78,9 @@ Raises a SerializerError exception if the file is not specified. + @param filename: The file to load + @type filename: C{str} + @note: If the file does not exist it will automatically create a blank file for you. """ if filename: @@ -79,8 +102,15 @@ """ Saves the XML file. + @param filename: The file to save + @type filename: C{str} + @note: This Overwrites the file if it exists. """ + if not self._initialized: + self.load() + self._initialized = True + if filename: savefile = filename else: @@ -102,6 +132,10 @@ @param defaultValue: Specifies the default value to return if the variable is not found @type defaultValue: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict} """ + if not self._initialized: + self.load() + self._initialized = True + if not isinstance(name, str) and not isinstance(name, unicode): raise AttributeError("SimpleXMLSerializer.get(): Invalid type for name argument.") @@ -162,6 +196,10 @@ @param extra_attrs: Extra attributes to be stored in the XML-file @type extra_attrs: C{dict} """ + if not self._initialized: + self.load() + self._initialized = True + if not isinstance(name, str) and not isinstance(name, unicode): raise AttributeError("SimpleXMLSerializer.set(): Invalid type for name argument.")