-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
70 lines (51 loc) · 1.71 KB
/
Copy pathapi.py
File metadata and controls
70 lines (51 loc) · 1.71 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
import web
import json
import driver
urls = (
'/race', 'race'
)
class race:
def GET(self):
message = {}
data = web.input()
if 'start' not in data or str(data.start.encode('utf-8')) is '':
self.addError(message, 'please specify a start page')
if 'end' not in data or str(data.end.encode('utf-8')) is '':
self.addError(message, 'please specify a end page')
if 'start' in data and 'end' in data:
timeout = 60
threads = 1
# read in timeout if specified
if 'timeout' in data:
try:
if int(data.timeout) > 0 and int(data.timeout) < 1000:
timeout = int(data.timeout)
else:
self.addError(message, 'invalid timeout - please specify a timeout between 1 and 999')
except ValueError as e:
self.addError(message, 'invalid timeout - timeout could not be read as int')
# read in threads if specified
if 'threads' in data:
try:
if int(data.threads) > 0 and int(data.threads) < 10:
threads = int(data.threads)
else:
self.addError(message, 'invalid thread count - please specify a thread count between 1 and 9')
except ValueError as e:
self.addError(message, 'invalid thread count - thread count could not be read as int')
# run the racers
if 'errors' not in message:
result = driver.drive(data.start, data.end, timeout, threads)
if 'error' in result:
self.addError(message, result['error'])
elif 'path' in result:
message['result'] = result['path']
message['time'] = result['time']
return json.dumps(message)
def addError(self, message, text):
if 'errors' not in message:
message['errors'] = []
message['errors'].append(text)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()