forked from ngerakines/commitment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.py
66 lines (52 loc) · 2.02 KB
/
commit.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
import os
import sys
import random
try:
from hashlib import md5
except ImportError:
from md5 import md5
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.escape import xhtml_unescape
names = ['Nick', 'Steve', 'Andy', 'Qi', 'Fanny', 'Sarah', 'Cord', 'Todd',
'Chris', 'Pasha', 'Gabe', 'Tony', 'Jason', 'Randal', 'Ali', 'Kim',
'Rainer', 'Guillaume']
messages_file = os.path.join(os.path.dirname(__file__), 'commit_messages.txt')
messages = {}
# Create a hash table of all commit messages
for line in open(messages_file).readlines():
messages[md5(line).hexdigest()] = line
class MainHandler(tornado.web.RequestHandler):
def get(self, message_hash=None):
if not message_hash:
message_hash = random.choice(messages.keys())
elif message_hash not in messages:
raise tornado.web.HTTPError(404)
message = messages[message_hash].replace(
'XNAMEX', random.choice(names))
message = message.replace('XUPPERNAMEX', random.choice(names).upper())
message = message.replace('XLOWERNAMEX', random.choice(names).lower())
self.output_message(message, message_hash)
def output_message(self, message, message_hash):
self.render('index.html', message=message, message_hash=message_hash)
class PlainTextHandler(MainHandler):
def output_message(self, message, message_hash):
self.set_header('Content-Type', 'text/plain')
self.write(xhtml_unescape(message).replace('<br/>', '\n'))
settings = {
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
}
application = tornado.web.Application([
(r'/', MainHandler),
(r'/([a-z0-9]+)', MainHandler),
(r'/index.txt', PlainTextHandler),
(r'/([a-z0-9]+)/index.txt', PlainTextHandler),
], **settings)
if __name__ == '__main__':
port = 8000
if len(sys.argv) > 1:
port = int(sys.argv[1])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()