comparison taginthemiddle/middleware.py @ 19:57ca873ed9bf default tip

make this a pluggable system and start reorging infrastructure for that
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 25 May 2010 07:48:53 -0700
parents 1701e5b0b0eb
children
comparison
equal deleted inserted replaced
18:3bf478cb3166 19:57ca873ed9bf
1 """ 1 """
2 request dispatcher: 2 apply and display tags via WSGI middleware;
3 data persisting across requests should go here 3 request dispatcher component
4 """ 4 """
5 5
6 import os 6 import os
7 7
8 from handlers import TagCloud, ViewTags, PostTags 8 from handlers import TagCloud, ViewTags, PostTags
9 from model import Tags
10 9
11 from genshi.template import TemplateLoader 10 from genshi.template import TemplateLoader
12 from lxmlmiddleware import LXMLMiddleware 11 from lxmlmiddleware import LXMLMiddleware
13 from paste.fileapp import FileApp 12 from paste.fileapp import FileApp
14 from pkg_resources import resource_filename 13 from pkg_resources import resource_filename
18 17
19 # instance defaults 18 # instance defaults
20 defaults = { 'auto_reload': 'False', 19 defaults = { 'auto_reload': 'False',
21 'template_dirs': '', 20 'template_dirs': '',
22 'patterns': '*', # glob pattern: all URLs by default 21 'patterns': '*', # glob pattern: all URLs by default
23 'tags_file': None,
24 'tags_url': 'tags', # base URL for viewing tags 22 'tags_url': 'tags', # base URL for viewing tags
25 'post_url': '.tags', # sub-URL for posting tags 23 'post_url': '.tags', # sub-URL for posting tags
26 'author': None, # for RSS 24 'author': None, # for RSS
25 'engine': None
27 } 26 }
28 27
29 def __init__(self, app, **kw): 28 def __init__(self, app, **kw):
30 self.app = app 29 self.app = app
31 30
32 # set instance parameters from kw and defaults 31 # set instance parameters from kw and defaults
33 for key in self.defaults: 32 for key in self.defaults:
34 setattr(self, key, kw.get(key, self.defaults[key])) 33 setattr(self, key, kw.get(key, self.defaults[key]))
35 self.auto_reload = self.auto_reload.lower() == 'true' 34 self.auto_reload = self.auto_reload.lower() == 'true'
36 assert self.tags_file is not None
37 self.tags = Tags(self.tags_file)
38 self.patterns = self.patterns.strip().split() 35 self.patterns = self.patterns.strip().split()
39 36
40 # request handlers 37 # request handlers
41 self.handlers = [ TagCloud, ViewTags, PostTags ] 38 self.handlers = [ TagCloud, ViewTags, PostTags ]
42 39
43 # template loader 40 # template loader
44 self.template_dirs = self.template_dirs.split() 41 self.template_dirs = self.template_dirs.split()
45 self.template_dirs.append(resource_filename(__name__, 'templates')) 42 self.template_dirs.append(resource_filename(__name__, 'templates'))
46 self.loader = TemplateLoader(self.template_dirs, 43 self.loader = TemplateLoader(self.template_dirs,
47 auto_reload=self.auto_reload) 44 auto_reload=self.auto_reload)
45
46 # tag engine
47 assert self.engine is not None, 'Must specify a tag.engine'
48 48
49 def __call__(self, environ, start_response): 49 def __call__(self, environ, start_response):
50 50
51 # get a request object 51 # get a request object
52 request = Request(environ) 52 request = Request(environ)