Mercurial > hg > decoupage
comparison decoupage/web.py @ 31:89a72b2fd149
merge from head
author | egj@socialplanning.org |
---|---|
date | Tue, 02 Feb 2010 18:04:02 +0000 |
parents | 81607129a45e f306089d6def |
children | 527ccb76d043 |
comparison
equal
deleted
inserted
replaced
30:81607129a45e | 31:89a72b2fd149 |
---|---|
2 decoupage: a view with webob | 2 decoupage: a view with webob |
3 """ | 3 """ |
4 | 4 |
5 import os | 5 import os |
6 | 6 |
7 from contenttransformer.app import FileTypeTransformer | |
7 from formatters import formatters | 8 from formatters import formatters |
8 | 9 |
9 from genshi.builder import Markup | 10 from genshi.builder import Markup |
10 from genshi.template import TemplateLoader | 11 from genshi.template import TemplateLoader |
11 from genshi.template.base import TemplateError | 12 from genshi.template.base import TemplateError |
60 | 61 |
61 # template loader | 62 # template loader |
62 self.loader = TemplateLoader(self.template_directories, | 63 self.loader = TemplateLoader(self.template_directories, |
63 auto_reload=self.auto_reload) | 64 auto_reload=self.auto_reload) |
64 | 65 |
66 | |
67 | |
65 ### methods dealing with HTTP | 68 ### methods dealing with HTTP |
66 def __call__(self, environ, start_response): | 69 def __call__(self, environ, start_response): |
67 request = Request(environ) | 70 request = Request(environ) |
68 filename = request.path_info.strip('/') | 71 filename = request.path_info.strip('/') |
69 path = os.path.join(self.directory, filename) | 72 path = os.path.join(self.directory, filename) |
74 raise exc.HTTPMovedPermanently(add_slash=True) | 77 raise exc.HTTPMovedPermanently(add_slash=True) |
75 | 78 |
76 res = self.get(request) | 79 res = self.get(request) |
77 return res(environ, start_response) | 80 return res(environ, start_response) |
78 else: | 81 else: |
79 fileserver = self.fileserver(path) | 82 conf = self.conf(request.path_info.rsplit('/',1)[0]) |
83 if '/transformer' in conf: | |
84 args = [i.split('=', 1) for i in conf['/transformer'].split(',') if '=' in i] | |
85 fileserver = FileTypeTransformer(*args) | |
86 else: | |
87 fileserver = FileApp | |
88 | |
89 fileserver = fileserver(path) | |
80 return fileserver(environ, start_response) | 90 return fileserver(environ, start_response) |
81 else: | 91 else: |
82 raise exc.HTTPNotFound() | 92 raise exc.HTTPNotFound() |
83 | 93 |
84 def get_response(self, text, content_type='text/html'): | |
85 """construct a response to a GET request""" | |
86 res = Response(content_type=content_type, body=text) | |
87 return res | |
88 | 94 |
89 def get(self, request): | 95 def get(self, request): |
90 """ | 96 """ |
91 return response to a GET requst | 97 return response to a GET requst |
92 """ | 98 """ |
150 except TemplateError: | 156 except TemplateError: |
151 if local_index: | 157 if local_index: |
152 return self.fileserver(local_index) | 158 return self.fileserver(local_index) |
153 raise | 159 raise |
154 | 160 |
155 return self.get_response(res) | 161 return Response(content_type='text/html', body=res) |
162 | |
156 | 163 |
157 ### internal methods | 164 ### internal methods |
158 | 165 |
159 def filedata(self, path, directory, conf): | 166 def filedata(self, path, directory, conf): |
160 files = [] | 167 files = [] |
200 inherit_directory = '/%s' % '/'.join(path_tuple[:-1]) | 207 inherit_directory = '/%s' % '/'.join(path_tuple[:-1]) |
201 if inherit_directory: | 208 if inherit_directory: |
202 parent_configuration = self.conf(inherit_directory) | 209 parent_configuration = self.conf(inherit_directory) |
203 for key, value in parent_configuration.items(): | 210 for key, value in parent_configuration.items(): |
204 if key.startswith('/') and key not in conf: | 211 if key.startswith('/') and key not in conf: |
205 conf[key] = value | 212 conf[key] = value |
206 | 213 |
207 # cache configuration | 214 # cache configuration |
208 if not self.auto_reload: | 215 if not self.auto_reload: |
209 if not hasattr(self, '_conf'): | 216 if not hasattr(self, '_conf'): |
210 self._conf = {} | 217 self._conf = {} |