Refined Level environment and class loading
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:
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)