-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.py
490 lines (393 loc) · 13 KB
/
web.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python
# encoding: utf-8
"""
web.py - Web viewr for willie-lumberjack module.
Copyright 2013, Timothy Lee <[email protected]>
Licensed under the MIT License.
"""
import sys
import json
import urllib
import logging
import re
import os
import bottle
from bottle.ext import redis
from redis import Redis
from socketio import socketio_manage
from socketio.namespace import BaseNamespace
from gevent import monkey
import yapdi
from lumberjack import str_time, str_date, CHANNELS
import config
###############################################################################
# Setup the APP
###############################################################################
logging.basicConfig(
filename=config.LOGFILE,
format='%(asctime)s - %(levelname)s - %(message)s',
#datefmt='%Y-%M-%d %H:%M:%S',
level=logging.DEBUG,
)
app = bottle.Bottle()
plugin = redis.RedisPlugin(
host = config.REDIS_HOST,
port = int(config.REDIS_PORT),
database = int(config.REDIS_DBID),
)
app.install(plugin)
###############################################################################
# Helper function
###############################################################################
def get_redis_from_app(app):
attr = 'redisdb'
for p in app.plugins:
if hasattr(p, attr):
return Redis(connection_pool=getattr(p, attr))
return None
def channel_name(channel):
"""Get IRC channel name.
:channel: channel name without #
:returns: channel name with #
"""
return "#%s" % channel if not channel.startswith('#') else channel
def get_logs(rdb, channel, date):
"""Get logs with specify date.
:rdb: redis db instance
:channel: IRC channel name without #
:date: string represend the date
:returns: list(logs)
"""
channel, date = channel_name(channel), str_date(date)
date = str_date(date)
key = "%s:%s" % (channel, date)
return rdb.lrange(key, 0, -1)
def get_channels(rdb):
"""get IRC channel names without #
:rdb: redis db instance
:returns: list(channels)
"""
return [ c.lstrip('#') for c in rdb.lrange(CHANNELS, 0, -1)]
def irc_row(str_json):
"""Convert IRC log from JSON to dict.
:str_json: JSON data
:returns: dict(data)
"""
data = json.loads(str_json)
return dict(
time=str_time(data['time']), nick=data['nick'], msg=data['msg'])
def run(dev=False):
"""Start the Bottle server.
:dev: True if development else False
"""
debug, reloader = False, False
if dev:
debug, reloader = True, True
monkey.patch_all()
bottle.run(
app=app, host=config.BIND_HOST, port=config.BIND_PORT,
server='geventSocketIO',
debug=debug, reloader=reloader)
def is_strdate(string):
"""Check the date format.
:string: string represend the date.
:returns: True or False
"""
p = re.compile('[0-9]{4}-[0-9]{2}-[0-9]{2}')
return True if p.search(string) else False
def set_theme(name):
"""Set the name of theme in cookie.
:name: theme's name
"""
bottle.response.set_cookie("theme", name, path='/')
def get_theme():
"""Get the name of theme in cookie.
:returns: theme's name
"""
return bottle.request.get_cookie('theme')
def _is_what(what):
"""Return True if waht is True in cookie else False
:what: key in cookie
:returns: True or False
"""
try:
value = bottle.request.get_cookie(what)
return True if value.lower() in ['true', '1'] else False
except Exception:
return False
def set_autolinks(value):
"""Set autolinks to value in cookie.
:value: Ture or False
"""
bottle.response.set_cookie('autolinks', value, path='/')
def is_autolinks():
"""Return if autolinks or not.
:returns: True or False
"""
return _is_what('autolinks')
def set_reverse(value):
"""Set reverse to value in cookie.
:value: Ture or False
"""
bottle.response.set_cookie('reverse', value, path='/')
def is_reverse():
"""Check if reverse or not.
:returns: True or False
"""
return _is_what('reverse')
###############################################################################
# Helper class
###############################################################################
class LogNameSpace(BaseNamespace):
"""NameSpace for socketio. """
def on_join(self, msg):
redis = get_redis_from_app(app)
key = channel = msg
sub = redis.pubsub()
sub.subscribe(channel)
for i in sub.listen():
if i['type'] == 'message':
self.emit('recive', redis.lrange(key, -1, -1)[0])
sub.unsubscribe()
###############################################################################
# View
###############################################################################
@app.get('/_static/<filepath:path>')
def get_static(filepath):
"""Return static file.
"""
return bottle.static_file(filepath, root='./static/')
@app.get('/')
def root(rdb):
"""Root view.
:rdb: redis DB instance
"""
bottle.redirect('/channels')
@app.get('/channels<slash:re:/*>')
def channels(rdb, slash):
"""Channels view.
:rdb: redis DB instance
:slash: / or None
"""
status, channels = [], get_channels(rdb)
for c in channels:
status.append(dict(name=c, length=len(get_logs(rdb, c, 'today'))))
return bottle.template('channels',
project=config.PROJECT,
channels=channels,
status=status)
@app.get('/channel/<channel><slash:re:/*>')
def channel(rdb, channel, slash):
"""Alias for channel view.
:rdb: redis DB instance
:channel: IRC channel name without #
:slash: / or None
"""
bottle.redirect("/channel/%s/today/" % (urllib.quote_plus(channel)))
@app.get('/channel/<channel>/<date><slash:re:/*>')
def viewer(rdb, channel, date, slash):
"""Channel view.
:rdb: redis DB instance
:channel: IRC channel name without #
:date: string represend the date
"""
socketio = True if date == 'today' else False
date = str_date(date)
if date:
rows = []
for i in get_logs(rdb, channel, date):
rows.append(irc_row(i))
if is_reverse():
rows.reverse()
return bottle.template('viewer',
channel=channel,
date=date,
channels=get_channels(rdb),
rows=rows,
socketio=socketio,
autolinks=is_autolinks(),
reverse=is_reverse(),
server=bottle.request.urlparts[1],
)
else:
bottle.redirect('/channel/%s/today/' % (channel))
@app.get('/channel/<channel>/<date>/<line:int>')
def show_quote(rdb, channel, date, line):
"""Show the quote.
:rdb: redis DB instance
:channel: IRC channel name without #
:date: string represend the date
:line: line number of logs in channel view
"""
date = str_date(date)
if date:
try:
logs = get_logs(rdb, channel , date)
row = irc_row(logs[line - 1])
except Exception:
row = None
return bottle.template('quote',
channel=channel,
date=date,
channels=get_channels(rdb),
row=row)
else:
bottle.redirect('/channel/%s/today/' % (channel))
@app.get('/widget/<channel><slash:re:/*>')
def widget(rdb, channel, slash):
"""Widget view, date='today', socketio = autolinks = reverse = True.
:rdb: redis DB instance
:channel: IRC channel name without #
"""
date = str_date('today')
rows = []
for i in get_logs(rdb, channel, date):
rows.append(irc_row(i))
rows.reverse()
return bottle.template('viewer',
channel=channel,
date=date,
channels=get_channels(rdb),
rows=rows,
socketio=True,
autolinks=True,
reverse=True,
server=bottle.request.urlparts[1],
widget=True,
)
@app.get('/options<slash:re:/*>')
def options(rdb, slash):
"""Options view.
:rdb: redis DB instance
:slash: / or None
"""
autolinks = is_autolinks()
autolinks_true_active = 'active' if autolinks else ''
autolinks_false_active= 'active' if not autolinks else ''
reverse = is_reverse()
reverse_true_active = 'active' if reverse else ''
reverse_false_active= 'active' if not reverse else ''
return bottle.template('options',
channels=get_channels(rdb),
themes = config.BOOTSWATCH_THEMES,
autolinks_true_active=autolinks_true_active,
autolinks_false_active=autolinks_false_active,
reverse_true_active=reverse_true_active,
reverse_false_active=reverse_false_active,
)
@app.get('/archives/<channel>/<log>')
def get_archive(rdb, channel, log):
"""Return plain text log file.
:channel: IRC channel name without #
:log: log name
"""
return bottle.static_file(
os.path.join("#%s" % channel, log),
root=config.LOG_PATH)
@app.get('/options/themes/<theme>')
def themes(rdb, theme):
"""API to set the theme.
:rdb: redis DB instance
:theme: theme's name
"""
set_theme(theme)
bottle.redirect('/options')
@app.get('/options/autolinks/<value>')
def autolinks(rdb, value):
"""API to set the autolinks.
:rdb: redis DB instance
:value: True or False
"""
set_autolinks(value)
bottle.redirect('/options/')
@app.get('/options/reverse/<value>')
def reverse(rdb, value):
"""API to set the reverse option.
:rdb: redis DB instance
:value: True or False
"""
set_reverse(value)
bottle.redirect('/options/')
@app.post('/go2date')
def go2date(rdb):
"""API to get the log view.
:rdb: redis DB instance
"""
#. regx for date
channel = bottle.request.forms.channel
date = str_date(bottle.request.forms.date) \
if is_strdate(bottle.request.forms.date) else None
date = date if date else 'today'
bottle.redirect("/channel/%s/%s/" % (
urllib.quote_plus(channel), date))
@app.error(404)
def _error404(error):
"""404 error handler.
"""
#return "404"
bottle.response.status = 303
bottle.response.headers['Location'] = '/404'
@app.error(500)
def _error500(error):
"""500 error handler.
"""
bottle.response.status = 303
bottle.response.headers['Location'] = '/500'
@app.get('/404')
def error404(rdb):
"""Error 404 view.
"""
return bottle.template('error404',
channels=get_channels(rdb))
@app.get('/500')
def error500(rdb):
"""Error 500 view.
"""
return bottle.template('error500',
channels=get_channels(rdb))
@app.get('/socket.io/<path:path>')
def socketio_service(path):
"""socket.io connect path.
"""
socketio_manage(bottle.request.environ,
{'/log': LogNameSpace}, bottle.request)
###############################################################################
# Main
###############################################################################
if __name__ == '__main__':
ctrls = ['start', 'stop', 'status', 'dev']
daemon = yapdi.Daemon(pidfile=config.PIDFILE, stderr=config.LOGFILE)
if len(sys.argv) == 2 and sys.argv[1] in ctrls:
arg = sys.argv[1]
if arg == 'start':
if daemon.status():
print "Pidfile exist: %s, %s(%s) is running already!" % (
config.PIDFILE, config.APPNAME, daemon.status())
exit()
retcode = daemon.daemonize()
if retcode == yapdi.OPERATION_SUCCESSFUL:
try:
run(dev=False)
except KeyboardInterrupt:
exit()
if arg == 'stop':
if not daemon.status():
print "%s is not running!" % (config.APPNAME)
exit()
retcode = daemon.kill()
if retcode == yapdi.OPERATION_FAILED:
print "Error during %s stop!" % (config.APPNAME)
exit(1)
else:
print "%s was stopped." % (config.APPNAME)
if arg == 'status':
if not daemon.status():
print "%s is not running!" % (config.APPNAME)
else:
print "%s(%s) is running." % (config.APPNAME, daemon.status())
if arg == 'dev':
run(dev=True)
else:
print 'Usage: web.py <%s>' % '|'.join(ctrls)
else:
application = app