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