commit:022cd9cdfc0eb90a797d6ea4635f93b0a463b1da
author:Chip Black
committer:Chip Black
date:Sun Jul 27 12:55:40 2008 -0500
parents:9c26adc212efda5dfd8d38de1cfe8e6d31812f80
Refined Level environment and class loading
diff --git a/Richter/Level.py b/Richter/Level.py
line changes: +10/-15
index 61b37c4..20936bf
--- a/Richter/Level.py
+++ b/Richter/Level.py
@@ -7,19 +7,7 @@ from Surface import SurfaceSet
 from OpenGL.GL import *
 
 classpath = ['classes']
-for path in classpath:
-	sys.path.append(path)
-
-
-def findClass(classname):
-	"A fun bit of metaprogramming, loading a class based on its name"
-	mod = imp.find_module(classname, classpath)
-	try:
-		c = imp.load_module(classname, *mod)
-		mod[0].close()
-		return eval("c.%s" % classname)
-	finally:
-		mod[0].close()
+sys.path += classpath
 
 
 class Layer:
@@ -106,15 +94,22 @@ class Level:
 	def compileThings(self, layer, things):
 		for (name, t) in things.iteritems():
 			print "Creating", t['class'], name
-			c = findClass(t['class'])
+			self.loadClass(t['class'])
 			properties = {}
 			if t.has_key('properties'):
 				properties = t['properties']
 			properties['surfaces'] = layer.surfaces
-			self.env[name] = c(**properties)
+
+			self.env[name] = eval('%s(**properties)' % t['class'], self.env, {'properties': properties})
 			layer.add(self.env[name])
 
 
+	def loadClass(self, classname):
+		"""A fun bit of metaprogramming. Load a class definition into
+		our environment based on its name"""
+		exec "from %s import %s" % (classname, classname) in self.env
+
+
 	def addLayerBack(self, layer):
 		self.layers.insert(0, layer)