Skip to content

Instantly share code, notes, and snippets.

@ryonsherman
Created February 12, 2013 18:39
Show Gist options
  • Save ryonsherman/4772134 to your computer and use it in GitHub Desktop.
Save ryonsherman/4772134 to your computer and use it in GitHub Desktop.
Simple MVC/Template implementation of the GAE Framework using webapp2/jinja2.
application: webapp2_framework
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
version: latest
handlers:
- url: /.*
script: main.app
import os
import glob
import webapp2
class WSGIApplication(webapp2.WSGIApplication):
def __init__(self, *args, **kwargs):
super(WSGIApplication, self).__init__(*args, **kwargs)
self.router.set_dispatcher(self.__class__.dispatcher)
@staticmethod
def dispatcher(router, request, response):
dispatcher = router.default_dispatcher(request, response)
if isinstance(dispatcher, basestring):
response = webapp2.Response(dispatcher)
elif isinstance(dispatcher, tuple):
response = webapp2.Response(*dispatcher)
return response
def route(self, *args, **kwargs):
def wrapper(func):
self.router.add(webapp2.Route(handler=func, *args, **kwargs))
return func
return wrapper
app = WSGIApplication(debug=True)
routes = glob.glob("%s/routes/*.py" % os.path.dirname(__file__))
routes = [os.path.basename(f)[:-3] for f in routes]
for route in routes:
__import__("routes.%s" % route, fromlist=route)
import webapp2
from views import View
class MetaRoute(type):
def __new__(cls, *args):
_class = super(MetaRoute, cls).__new__(cls, *args)
_class.get = cls.get(_class.get)
return _class
@classmethod
def get(cls, func):
def wrapper(self, *args):
_return = func(self, *args)
self.response.out.write(self.view.render())
return _return
return wrapper
class Route(webapp2.RequestHandler):
__metaclass__ = MetaRoute
def __init__(self, *args, **kwargs):
super(Route, self).__init__(*args, **kwargs)
self.view = View()
def __getattribute__(self, name):
if name == 'get' and not self.view.template:
self.view.template = self.request.path
return super(Route, self).__getattribute__(name)
import os
import glob
import jinja2
views = os.path.dirname(__file__)
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader(views))
class View(object):
@property
def template(self):
return getattr(self, '_template', None)
@template.setter
def template(self, template):
# Remove leading forward-slash
if template.startswith('/'):
template = template[1:]
# Remove trailing forward-slash
if template.endswith('/'):
template = template[:-1]
# Append 'index' if needed
path = glob.glob("%s/%s" % (views, template))[0]
if not template or os.path.isdir(path):
template = "%s/index" % template
# Append '.html' if needed
if not template.endswith('.html'):
template = "%s.html" % template
# Load template
self._template = jinja.get_template(template)
def __init__(self, *args, **kwargs):
self.data = {}
def render(self):
return self.template.render(self.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment