diff engine/python/fife/extensions/serializers/simplexml.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 0bbe6e8ad9c8
children 567d53c1c010
line wrap: on
line diff
--- 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.")