Mercurial > hg > TagInTheMiddle
diff taginthemiddle/middleware.py @ 0:1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 04 May 2010 08:37:15 -0700 |
parents | |
children | 837cfc05d4d9 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taginthemiddle/middleware.py Tue May 04 08:37:15 2010 -0700 @@ -0,0 +1,86 @@ +""" +request dispatcher: +data persisting across requests should go here +""" + +import os + +from handlers import Index +from model import Tags + +from genshi.template import TemplateLoader +from lxmlmiddleware import LXMLMiddleware +from paste.fileapp import FileApp +from pkg_resources import resource_filename +from webob import Request, Response, exc + +# tags have: +# - a list of resources they're applied to +# for now just assume auth is on and that anyone authenticated may tag + +# e.g. a tags file +# foo = /bar /fleem/baz etc +# cats = /cats /blog/mycat + +class Tagger(LXMLMiddleware): + + # instance defaults + defaults = { 'auto_reload': 'False', + 'template_dirs': '', + 'patterns': '*' + 'tags_file': None, + 'url': 'tags' + 'post_url': '.tags' + } + + def __init__(self, app, **kw): + self.app = app + + # 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' + assert self.tags_file is not None + self.tags + self.patterns = self.patterns.strip().split() + + # request handlers + self.handlers = [ Cloud ] + + # template loader + self.template_dirs = self.template_dirs.split() + self.template_dirs.append(resource_filename(__name__, 'templates')) + self.loader = TemplateLoader(self.template_dirs, + auto_reload=self.auto_reload) + + def __call__(self, environ, start_response): + + # get a request object + request = Request(environ) + + # get the path + path = request.path_info.strip('/').split('/') + if path == ['']: + path = [] + request.environ['path'] = path + + # match the request to a handler + for h in self.handlers: + handler = h.match(self, request) + 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] + if hasattr(handler, 'data'): + handler.data.setdefault('links', []) + for h in self.handlers: + handler.data['links'].append((handler.link(h.handler_path), + h.__name__)) + + # get response + res = handler() + return res(environ, start_response)