1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# wandbox.py |
4
|
|
|
# |
5
|
|
|
|
6
|
|
|
""" |
7
|
|
|
Wandbox API for Python |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
import requests |
11
|
|
|
import json |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
# |
15
|
|
|
# |
16
|
|
|
class Wandbox: |
17
|
|
|
"""wandbox api class""" |
18
|
|
|
|
19
|
|
|
#api_url = 'http://melpon.org/wandbox/api' |
20
|
|
|
api_url = 'https://wandbox.org/api' |
21
|
|
|
|
22
|
|
|
def __init__(self): |
23
|
|
|
self.reset() |
24
|
|
|
|
25
|
|
|
def __enter__(self): |
26
|
|
|
return self |
27
|
|
|
|
28
|
|
|
def __exit__(self, exc_type, exc_value, traceback): |
29
|
|
|
return True |
30
|
|
|
|
31
|
|
|
@staticmethod |
32
|
|
|
def GetCompilerList(): |
33
|
|
|
""" |
34
|
|
|
get compiler list |
35
|
|
|
""" |
36
|
|
|
response = requests.get(Wandbox.api_url + '/list.json') |
37
|
|
|
response.raise_for_status() |
38
|
|
|
return response.json() |
39
|
|
|
|
40
|
|
|
def get_compiler_list(self): |
41
|
|
|
""" |
42
|
|
|
get compiler list |
43
|
|
|
.. deprecated:: 0.3.4 |
44
|
|
|
""" |
45
|
|
|
return Wandbox.GetCompilerList() |
46
|
|
|
|
47
|
|
|
@staticmethod |
48
|
|
|
def GetPermlink(link): |
49
|
|
|
""" |
50
|
|
|
get wandbox permanet link |
51
|
|
|
""" |
52
|
|
|
response = requests.get(Wandbox.api_url + '/permlink/' + link) |
53
|
|
|
response.raise_for_status() |
54
|
|
|
return response.json() |
55
|
|
|
|
56
|
|
|
def get_permlink(self, link): |
57
|
|
|
""" |
58
|
|
|
get wandbox permanet link |
59
|
|
|
.. deprecated:: 0.3.4 |
60
|
|
|
""" |
61
|
|
|
return Wandbox.GetPermlink(link) |
62
|
|
|
|
63
|
|
|
def run(self): |
64
|
|
|
""" |
65
|
|
|
excute on wandbox |
66
|
|
|
""" |
67
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
68
|
|
|
payload = json.dumps(self.parameter) |
69
|
|
|
response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers) |
70
|
|
|
response.raise_for_status() |
71
|
|
|
return response.json() |
72
|
|
|
|
73
|
|
|
def code(self, code): |
74
|
|
|
""" |
75
|
|
|
set main source code |
76
|
|
|
""" |
77
|
|
|
self.parameter.update({'code': code}) |
78
|
|
|
|
79
|
|
|
def add_file(self, filename, code): |
80
|
|
|
""" |
81
|
|
|
append file |
82
|
|
|
""" |
83
|
|
|
if 'codes' in self.parameter: |
84
|
|
|
self.parameter['codes'].append({'file': filename, 'code': code}) |
85
|
|
|
else: |
86
|
|
|
self.parameter.update({'codes': [{'file': filename, 'code': code}]}) |
87
|
|
|
|
88
|
|
|
def compiler(self, name): |
89
|
|
|
""" |
90
|
|
|
set compiler name |
91
|
|
|
""" |
92
|
|
|
self.parameter.update({'compiler': name}) |
93
|
|
|
|
94
|
|
|
def options(self, options_str): |
95
|
|
|
""" |
96
|
|
|
set wandbox options |
97
|
|
|
""" |
98
|
|
|
self.parameter.update({'options': options_str}) |
99
|
|
|
|
100
|
|
|
def stdin(self, input_str): |
101
|
|
|
""" |
102
|
|
|
set stdin buffer |
103
|
|
|
""" |
104
|
|
|
self.parameter.update({'stdin': input_str}) |
105
|
|
|
|
106
|
|
|
def compiler_options(self, options_str): |
107
|
|
|
""" |
108
|
|
|
set wandbox defined compiler options |
109
|
|
|
""" |
110
|
|
|
self.parameter.update({'compiler-option-raw': options_str}) |
111
|
|
|
|
112
|
|
|
def add_compiler_options(self, options_str): |
113
|
|
|
""" |
114
|
|
|
set compiler options |
115
|
|
|
""" |
116
|
|
|
if 'compiler-option-raw' not in self.parameter: |
117
|
|
|
self.compiler_options(options_str) |
118
|
|
|
else: |
119
|
|
|
option = self.parameter['compiler-option-raw'] |
120
|
|
|
option += '\n' |
121
|
|
|
option += options_str |
122
|
|
|
self.parameter.update({'compiler-option-raw': option}) |
123
|
|
|
|
124
|
|
|
def runtime_options(self, options_str): |
125
|
|
|
""" |
126
|
|
|
set runtime options |
127
|
|
|
""" |
128
|
|
|
self.parameter.update({'runtime-option-raw': options_str}) |
129
|
|
|
|
130
|
|
|
def permanent_link(self, enable): |
131
|
|
|
""" |
132
|
|
|
wandbox permanet link to enable |
133
|
|
|
""" |
134
|
|
|
self.parameter.update({'save': enable}) |
135
|
|
|
|
136
|
|
|
def dump(self): |
137
|
|
|
""" |
138
|
|
|
dump parameters |
139
|
|
|
""" |
140
|
|
|
print(self.parameter) |
141
|
|
|
|
142
|
|
|
def reset(self): |
143
|
|
|
""" |
144
|
|
|
reset parametes |
145
|
|
|
""" |
146
|
|
|
self.parameter = {'code': ''} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
if __name__ == '__main__': |
150
|
|
|
with Wandbox() as w: |
151
|
|
|
w.compiler('gcc-head') |
152
|
|
|
w.options('warning,gnu++1y') |
153
|
|
|
w.compiler_options('-Dx=hogefuga\n-O3') |
154
|
|
|
w.code('#include <iostream>\nint main() { int x = 0; std::cout << "hoge" << std::endl; }') |
155
|
|
|
print(w.run()) |
156
|
|
|
|