forked from ngerakines/commitment
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.py
More file actions
Latest commit
131 lines (106 loc) · 4.36 KB
/
Copy pathcommit.py
File metadata and controls
131 lines (106 loc) · 4.36 KB
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
importos
importsys
importrandom
importre
importjson
fromtypingimportDict, List
try:
fromhashlibimportmd5
exceptImportError:
frommd5importmd5
importtornado.httpserver
importtornado.ioloop
importtornado.web
fromtornado.escapeimportxhtml_unescape
fromtornado.optionsimportdefine, options
define("port", default=5000, help="run on the given port", type=int)
names= ['Nick', 'Steve', 'Andy', 'Qi', 'Fanny', 'Sarah', 'Cord', 'Todd',
'Chris', 'Pasha', 'Gabe', 'Tony', 'Jason', 'Randal', 'Ali', 'Kim',
'Rainer', 'Guillaume', 'Kelan', 'David', 'John', 'Stephen', 'Tom', 'Steven',
'Jen', 'Marcus', 'Edy', 'Rachel']
humans_file=os.path.join(os.path.dirname(__file__), 'static', 'humans.txt')
messages_file=os.path.join(os.path.dirname(__file__), 'commit_messages.txt')
messages: Dict[str, str] = {}
# Create a hash table of all commit messages
withopen(messages_file, 'r', encoding='utf-8') asmessages_input:
forlineinmessages_input.readlines():
messages[md5(line.encode('utf-8')).hexdigest()] =line
names: List[str] = []
withopen(humans_file, 'r', encoding='utf-8') ashumans_input:
humans_content=humans_input.read()
forlineinhumans_content.split("\n"):
if"Name:"inline:
data=line[6:].rstrip()
ifdata.find("github:") ==0:
names.append(data[7:])
else:
names.append(data.split(" ")[0])
print(names)
num_re=re.compile(r"XNUM([0-9,]*)X")
deffill_line(message):
message=message.replace('XNAMEX', random.choice(names))
message=message.replace('XUPPERNAMEX', random.choice(names).upper())
message=message.replace('XLOWERNAMEX', random.choice(names).lower())
nums=num_re.findall(message)
whilenums:
start=1
end=999
value=nums.pop(0) orstr(end)
if","invalue:
position=value.index(",")
ifposition==0: # XNUM,5X
end=int(value[1:])
elifposition==len(value) -1: # XNUM5,X
start=int(value[:position])
else: # XNUM1,5X
start=int(value[:position])
end=int(value[position+1:])
else:
end=int(value)
ifstart>end:
end=start*2
randint=random.randint(start, end)
message=num_re.sub(str(randint), message, count=1)
returnmessage
classMainHandler(tornado.web.RequestHandler):
defget(self, message_hash=None):
ifnotmessage_hash:
message_hash=random.choice(list(messages.keys()))
elifmessage_hashnotinmessages:
raisetornado.web.HTTPError(404)
message=fill_line(messages[message_hash])
self.output_message(message, message_hash)
defoutput_message(self, message, message_hash):
self.set_header('X-Message-Hash', message_hash)
self.render('index.html', message=message, message_hash=message_hash)
classPlainTextHandler(MainHandler):
defoutput_message(self, message, message_hash):
self.set_header('Content-Type', 'text/plain')
self.set_header('X-Message-Hash', message_hash)
self.write(xhtml_unescape(message).replace('<br/>', '\n'))
classJsonHandler(MainHandler):
defoutput_message(self, message, message_hash):
self.set_header('Content-Type', 'application/json')
self.set_header('X-Message-Hash', message_hash)
self.write(json.dumps({'hash': message_hash, 'commit_message':message.replace('\n', ''), 'permalink': self.request.protocol+"://"+self.request.host+'/'+message_hash }))
classHumansHandler(tornado.web.RequestHandler):
defget(self):
self.set_header('Content-Type', 'text/plain')
self.write(humans_content)
settings= {
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
}
application=tornado.web.Application([
(r'/', MainHandler),
(r'/([a-z0-9]+)', MainHandler),
(r'/index.json', JsonHandler),
(r'/([a-z0-9]+).json', JsonHandler),
(r'/index.txt', PlainTextHandler),
(r'/([a-z0-9]+)/index.txt', PlainTextHandler),
(r'/humans.txt', HumansHandler),
], **settings)
if__name__=='__main__':
tornado.options.parse_command_line()
http_server=tornado.httpserver.HTTPServer(application)
http_server.listen(os.environ.get("PORT", 5000))
tornado.ioloop.IOLoop.instance().start()