0
|
1 """
|
|
2 request handlers:
|
|
3 these are instantiated for every request, then called
|
|
4 """
|
|
5
|
1
|
6 import os
|
4
|
7 from paste.fileapp import FileApp
|
0
|
8 from urlparse import urlparse
|
|
9 from webob import Response, exc
|
|
10
|
|
11 class HandlerMatchException(Exception):
|
|
12 """the handler doesn't match the request"""
|
|
13
|
|
14 class Handler(object):
|
|
15
|
|
16 methods = set(['GET']) # methods to listen to
|
|
17 handler_path = [] # path elements to match
|
|
18
|
|
19 @classmethod
|
|
20 def match(cls, app, request):
|
|
21
|
|
22 # check the method
|
|
23 if request.method not in cls.methods:
|
|
24 return None
|
|
25
|
|
26 # check the path
|
|
27 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
|
|
28 return None
|
|
29
|
|
30 try:
|
|
31 return cls(app, request)
|
|
32 except HandlerMatchException:
|
|
33 return None
|
|
34
|
|
35 def __init__(self, app, request):
|
|
36 self.app = app
|
|
37 self.request = request
|
|
38 self.application_path = urlparse(request.application_url)[2]
|
|
39
|
|
40 def link(self, path=(), permanant=False):
|
|
41 if isinstance(path, basestring):
|
|
42 path = [ path ]
|
|
43 path = [ i.strip('/') for i in path ]
|
|
44 if permanant:
|
|
45 application_url = [ self.request.application_url ]
|
|
46 else:
|
|
47 application_url = [ self.application_path ]
|
|
48 path = application_url + path
|
|
49 return '/'.join(path)
|
|
50
|
|
51 def redirect(self, location):
|
|
52 raise exc.HTTPSeeOther(location=location)
|
|
53
|
1
|
54 class GenshiRenderer(Handler):
|
|
55
|
|
56 @classmethod
|
|
57 def match(cls, app, request):
|
|
58
|
|
59 # check the method
|
|
60 if request.method not in cls.methods:
|
|
61 return None
|
|
62
|
|
63 # check the path
|
|
64 path = request.environ['path']
|
|
65 if not path:
|
|
66 return None
|
|
67 if not path[-1].endswith('.html'):
|
|
68 return None
|
|
69
|
|
70
|
|
71 try:
|
|
72 return cls(app, request)
|
|
73 except HandlerMatchException:
|
|
74 return None
|
0
|
75
|
|
76 def __init__(self, app, request):
|
|
77 Handler.__init__(self, app, request)
|
1
|
78 self.template = os.path.join(app.directory, *request.environ['path'])
|
|
79 if not os.path.exists(self.template):
|
|
80 raise HandlerMatchException
|
0
|
81 self.data = { 'request': request,
|
|
82 'link': self.link }
|
|
83
|
|
84 def __call__(self):
|
|
85 return getattr(self, self.request.method.title())()
|
|
86
|
|
87 def Get(self):
|
|
88 template = self.app.loader.load(self.template)
|
|
89 return Response(content_type='text/html',
|
|
90 body=template.generate(**self.data).render('html'))
|
|
91
|
2
|
92
|
|
93 class Index(Handler):
|
|
94
|
|
95 template = 'index.html'
|
|
96
|
|
97 def __init__(self, app, request):
|
|
98 Handler.__init__(self, app, request)
|
|
99 self.directory = os.path.join(app.directory, *request.environ['path'])
|
|
100 if not os.path.isdir(self.directory):
|
|
101 raise HandlerMatchException
|
|
102 path = request.environ['path']
|
|
103 files = []
|
|
104 files = os.listdir(self.directory)
|
|
105 self.data = { 'request': request,
|
|
106 'link': self.link,
|
|
107 'directory': '/' + '/'.join(path),
|
|
108 'files': files }
|
|
109
|
|
110 def __call__(self):
|
|
111 return getattr(self, self.request.method.title())()
|
|
112
|
|
113 def Get(self):
|
|
114 if not self.request.path_info.endswith('/'):
|
|
115 self.redirect(self.request.path_info + '/')
|
|
116 template = self.app.loader.load(self.template)
|
|
117 return Response(content_type='text/html',
|
|
118 body=template.generate(**self.data).render('html'))
|
|
119
|
3
|
120 class Post(Handler):
|
|
121 methods = set(['POST']) # methods to listen to
|
|
122
|
|
123 def __init__(self, app, request):
|
|
124 Handler.__init__(self, app, request)
|
|
125 if 'file' not in request.POST:
|
|
126 raise HandlerMatchException
|
|
127 self.file = self.request.POST['file']
|
|
128 if not getattr(self.file, 'filename', None):
|
|
129 raise HandlerMatchException
|
|
130 self.location = request.path_info.rstrip('/')
|
|
131 path = os.path.join(self.app.directory, *self.request.environ['path'])
|
|
132 if os.path.isdir(path):
|
|
133 self.directory = path
|
|
134 self.filename = os.path.join(self.directory, self.file.filename)
|
|
135 self.location += '/' + self.file.filename
|
|
136 else:
|
|
137 self.directory = os.path.dirname(path)
|
|
138 self.filename = path
|
|
139
|
|
140 f = file(self.filename, 'wb')
|
|
141 f.write(self.file.file.read())
|
|
142 f.close()
|
|
143
|
|
144 def __call__(self):
|
|
145 self.redirect(self.location)
|
4
|
146
|
|
147 class FileServer(Handler):
|
|
148 methods = set(['GET']) # methods to listen to
|
|
149
|
|
150 def __init__(self, app, request):
|
|
151 Handler.__init__(self, app, request)
|
|
152 self.file = os.path.join(self.app.directory, *request.environ['path'])
|
|
153 if not os.path.exists(self.file):
|
|
154 raise HandlerMatchException
|
|
155
|
|
156 def __call__(self):
|
|
157 return FileApp(self.file)
|
|
158
|