Mercurial > hg > TagInTheMiddle
annotate 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 |
rev | line source |
---|---|
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
19
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
2 apply and display tags via WSGI middleware; |
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
3 request dispatcher component |
0
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 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 from genshi.template import TemplateLoader |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 from lxmlmiddleware import LXMLMiddleware |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 from paste.fileapp import FileApp |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 from pkg_resources import resource_filename |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 from webob import Request, Response, exc |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 class Tagger(LXMLMiddleware): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 # instance defaults |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 defaults = { 'auto_reload': 'False', |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 'template_dirs': '', |
6
8fb20b780341
correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
21 'patterns': '*', # glob pattern: all URLs by default |
8fb20b780341
correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
22 'tags_url': 'tags', # base URL for viewing tags |
8fb20b780341
correct and document middleware class defaults
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
23 'post_url': '.tags', # sub-URL for posting tags |
2
1182315b18ac
add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
24 'author': None, # for RSS |
19
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
25 'engine': None |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 } |
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 def __init__(self, app, **kw): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 self.app = app |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 # set instance parameters from kw and defaults |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 for key in self.defaults: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 self.patterns = self.patterns.strip().split() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 # request handlers |
2
1182315b18ac
add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
38 self.handlers = [ TagCloud, ViewTags, PostTags ] |
0
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 # template loader |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 self.template_dirs = self.template_dirs.split() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 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
|
43 self.loader = TemplateLoader(self.template_dirs, |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 auto_reload=self.auto_reload) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 |
19
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
46 # tag engine |
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
47 assert self.engine is not None, 'Must specify a tag.engine' |
57ca873ed9bf
make this a pluggable system and start reorging infrastructure for that
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
48 |
0
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 # get response |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 res = handler() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 return res(environ, start_response) |
15 | 73 |
74 def manipulate(self, request, response, tree): | |
75 return tree |