# HG changeset patch # User k0s # Date 1257480937 18000 # Node ID b8e5471794b271c7dd8d7e4018282f12d2e723ff # Parent d9519f40c1770d7a3a886fa00d63e36ce55968a9 move to a dispatcher architecture diff -r d9519f40c177 -r b8e5471794b2 genshi_view/template/+package+/+package+.py_tmpl --- a/genshi_view/template/+package+/+package+.py_tmpl Thu Nov 05 23:11:45 2009 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -""" -request dispatcher -""" - -from handlers import Index - -from genshi.template import TemplateLoader -from paste.fileapp import FileApp -from pkg_resources import resource_filename -from webob import Request, Response, exc - -class Dispatcher(object): - - ### class level variables - defaults = { 'auto_reload': 'False'} - - def __init__(self, **kw): - - # set instance parameters from kw and defaults - for key in self.defaults: - setattr(self, key, kw.get(key, self.defaults[key])) -# self.auto_reload = self.auto_reload.lower() == 'true' - - self.handlers = [ Index ] - - # static files - self.htdocs = resource_filename(__name__, 'static') - - # template loader # to move??? -# templates_dir = resource_filename(__name__, 'templates') -# self.loader = TemplateLoader(templates_dir, -# auto_reload=self.auto_reload) - - ### methods dealing with HTTP - def __call__(self, environ, start_response): - request = Request(environ) - - # serve static files - # TODO: put into a handler - filename = os.path.join(self.htdocs, request.path_info) - if request.path_info != '/' and os.path.exists(filename): - fileapp = FileApp(filename) - return fileapp(environ, start_response) - - # match the request to a handler - for h in self.handlers: - handler = h.match(request) - if handler is not None: - break - else: - handler = exc.HTTPNotFound - - res = handler() - return res(environ, start_response) - diff -r d9519f40c177 -r b8e5471794b2 genshi_view/template/+package+/dispatcher.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/genshi_view/template/+package+/dispatcher.py Thu Nov 05 23:15:37 2009 -0500 @@ -0,0 +1,55 @@ +""" +request dispatcher +""" + +from handlers import Index + +from genshi.template import TemplateLoader +from paste.fileapp import FileApp +from pkg_resources import resource_filename +from webob import Request, Response, exc + +class Dispatcher(object): + + ### class level variables + defaults = { 'auto_reload': 'False'} + + def __init__(self, **kw): + + # set instance parameters from kw and defaults + for key in self.defaults: + setattr(self, key, kw.get(key, self.defaults[key])) +# self.auto_reload = self.auto_reload.lower() == 'true' + + self.handlers = [ Index ] + + # static files + self.htdocs = resource_filename(__name__, 'static') + + # template loader # to move??? +# templates_dir = resource_filename(__name__, 'templates') +# self.loader = TemplateLoader(templates_dir, +# auto_reload=self.auto_reload) + + ### methods dealing with HTTP + def __call__(self, environ, start_response): + request = Request(environ) + + # serve static files + # TODO: put into a handler + filename = os.path.join(self.htdocs, request.path_info) + if request.path_info != '/' and os.path.exists(filename): + fileapp = FileApp(filename) + return fileapp(environ, start_response) + + # match the request to a handler + for h in self.handlers: + handler = h.match(request) + if handler is not None: + break + else: + handler = exc.HTTPNotFound + + res = handler() + return res(environ, start_response) + diff -r d9519f40c177 -r b8e5471794b2 genshi_view/template/+package+/factory.py_tmpl --- a/genshi_view/template/+package+/factory.py_tmpl Thu Nov 05 23:11:45 2009 -0500 +++ b/genshi_view/template/+package+/factory.py_tmpl Thu Nov 05 23:15:37 2009 -0500 @@ -1,12 +1,13 @@ import os -from ${package} import ${project.title()}View +from dispatcher import Dispatcher from paste.httpexceptions import HTTPExceptionHandler from paste.urlparser import StaticURLParser from pkg_resources import resource_filename class PassthroughFileserver(object): """serve files if they exist""" + # XXX needed??? def __init__(self, app, directory): self.app = app @@ -26,6 +27,6 @@ args = dict([(key.split(keystr, 1)[-1], value) for key, value in app_conf.items() if key.startswith(keystr) ]) - app = ${project.title()}View(**args) + app = DispatcherView(**args) return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static'))) diff -r d9519f40c177 -r b8e5471794b2 setup.py --- a/setup.py Thu Nov 05 23:11:45 2009 -0500 +++ b/setup.py Thu Nov 05 23:15:37 2009 -0500 @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '0.1.1' +version = '0.2' setup(name='genshi_view', version=version,