OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 # | |
5 from twisted.web import resource | |
6 | |
7 class RewriterResource(resource.Resource): | |
8 | |
9 def __init__(self, orig, *rewriteRules): | |
10 resource.Resource.__init__(self) | |
11 self.resource = orig | |
12 self.rewriteRules = list(rewriteRules) | |
13 | |
14 def _rewrite(self, request): | |
15 for rewriteRule in self.rewriteRules: | |
16 rewriteRule(request) | |
17 | |
18 def getChild(self, path, request): | |
19 request.postpath.insert(0, path) | |
20 request.prepath.pop() | |
21 self._rewrite(request) | |
22 path = request.postpath.pop(0) | |
23 request.prepath.append(path) | |
24 return self.resource.getChildWithDefault(path, request) | |
25 | |
26 def render(self, request): | |
27 self._rewrite(request) | |
28 return self.resource.render(request) | |
29 | |
30 | |
31 def tildeToUsers(request): | |
32 if request.postpath and request.postpath[0][:1]=='~': | |
33 request.postpath[:1] = ['users', request.postpath[0][1:]] | |
34 request.path = '/'+'/'.join(request.prepath+request.postpath) | |
35 | |
36 def alias(aliasPath, sourcePath): | |
37 """ | |
38 I am not a very good aliaser. But I'm the best I can be. If I'm | |
39 aliasing to a Resource that generates links, and it uses any parts | |
40 of request.prepath to do so, the links will not be relative to the | |
41 aliased path, but rather to the aliased-to path. That I can't | |
42 alias static.File directory listings that nicely. However, I can | |
43 still be useful, as many resources will play nice. | |
44 """ | |
45 sourcePath = sourcePath.split('/') | |
46 aliasPath = aliasPath.split('/') | |
47 def rewriter(request): | |
48 if request.postpath[:len(aliasPath)] == aliasPath: | |
49 after = request.postpath[len(aliasPath):] | |
50 request.postpath = sourcePath + after | |
51 request.path = '/'+'/'.join(request.prepath+request.postpath) | |
52 return rewriter | |
OLD | NEW |