Mercurial > hg > SimpleWiki
changeset 1:4c83f7715993
[mq]: renderer
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 07 Sep 2010 18:11:04 -0700 |
parents | d5102c881cb5 |
children | 2464e2051b78 |
files | example/hello.html simplewiki.ini simplewiki/dispatcher.py simplewiki/handlers.py |
diffstat | 4 files changed, 40 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/hello.html Tue Sep 07 18:11:04 2010 -0700 @@ -0,0 +1,8 @@ +<html> +<head> +<title>Hello, world!</title> +</head> +<body> +Hello, world! +</body> +</html>
--- a/simplewiki.ini Tue Sep 07 17:50:04 2010 -0700 +++ b/simplewiki.ini Tue Sep 07 18:11:04 2010 -0700 @@ -19,4 +19,4 @@ [app:SimpleWiki] paste.app_factory = simplewiki.factory:factory -SimpleWiki.name = world \ No newline at end of file +SimpleWiki.directory = %(here)s/example \ No newline at end of file
--- a/simplewiki/dispatcher.py Tue Sep 07 17:50:04 2010 -0700 +++ b/simplewiki/dispatcher.py Tue Sep 07 18:11:04 2010 -0700 @@ -5,7 +5,7 @@ import os -from handlers import Index +from handlers import GenshiRenderer from genshi.template import TemplateLoader from paste.fileapp import FileApp @@ -17,8 +17,8 @@ ### class level variables defaults = { 'auto_reload': 'False', 'template_dirs': '', - 'app': None, - 'name': 'anonymous' } + 'name': 'anonymous', + 'directory': None } def __init__(self, **kw): @@ -27,12 +27,10 @@ setattr(self, key, kw.get(key, self.defaults[key])) self.auto_reload = self.auto_reload.lower() == 'true' - # request handlers - self.handlers = [ Index ] + assert self.directory and os.path.exists(self.directory), "Must specify an existing directory" - # endpoint app if used as middleware - if self.app: - assert hasattr(self.app, '__call__') + # request handlers + self.handlers = [ GenshiRenderer ] # template loader self.template_dirs = self.template_dirs.split() @@ -57,8 +55,6 @@ if handler is not None: break else: - if self.app: - return self.app(environ, start_response) handler = exc.HTTPNotFound # add navigation links to handler [example]
--- a/simplewiki/handlers.py Tue Sep 07 17:50:04 2010 -0700 +++ b/simplewiki/handlers.py Tue Sep 07 18:11:04 2010 -0700 @@ -3,6 +3,7 @@ these are instantiated for every request, then called """ +import os from urlparse import urlparse from webob import Response, exc @@ -49,10 +50,33 @@ def redirect(self, location): raise exc.HTTPSeeOther(location=location) -class GenshiHandler(Handler): +class GenshiRenderer(Handler): + + @classmethod + def match(cls, app, request): + + # check the method + if request.method not in cls.methods: + return None + + # check the path + path = request.environ['path'] + if not path: + return None + if not path[-1].endswith('.html'): + return None + + + try: + return cls(app, request) + except HandlerMatchException: + return None def __init__(self, app, request): Handler.__init__(self, app, request) + self.template = os.path.join(app.directory, *request.environ['path']) + if not os.path.exists(self.template): + raise HandlerMatchException self.data = { 'request': request, 'link': self.link } @@ -65,17 +89,3 @@ return Response(content_type='text/html', body=template.generate(**self.data).render('html')) -class Index(GenshiHandler): - template = 'index.html' - methods=set(['GET', 'POST']) - - def __init__(self, app, request): - GenshiHandler.__init__(self, app, request) - - def Get(self): - self.data['name'] = self.request.remote_user or self.app.name - return GenshiHandler.Get(self) - - def Post(self): - self.app.name = self.request.POST.get('name', self.app.name) - self.redirect(self.link(self.handler_path))