changeset 569:466d76db9701

Some small code cleanups in the extensions. The SimpleXMLSerializer now makes use of exceptions.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 28 Jun 2010 19:28:53 +0000
parents bfbf329e1da8
children 0e0a30f0eb20
files engine/python/fife/extensions/__init__.py engine/python/fife/extensions/fife_settings.py engine/python/fife/extensions/serializers/__init__.py engine/python/fife/extensions/serializers/simplexml.py
diffstat 4 files changed, 24 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/engine/python/fife/extensions/__init__.py	Mon Jun 28 18:43:03 2010 +0000
+++ b/engine/python/fife/extensions/__init__.py	Mon Jun 28 19:28:53 2010 +0000
@@ -5,10 +5,12 @@
 	'fife_compat',
 	'fife_timer',
 	'fife_utils',
+	'fife_settings',
 	'fifelog',
 	'filebrowser',
 	'pythonize',
 	'savers',
+	'loaders',
 	'soundmanager',
 	'fife_math'
 ]
--- a/engine/python/fife/extensions/fife_settings.py	Mon Jun 28 18:43:03 2010 +0000
+++ b/engine/python/fife/extensions/fife_settings.py	Mon Jun 28 19:28:53 2010 +0000
@@ -40,11 +40,6 @@
 from fife.extensions.fife_utils import getUserDataDirectory
 from fife.extensions.serializers.simplexml import SimpleXMLSerializer
 
-try:
-	import xml.etree.cElementTree as ET
-except:
-	import xml.etree.ElementTree as ET
-
 SETTINGS_GUI_XML="""\
 <Window name="Settings" title="Settings">
 	<Label text="Settings menu!" />
--- a/engine/python/fife/extensions/serializers/__init__.py	Mon Jun 28 18:43:03 2010 +0000
+++ b/engine/python/fife/extensions/serializers/__init__.py	Mon Jun 28 19:28:53 2010 +0000
@@ -50,12 +50,12 @@
 	print 'Warning (%s): %s' % (self.filename, msg)
 
 def root_subfile(masterfile, subfile):
-	'''
+	"""
 	Returns new path for given subfile (path), which is rooted against masterfile
 	E.g. if masterfile is ./../foo/bar.xml and subfile is ./../foo2/subfoo.xml,
 	returned path is ../foo2/subfoo.xml
 	NOTE: masterfile is expected to be *file*, not directory. subfile can be either
-	'''
+	"""
 	s = '/'
 
 	masterfile = norm_path(os.path.abspath(masterfile))
@@ -82,13 +82,13 @@
 	return pathstr
 
 def reverse_root_subfile(masterfile, subfile):
-	'''
+	"""
 	does inverse operation to root_subfile. E.g. 
 	E.g. if masterfile is ./../foo/bar.xml and subfile is ../foo2/subfoo.xml,
 	returned path ./../foo2/subfoo.xml
 	Usually this function is used to convert saved paths into engine relative paths
 	NOTE: masterfile is expected to be *file*, not directory. subfile can be either
-	'''
+	"""
 	s = '/'
 
 	masterfile = norm_path(os.path.abspath(masterfile)).split(s)[:-1]
@@ -97,10 +97,10 @@
 	return root_subfile(masterfile, subfile)
 
 def norm_path(path):
-	'''
+	"""
 	Makes the path use '/' delimited separators. FIFE always uses these delimiters, but some os-related
   routines will default to os.path.sep.
-	'''
+	"""
 	if os.path.sep == '/':
 		return path
 
--- a/engine/python/fife/extensions/serializers/simplexml.py	Mon Jun 28 18:43:03 2010 +0000
+++ b/engine/python/fife/extensions/serializers/simplexml.py	Mon Jun 28 19:28:53 2010 +0000
@@ -23,11 +23,8 @@
 
 import os
 from StringIO import StringIO
-
-try:
-	import xml.etree.cElementTree as ET
-except:
-	import xml.etree.ElementTree as ET
+	
+from fife.extensions.serializers import ET, SerializerError, InvalidFormat, NotFound
 
 
 EMPTY_XML_FILE="""\
@@ -59,14 +56,15 @@
 		"""
 		Loads the XML file into memory and validates it.
 		
+		Raises a SerializerError exception if the file is not specified.
+		
 		@note: If the file does not exist it will automatically create a blank file for you.
 		"""
 		if filename:
 			self._file = filename
 		
 		if not self._file:
-			print "Cannot load file.  No filename specified!"
-			return
+			raise SerializerError("Cannot load file or create file.  No filename specified!")
 		
 		if not os.path.exists(self._file):
 			self._tree = ET.parse(StringIO(EMPTY_XML_FILE))
@@ -89,8 +87,7 @@
 			savefile = self._file
 				
 		if not savefile:
-			print "Cannot save file.  No filename specified!"
-			return
+			raise SerializerError("Cannot save file.  No filename specified!")
 	
 		""" Writes the settings to file """
 		self._indent(self._root_element)
@@ -166,7 +163,7 @@
 		@type extra_attrs: C{dict}
 		"""
 		if not isinstance(name, str) and not isinstance(name, unicode):
-			raise AttributeError("Settings:set: Invalid type for name argument.")
+			raise AttributeError("SimpleXMLSerializer.set(): Invalid type for name argument.")
 
 		moduleTree = self._getModuleTree(module)
 		e_type = "str"
@@ -207,20 +204,22 @@
 			elm.text = value
 
 	def _validateTree(self):
-		""" Iterates the XML tree and prints warning when an invalid tag is found """
+		""" 
+		Iterates the XML tree and prints warning when an invalid tag is found.
+		
+		Raises an InvalidFormat exception if there is a format error.
+		"""
 		for c in self._root_element.getchildren():
 			if c.tag != "Module":
-				print "Invalid tag in " + self._file + ". Expected Module, got: ", c.tag
+				raise InvalidFormat("Invalid tag in " + self._file + ". Expected Module, got: " + c.tag)
 			elif c.get("name", "") == "":
-				print "Invalid tag in " + self._file + ". Module name is empty."
+				raise InvalidFormat("Invalid tag in " + self._file + ". Module name is empty.")
 			else:
 				for e in c.getchildren():
 					if e.tag != "Setting":
-						print "Invalid tag in " + self._file + " in module: ",c.tag,
-						print ". Expected Setting, got: ", e.tag
+						raise InvalidFormat("Invalid tag in " + self._file + " in module: " + c.tag + ". Expected Setting, got: " + e.tag)
 					elif c.get("name", "") == "":
-						print "Invalid tag in " + self._file + " in module: ",c.tag,
-						print ". Setting name is empty", e.tag
+						raise InvalidFormat("Invalid tag in " + self._file + " in module: " + c.tag + ". Setting name is empty" + e.tag)
 						
 	def _getModuleTree(self, module):
 		"""