forked from nattofriends/moffle
/
app.py
252 lines (183 loc) · 7.31 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import monkey_patch # noqa
from babel import negotiate_locale
from flask import Flask
from flask import abort
from flask import g
from flask import redirect
from flask import request
from flask import render_template
from flask import Response
from flask import url_for
from flask_babel import Babel
from werkzeug.contrib.fixers import ProxyFix
from werkzeug.contrib.profiler import ProfilerMiddleware
import config
import exceptions
import grep
import log_path
import util
# Must import to run decorator
import template_context # noqa
import line_format # noqa
from acl import AccessControl
from forms import AjaxSearchForm
from forms import SearchForm
if config.DEBUG_PYINSTRUMENT:
from pyinstrument import Profiler
app = Flask(__name__)
babel = Babel(app)
@app.route('/')
def index():
networks = paths.networks()
return render_template('index.html', networks=networks)
@app.route('/<network>/')
def network(network):
try:
channels = paths.channels(network)
except exceptions.NoResultsException:
abort(404)
return render_template('network.html', network=network, channels=channels)
@app.route('/<network>/<channel>/')
def channel(network, channel):
try:
dates = paths.channel_dates(network, channel)
return render_template('channel.html', network=network, channel=channel, dates=dates)
except exceptions.NoResultsException as ex:
abort(404)
except exceptions.MultipleResultsException as ex:
return render_template('error/multiple_results.html', network=network, channel=channel)
except exceptions.CanonicalNameException as ex:
_, canonical_channel = ex.args
dates = paths.channel_dates(network, canonical_channel)
g.canonical_url = url_for('channel', network=network, channel_=canonical_channel)
return channel_(network, canonical_channel)
channel_ = channel
@app.route('/<network>/<channel>/<date>')
def log(network, channel, date):
try:
log = paths.log(network, channel, date)
pagination_control = 1 + sum(bool(maybe) for maybe in (log.before, log.after))
return render_template('log.html', network=network, channel=channel, date=date, pagination_control=pagination_control, log=log)
except exceptions.NoResultsException as ex:
abort(404)
except exceptions.MultipleResultsException as ex:
return render_template('error/multiple_results.html', network=network, channel=channel)
except exceptions.CanonicalNameException as ex:
info_type, canonical_data = ex.args
if info_type == util.Scope.CHANNEL:
channel = canonical_data
elif info_type == util.Scope.DATE:
date = canonical_data
g.canonical_url = url_for('log', network=network, channel=channel, date=date)
return log_(network, channel, date)
@app.route('/<network>/<channel>/<date>/raw')
def log_raw(network, channel, date):
try:
log = paths.log(network, channel, date)
# Don't look, kids!
_, (log_lines_only, _) = log.log.__reduce__()
log_text = ''.join(list(log_lines_only))
return Response(log_text, mimetype='text/plain')
except exceptions.NoResultsException as ex:
abort(404)
except exceptions.MultipleResultsException as ex:
return render_template('error/multiple_results.html', network=network, channel=channel)
except exceptions.CanonicalNameException as ex:
info_type, canonical_data = ex.args
if info_type == util.Scope.CHANNEL:
channel = canonical_data
elif info_type == util.Scope.DATE:
date = canonical_data
return redirect(url_for('log_raw', network=network, channel=channel, date=date))
log_ = log
@app.route('/search/')
def search():
# TODO: Expose multi-channel search
form = SearchForm(request.args, csrf_enabled=False)
valid = form.validate()
network = form.network.data
channel = form.channel.data
if config.SEARCH_AJAX_ENABLED:
if not valid:
# TODO: Improve this?
abort(404)
try:
dates = paths.channels_dates(network, [channel])
except exceptions.NoResultsException:
abort(404)
except exceptions.MultipleResultsException:
return render_template('error/multiple_results.html', network=network, channel=channel)
max_segment = grep.max_segment(dates[-1]['date_obj'])
return render_template('search_ajax.html', valid=valid, form=form, network=network, channel=channel, author=form.author.data, query=form.text.data, max_segment=max_segment)
else:
# We should have another copy of this to use...
if not valid:
results = []
else:
results = grep.run(
channels=[channel],
network=network,
author=form.author.data,
query=form.text.data,
)
return render_template('search.html', valid=valid, form=form, network=network, channel=channel, results=results)
@app.route('/search/chunk')
def search_ajax_chunk():
form = AjaxSearchForm(request.args, csrf_enabled=False)
valid = form.validate()
date_start, date_end = grep.segment_bounds(form.segment.data)
# We should have another copy of this to use...
if not valid:
results = []
else:
results = grep.run(
channels=[form.channel.data],
network=form.network.data,
author=form.author.data,
query=form.text.data,
date_range=[date_start, date_end],
)
return render_template('search_result.html', network=form.network.data, channels=[form.channel.data], results=results)
@app.errorhandler(404)
def not_found(ex):
return render_template('error/not_found.html'), 404
@babel.localeselector
def get_locale():
from_cookie = request.cookies.get('lang', None)
if from_cookie and from_cookie in config.LOCALE_PREFER:
return from_cookie
else:
preferred = [x.replace('-', '_') for x in request.accept_languages.values()]
return negotiate_locale(preferred, config.LOCALE_PREFER)
def inject_profiler():
request.profiler = Profiler(use_signal=False)
request.profiler.start()
def output_profiler(response):
request.profiler.stop()
print(request.profiler.output_text(unicode=True, color=True))
return response
def create():
global paths, grep
paths = getattr(log_path, config.LOG_PATH_CLASS)(AccessControl(config.ACL))
grep = getattr(grep, config.GREP_BUILDER_CLASS)(paths)
util.register_context_processors(app)
util.register_template_filters(app)
from auth import auth
app.register_blueprint(auth, url_prefix='/auth')
app.secret_key = config.SECRET_KEY
# Produces actual debug under internal server. Produces stderr under uwsgi
app.debug = True
# Not app.jinja_options, because ???
# app.jinja_env.bytecode_cache = FileSystemBytecodeCache()
if config.FLASK_PROXY:
app.wsgi_app = ProxyFix(app.wsgi_app)
if config.DEBUG_PROFILER:
app.config['PROFILE'] = True
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
if config.DEBUG_PYINSTRUMENT:
app.before_request(inject_profiler)
app.after_request(output_profiler)
return app
if __name__ == '__main__':
create()
app.run(host='0.0.0.0', debug=True)