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
|
|
|
api_url = 'https://wandbox.org/api' |
16
|
|
|
|
17
|
|
|
def __init__(self): |
18
|
|
|
self.reset() |
19
|
|
|
|
20
|
|
|
def __enter__(self): |
21
|
|
|
return self |
22
|
|
|
|
23
|
|
|
def __exit__(self, exc_type, exc_value, traceback): |
24
|
|
|
return True |
25
|
|
|
|
26
|
|
|
@staticmethod |
27
|
|
|
def GetCompilerList(): |
28
|
|
|
r = requests.get(Wandbox.api_url + '/list.json') |
29
|
|
|
r.raise_for_status() |
30
|
|
|
return r.json() |
31
|
|
|
|
32
|
|
|
def get_compiler_list(self): |
33
|
|
|
return Wandbox.GetCompilerList() |
34
|
|
|
|
35
|
|
|
@staticmethod |
36
|
|
|
def GetPermlink(link): |
37
|
|
|
r = requests.get(Wandbox.api_url + '/permlink/' + link) |
38
|
|
|
r.raise_for_status() |
39
|
|
|
return r.json() |
40
|
|
|
|
41
|
|
|
def get_permlink(self, link): |
42
|
|
|
return Wandbox.GetPermlink(link) |
43
|
|
|
|
44
|
|
|
def run(self): |
45
|
|
|
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} |
46
|
|
|
payload = json.dumps(self.parameter) |
47
|
|
|
r = requests.post(self.api_url + '/compile.json', data=payload, headers=headers) |
48
|
|
|
r.raise_for_status() |
49
|
|
|
return r.json() |
50
|
|
|
|
51
|
|
|
def code(self, str): |
52
|
|
|
self.parameter.update({'code': str}) |
53
|
|
|
|
54
|
|
|
def add_file(self, filename, str): |
55
|
|
|
if 'codes' in self.parameter: |
56
|
|
|
self.parameter['codes'].append({'file': filename, 'code': str}) |
57
|
|
|
else: |
58
|
|
|
self.parameter.update({'codes': [{'file': filename, 'code': str}]}) |
59
|
|
|
|
60
|
|
|
def compiler(self, str): |
61
|
|
|
self.parameter.update({'compiler': str}) |
62
|
|
|
|
63
|
|
|
def options(self, str): |
64
|
|
|
self.parameter.update({'options': str}) |
65
|
|
|
|
66
|
|
|
def stdin(self, str): |
67
|
|
|
self.parameter.update({'stdin': str}) |
68
|
|
|
|
69
|
|
|
def compiler_options(self, str): |
70
|
|
|
self.parameter.update({'compiler-option-raw': str}) |
71
|
|
|
|
72
|
|
|
def add_compiler_options(self, str): |
73
|
|
|
if 'compiler-option-raw' not in self.parameter: |
74
|
|
|
self.compiler_options(str) |
75
|
|
|
else: |
76
|
|
|
option = self.parameter['compiler-option-raw'] |
77
|
|
|
option += '\n' |
78
|
|
|
option += str |
79
|
|
|
self.parameter.update({'compiler-option-raw': option}) |
80
|
|
|
|
81
|
|
|
def runtime_options(self, str): |
82
|
|
|
self.parameter.update({'runtime-option-raw': str}) |
83
|
|
|
|
84
|
|
|
def permanent_link(self, b): |
85
|
|
|
self.parameter.update({'save': b}) |
86
|
|
|
|
87
|
|
|
def dump(self): |
88
|
|
|
print(self.parameter) |
89
|
|
|
|
90
|
|
|
def close(self): |
91
|
|
|
self.reset() |
92
|
|
|
|
93
|
|
|
def reset(self): |
94
|
|
|
self.parameter = {'code': ''} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
if __name__ == '__main__': |
98
|
|
|
with Wandbox() as w: |
99
|
|
|
w.compiler('gcc-head') |
100
|
|
|
w.options('warning,gnu++1y') |
101
|
|
|
w.compiler_options('-Dx=hogefuga\n-O3') |
102
|
|
|
w.code('#include <iostream>\nint main() { int x = 0; std::cout << "hoge" << std::endl; }') |
103
|
|
|
print(w.run()) |
104
|
|
|
|