|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# wandbox.py |
|
4
|
|
|
# |
|
5
|
|
|
|
|
6
|
|
|
import requests |
|
7
|
|
|
import json |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
# |
|
11
|
|
|
# |
|
12
|
|
|
class Wandbox: |
|
13
|
|
|
"""wandbox api class""" |
|
14
|
|
|
api_url = 'http://melpon.org/wandbox/api' |
|
15
|
|
|
parameter = {'code': ''} |
|
16
|
|
|
|
|
17
|
|
|
def get_compiler_list(self): |
|
18
|
|
|
r = requests.get(self.api_url + '/list.json') |
|
19
|
|
|
r.raise_for_status() |
|
20
|
|
|
return r.json() |
|
21
|
|
|
|
|
22
|
|
|
def run(self): |
|
23
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
|
24
|
|
|
payload = json.dumps(self.parameter) |
|
25
|
|
|
r = requests.post(self.api_url + '/compile.json', data=payload, headers=headers) |
|
26
|
|
|
r.raise_for_status() |
|
27
|
|
|
return r.json() |
|
28
|
|
|
|
|
29
|
|
|
def get_permlink(self, link): |
|
30
|
|
|
r = requests.get(self.api_url + '/permlink/' + link) |
|
31
|
|
|
r.raise_for_status() |
|
32
|
|
|
return r.json() |
|
33
|
|
|
|
|
34
|
|
|
def code(self, str): |
|
35
|
|
|
self.parameter.update({'code': str}) |
|
36
|
|
|
|
|
37
|
|
|
def add_file(self, filename, str): |
|
38
|
|
|
if 'codes' in self.parameter: |
|
39
|
|
|
self.parameter['codes'].append({'file': filename, 'code': str}) |
|
40
|
|
|
else: |
|
41
|
|
|
self.parameter.update({'codes': [{'file': filename, 'code': str}]}) |
|
42
|
|
|
|
|
43
|
|
|
def compiler(self, str): |
|
44
|
|
|
self.parameter.update({'compiler': str}) |
|
45
|
|
|
|
|
46
|
|
|
def options(self, str): |
|
47
|
|
|
self.parameter.update({'options': str}) |
|
48
|
|
|
|
|
49
|
|
|
def stdin(self, str): |
|
50
|
|
|
self.parameter.update({'stdin': str}) |
|
51
|
|
|
|
|
52
|
|
|
def compiler_options(self, str): |
|
53
|
|
|
self.parameter.update({'compiler-option-raw': str}) |
|
54
|
|
|
|
|
55
|
|
|
def add_compiler_options(self, str): |
|
56
|
|
|
if 'compiler-option-raw' not in self.parameter: |
|
57
|
|
|
self.compiler_options(str) |
|
58
|
|
|
else: |
|
59
|
|
|
option = self.parameter['compiler-option-raw'] |
|
60
|
|
|
option += '\n' |
|
61
|
|
|
option += str |
|
62
|
|
|
self.parameter.update({'compiler-option-raw': option}) |
|
63
|
|
|
|
|
64
|
|
|
def runtime_options(self, str): |
|
65
|
|
|
self.parameter.update({'runtime-option-raw': str}) |
|
66
|
|
|
|
|
67
|
|
|
def permanent_link(self, b): |
|
68
|
|
|
self.parameter.update({'save': b}) |
|
69
|
|
|
|
|
70
|
|
|
def dump(self): |
|
71
|
|
|
print(self.parameter) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if __name__ == '__main__': |
|
75
|
|
|
w = Wandbox() |
|
76
|
|
|
w.compiler('gcc-head') |
|
77
|
|
|
w.options('warning,gnu++1y') |
|
78
|
|
|
w.compiler_options('-Dx=hogefuga\n-O3') |
|
79
|
|
|
w.code('#include <iostream>\nint main() { int x = 0; std::cout << "hoge" << std::endl; }') |
|
80
|
|
|
print(w.run()) |
|
81
|
|
|
|