|
1 | 1 | import os |
2 | | -import sys |
3 | | -import random |
4 | | -import re |
5 | 2 | import json |
6 | 3 | import signal |
7 | | -from typing import Dict, List |
8 | | - |
9 | | -from hashlib import md5 |
10 | 4 |
|
11 | 5 | import tornado.httpserver |
12 | 6 | import tornado.ioloop |
13 | 7 | import tornado.web |
14 | 8 | from tornado.escape import xhtml_unescape |
15 | 9 | from tornado.options import define, options |
16 | 10 |
|
17 | | -define("port", default=5000, help="run on the given port", type=int) |
18 | | - |
19 | | -humans_file = os.path.join(os.path.dirname(__file__), 'static', 'humans.txt') |
20 | | -messages_file = os.path.join(os.path.dirname(__file__), 'commit_messages.txt') |
21 | | -messages: Dict[str, str] = {} |
22 | | - |
23 | | -# Create a hash table of all commit messages |
24 | | -with open(messages_file, 'r', encoding='utf-8') as messages_input: |
25 | | - for line in messages_input.readlines(): |
26 | | - messages[md5(line.encode('utf-8')).hexdigest()] = line |
27 | | - |
28 | | -names: List[str] = [] |
29 | | - |
30 | | -with open(humans_file, 'r', encoding='utf-8') as humans_input: |
31 | | - humans_content = humans_input.read() |
32 | | - for line in humans_content.split("\n"): |
33 | | - if "Name:" in line: |
34 | | - data = line[6:].rstrip() |
35 | | - if data.find("github:") == 0: |
36 | | - names.append(data[7:]) |
37 | | - else: |
38 | | - names.append(data.split(" ")[0]) |
39 | | - |
40 | | -num_re = re.compile(r"XNUM([0-9,]*)X") |
41 | | - |
42 | | -def fill_line(message): |
43 | | - message = message.replace('XNAMEX', random.choice(names)) |
44 | | - message = message.replace('XUPPERNAMEX', random.choice(names).upper()) |
45 | | - message = message.replace('XLOWERNAMEX', random.choice(names).lower()) |
46 | | - |
47 | | - nums = num_re.findall(message) |
48 | | - |
49 | | - while nums: |
50 | | - start = 1 |
51 | | - end = 999 |
52 | | - value = nums.pop(0) or str(end) |
53 | | - if "," in value: |
54 | | - position = value.index(",") |
55 | | - if position == 0: # XNUM,5X |
56 | | - end = int(value[1:]) |
57 | | - elif position == len(value) - 1: # XNUM5,X |
58 | | - start = int(value[:position]) |
59 | | - else: # XNUM1,5X |
60 | | - start = int(value[:position]) |
61 | | - end = int(value[position+1:]) |
62 | | - else: |
63 | | - end = int(value) |
64 | | - if start > end: |
65 | | - end = start * 2 |
66 | | - |
67 | | - randint = random.randint(start, end) |
68 | | - message = num_re.sub(str(randint), message, count=1) |
| 11 | +import messages |
69 | 12 |
|
70 | | - return message |
| 13 | +define("port", default=5000, help="run on the given port", type=int) |
71 | 14 |
|
72 | 15 | class MainHandler(tornado.web.RequestHandler): |
73 | 16 | def get(self, message_hash=None): |
74 | | - if not message_hash: |
75 | | - message_hash = random.choice(list(messages.keys())) |
76 | | - elif message_hash not in messages: |
77 | | - raise tornado.web.HTTPError(404) |
| 17 | + found_message = messages.find_by_md5(message_hash) |
78 | 18 |
|
79 | | - message = fill_line(messages[message_hash]) |
| 19 | + if message_hash and not found_message: |
| 20 | + raise tornado.web.HTTPError(404) |
80 | 21 |
|
81 | | - self.output_message(message, message_hash) |
| 22 | + if found_message: |
| 23 | + self.output_message(found_message, message_hash) |
| 24 | + else: |
| 25 | + message, generated_message_hash = messages.generate() |
| 26 | + self.output_message(message, generated_message_hash) |
82 | 27 |
|
83 | 28 | def output_message(self, message, message_hash): |
84 | 29 | self.set_header('X-Message-Hash', message_hash) |
@@ -129,6 +74,8 @@ def try_exit(self): |
129 | 74 | tornado.options.parse_command_line() |
130 | 75 | signal.signal(signal.SIGINT, application.signal_handler) |
131 | 76 | http_server = tornado.httpserver.HTTPServer(application) |
132 | | - http_server.listen(os.environ.get("PORT", 5000)) |
| 77 | + port = os.environ.get("PORT", 5000) |
| 78 | + print("ready for requests (on port %s)" % (port)) |
| 79 | + http_server.listen(port) |
133 | 80 | tornado.ioloop.PeriodicCallback(application.try_exit, 100).start() |
134 | 81 | tornado.ioloop.IOLoop.instance().start() |
0 commit comments