0
+from google.appengine.ext import webapp
0
+from google.appengine.ext.webapp import template
0
+TEMPLATES = os.path.join(os.path.dirname(__file__), '..', '..', 'templates')
0
+class BaseHandlerMeta(type):
0
+ """This metaclass generates and installs filter methods."""
0
+ def __new__(cls, name, bases, attrs):
0
+ # If this isn't a subclass of BaseHandler, don't do anything special.
0
+ if name == 'BaseHandler' or not [c for c in bases if issubclass(c, BaseHandler)]:
0
+ return super(BaseHandlerMeta, cls).__new__(
0
+ cls, name, bases, attrs)
0
+ fqn = attrs['__module__'] + "." + name
0
+ if _handlers.has_key(fqn):
0
+ # Intercept all methods and add a before_x and after_x method.
0
+ for k,v in attrs.items():
0
+ if callable(v) and not (k.startswith('before_') or k.startswith('after_')):
0
+ getattr(s, 'before_' + k, lambda *args: None)()
0
+ getattr(s, 'after_' + k, lambda *args: None)()
0
+ new_class = type.__new__(cls, name, bases, attrs)
0
+ logging.debug("Created a %s/%s/%s/%s as %s",
0
+ str(cls), str(name), str(bases), str(attrs), str(new_class))
0
+ _handlers[fqn] = new_class
0
+ def __makeWrappedMethod(methodName, originalMethod):
0
+class BaseHandler(webapp.RequestHandler):
0
+ """Base class for all of your controllers."""
0
+ __metaclass__ = BaseHandlerMeta
0
+ def renderTemplate(self, name, args={}):
0
+ """Render the given named template under templates/"""
0
+ path = os.path.join(TEMPLATES, name)
0
+ self.response.out.write(template.render(path, args))
Comments
No one has commented yet.