annotate taginthemiddle/middleware.py @ 8:fc3491dfe374

correct function signature for reading tags file
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 05 May 2010 22:16:25 -0700
parents 8fb20b780341
children 1701e5b0b0eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request dispatcher:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 data persisting across requests should go here
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
8 from handlers import TagCloud, ViewTags, PostTags
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 from model import Tags
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 from genshi.template import TemplateLoader
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from lxmlmiddleware import LXMLMiddleware
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 from paste.fileapp import FileApp
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 from pkg_resources import resource_filename
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 from webob import Request, Response, exc
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 class Tagger(LXMLMiddleware):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # instance defaults
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 defaults = { 'auto_reload': 'False',
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 'template_dirs': '',
6
8fb20b780341 correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
22 'patterns': '*', # glob pattern: all URLs by default
8fb20b780341 correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
23 'tags_file': None,
8fb20b780341 correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
24 'tags_url': 'tags', # base URL for viewing tags
8fb20b780341 correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
25 'post_url': '.tags', # sub-URL for posting tags
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
26 'author': None, # for RSS
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 }
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 def __init__(self, app, **kw):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 self.app = app
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 # set instance parameters from kw and defaults
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 for key in self.defaults:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 setattr(self, key, kw.get(key, self.defaults[key]))
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 self.auto_reload = self.auto_reload.lower() == 'true'
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 assert self.tags_file is not None
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
37 self.tags = Tags(self.tags_file)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.patterns = self.patterns.strip().split()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 # request handlers
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
41 self.handlers = [ TagCloud, ViewTags, PostTags ]
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 # template loader
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 self.template_dirs = self.template_dirs.split()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 self.template_dirs.append(resource_filename(__name__, 'templates'))
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 self.loader = TemplateLoader(self.template_dirs,
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 auto_reload=self.auto_reload)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 def __call__(self, environ, start_response):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 # get a request object
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 request = Request(environ)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 # get the path
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 path = request.path_info.strip('/').split('/')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 if path == ['']:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 path = []
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 request.environ['path'] = path
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 # match the request to a handler
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 for h in self.handlers:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 handler = h.match(self, request)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 if handler is not None:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 break
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 else:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 if self.app:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 return self.app(environ, start_response)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 handler = exc.HTTPNotFound
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 # add navigation links to handler [example]
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 if hasattr(handler, 'data'):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 handler.data.setdefault('links', [])
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 for h in self.handlers:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 handler.data['links'].append((handler.link(h.handler_path),
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 h.__name__))
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 # get response
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 res = handler()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 return res(environ, start_response)