forked from DrSwad/FastOlympicCodingHook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastOlympicCodingHook.py
More file actions
53 lines (47 loc) · 1.86 KB
/
FastOlympicCodingHook.py
File metadata and controls
53 lines (47 loc) · 1.86 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
import sublime
import sublime_plugin
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import _thread
import threading
import platform
def MakeHandlerClassFromFilename(filename):
class HandleRequests(BaseHTTPRequestHandler):
def do_POST(self):
try:
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
tests = json.loads(body.decode('utf8'))
tests = tests["tests"]
ntests = []
for test in tests:
ntest = {
"test": test["input"],
"correct_answers": [test["output"].strip()]
}
ntests.append(ntest)
nfilename = filename + ":tests"
if platform.system() == "Windows":
nfilename = filename + "__tests"
print(nfilename)
with open(nfilename, "w") as f:
f.write(json.dumps(ntests))
except Exception as e:
print("Error handling POST - " + str(e))
threading.Thread(target=self.server.shutdown, daemon=True).start()
return HandleRequests
class CompetitiveCompanionServer:
def startServer(filename):
host = 'localhost'
port = 12345
HandlerClass = MakeHandlerClassFromFilename(filename)
httpd = HTTPServer((host, port), HandlerClass)
httpd.serve_forever()
print("Server has been shutdown")
class FastOlympicCodingHookCommand(sublime_plugin.TextCommand):
def run(self, edit):
try:
_thread.start_new_thread(CompetitiveCompanionServer.startServer,
(self.view.file_name(),))
except Exception as e:
print("Error: unable to start thread - " + str(e))