wandbox   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 21

19 Methods

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