0
|
1 """
|
|
2 request handlers:
|
|
3 these are instantiated for every request, then called
|
|
4 """
|
|
5
|
3
|
6 import urllib2
|
|
7
|
0
|
8 from pprint import pprint
|
|
9 from urlparse import urlparse
|
|
10 from webob import Response, exc
|
|
11 from StringIO import StringIO
|
3
|
12 from wordstream.api import Corpus
|
|
13 from wordstream.dissociate import dissociate
|
0
|
14
|
|
15 class HandlerMatchException(Exception):
|
|
16 """the handler doesn't match the request"""
|
|
17
|
|
18 class Handler(object):
|
|
19
|
|
20 methods = set(['GET']) # methods to listen to
|
|
21 handler_path = [] # path elements to match
|
|
22
|
|
23 @classmethod
|
|
24 def match(cls, app, request):
|
|
25
|
|
26 # check the method
|
|
27 if request.method not in cls.methods:
|
|
28 return None
|
|
29
|
|
30 # check the path
|
|
31 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
|
|
32 return None
|
|
33
|
|
34 try:
|
|
35 return cls(app, request)
|
|
36 except HandlerMatchException:
|
|
37 return None
|
|
38
|
|
39 def __init__(self, app, request):
|
|
40 self.app = app
|
|
41 self.request = request
|
|
42 self.application_path = urlparse(request.application_url)[2]
|
|
43
|
|
44 def link(self, path=(), permanant=False):
|
|
45 if isinstance(path, basestring):
|
|
46 path = [ path ]
|
|
47 path = [ i.strip('/') for i in path ]
|
|
48 if permanant:
|
|
49 application_url = [ self.request.application_url ]
|
|
50 else:
|
|
51 application_url = [ self.application_path ]
|
|
52 path = application_url + path
|
|
53 return '/'.join(path)
|
|
54
|
|
55 def redirect(self, location):
|
|
56 raise exc.HTTPSeeOther(location=location)
|
|
57
|
|
58 class GenshiHandler(Handler):
|
|
59
|
|
60 def __init__(self, app, request):
|
|
61 Handler.__init__(self, app, request)
|
|
62 self.data = { 'request': request,
|
|
63 'link': self.link }
|
|
64
|
|
65 def __call__(self):
|
|
66 return getattr(self, self.request.method.title())()
|
|
67
|
|
68 def Get(self):
|
|
69 # needs to have self.template set
|
|
70 template = self.app.loader.load(self.template)
|
|
71 return Response(content_type='text/html',
|
|
72 body=template.generate(**self.data).render('html'))
|
|
73
|
|
74 class Index(GenshiHandler):
|
|
75 template = 'index.html'
|
|
76 methods=set(['GET'])
|
|
77
|
|
78 @classmethod
|
|
79 def match(cls, app, request):
|
|
80 if not request.path_info.strip('/'):
|
|
81 return cls(app, request)
|
|
82
|
|
83 class ViewCorpus(GenshiHandler):
|
|
84 template = 'corpus.html'
|
|
85 handler_path = ['corpus']
|
|
86
|
|
87 def Get(self):
|
|
88 buffer = StringIO()
|
|
89 pprint(self.app.corpus, buffer)
|
|
90 self.data['corpus'] = buffer.getvalue()
|
|
91 return GenshiHandler.Get(self)
|
|
92
|
|
93 class Eat(Handler):
|
|
94
|
|
95 methods = set(['GET'])
|
|
96
|
|
97 @classmethod
|
|
98 def match(cls, app, request):
|
|
99
|
|
100 # check the method
|
|
101 if request.method not in cls.methods:
|
|
102 return None
|
|
103
|
|
104 if len(request.environ['path']) == 1:
|
|
105 return cls(app, request)
|
|
106
|
|
107 def __call__(self):
|
|
108 word = self.request.path_info.strip('/')
|
|
109 association = self.app.corpus.eat(word) or ''
|
|
110 return Response(content_type='text/plain',
|
|
111 body=association)
|
|
112
|
|
113
|
|
114
|
|
115 class Feed(Handler):
|
|
116
|
|
117 methods = set(['POST'])
|
|
118
|
|
119 @classmethod
|
|
120 def match(cls, app, request):
|
|
121
|
|
122 # check the method
|
|
123 if request.method not in cls.methods:
|
|
124 return None
|
|
125
|
|
126 if len(request.environ['path']) > 1:
|
|
127 return cls(app, request)
|
|
128
|
|
129 def __call__(self):
|
|
130 self.app.corpus.feed_stream(self.request.environ['path'])
|
|
131 return exc.HTTPOk()
|
3
|
132
|
|
133
|
|
134 class Dissociate(GenshiHandler):
|
|
135 template = 'post.html'
|
|
136 methods = set(['GET', 'POST'])
|
|
137
|
|
138
|
|
139 @classmethod
|
|
140 def match(cls, app, request):
|
|
141
|
|
142 # check the method
|
|
143 if request.method not in cls.methods:
|
|
144 return None
|
|
145
|
|
146 return cls(app, request)
|
|
147
|
|
148 def Get(self):
|
|
149 if 'url' in self.request.GET:
|
|
150 contents = self.url_contents(self.request.GET['url'])
|
|
151 dissociation = self.dissociation(contents)
|
|
152 return Response(content_type='text/html',
|
|
153 body='<html><body>%s</body></html>' % dissociation)
|
|
154
|
|
155
|
|
156 return GenshiHandler.Get(self)
|
|
157
|
|
158 def url_contents(self, url):
|
|
159 return urllib2.urlopen(url).read()
|
|
160
|
|
161 def dissociation(self, contents):
|
|
162 corpus = Corpus()
|
|
163 corpus.feed_stream(contents)
|
|
164 corpus.scramble()
|
|
165 buffer = StringIO()
|
|
166 dissociate(corpus, buffer)
|
|
167 return buffer.getvalue()
|
|
168
|
|
169 def Post(self):
|
|
170 if 'url' in self.request.POST:
|
|
171 contents = self.url_contents(self.request.POST['url'])
|
|
172 elif 'text' in self.request.POST:
|
|
173 contents = self.request.POST['text']
|
|
174 elif 'file' in self.request.POST:
|
|
175 contents = self.request.POST['file'].file.read()
|
|
176 dissociation = self.dissociation(contents)
|
|
177 return Response(content_type='text/html',
|
|
178 body='<html><body>%s</body></html>' % dissociation)
|