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