-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.py
82 lines (66 loc) · 2.68 KB
/
compiler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from sphere_engine import CompilersClientV4
from sphere_engine.exceptions import SphereEngineException
import time
# define access parameters
accessToken = '77501c36922866a03b1822b4508a50c6'
endpoint = 'dd57039c.compilers.sphere-engine.com'
# initialization
client = CompilersClientV4(accessToken, endpoint)
# API usage
# source = 'function f() {return "hello"; } print(f());' # Javascript
# compiler = 112 # Javascript
source = 'print("hello world please work!!!!!")' # Python
compiler = 116 # Python
input = '2017'
# Set default value for response
response = None
# Sends the submission and checks for errors in sending the submission
try:
response = client.submissions.create(source, compiler, input)
# response['id'] stores the ID of the created submission
except SphereEngineException as e:
if e.code == 401:
print('Invalid access token')
elif e.code == 402:
print('Unable to create submission')
elif e.code == 400:
print('Error code: ' + str(e.error_code) + ', details available in the message: ' + str(e))
# Set default value for response data
responseData = None
print("Code submitted is: ")
print(source)
print("Submission ID is: " + str(response.get('id')))
print()
# Try getting submission ID and check if there are errors
try:
client.submissions.get(response.get('id'))
except SphereEngineException as e:
if e.code == 401:
print('Invalid access token')
elif e.code == 403:
print('Access to the submission is forbidden')
elif e.code == 404:
print('Submission does not exist')
# Uses submission ID, and checks every x seconds to see if query has been 'accepted' (finished processing)
while client.submissions.get(response.get('id')).get('result').get('status').get('name') != 'accepted' :
responseData = client.submissions.get(response.get('id'))
print(responseData) # for test purposes
print("Status is: " + responseData.get('result').get('status').get('name'))
time.sleep(5)
print("Status is: " + client.submissions.get(response.get('id')).get('result').get('status').get('name'))
print()
rawresponse = None
# Get the output of the query
try:
rawresponse = client.submissions.getStream(response.get('id'), 'output')
except SphereEngineException as e:
if e.code == 401:
print('Invalid access token')
elif e.code == 403:
print('Access to the submission is forbidden')
elif e.code == 404:
print('Non existing resource, error code: ' + str(e.error_code) + ', details available in the message: ' + str(e))
elif e.code == 400:
print('Error code: ' + str(e.error_code) + ', details available in the message: ' + str(e))
print("Output returned is: ")
print(rawresponse)