-
Notifications
You must be signed in to change notification settings - Fork 2
/
builtins.py
66 lines (51 loc) · 1.77 KB
/
builtins.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
# coding: utf-8
import re
try:
import __builtin__ as builtins
except:
import builtins
from random import choice
from flask import Flask
from flask import render_template
from flask import jsonify
from flask import request
from flask import make_response
# from pygments import highlight
# from pygments.lexers import PythonLexer
# from pygments.formatters import HtmlFormatter
app = Flask(__name__)
re_module = re.compile(r'([a-z]|__import__)')
choices = [i for i in dir(builtins) if re_module.match(i)]
@app.route("/")
@app.route("/<builtin>")
def builtin(builtin=None):
''' Get specified builtin or fail '''
if builtin is None:
builtin = choice(choices)
if builtin not in choices:
return render_template('404.html'), 404
help_text = getattr(builtins, builtin).__doc__
return render_template('show.html', builtin=builtin, choices=choices,
help=help_text)
@app.route('/api/v1.0/builtins', methods=['GET'])
def get_builtins():
resp = []
for builtin in choices:
help_text = getattr(builtins, builtin).__doc__
resp.append({'name': builtin, 'help_text':help_text})
return jsonify({'builtins': resp})
@app.route('/api/v1.0/builtins/<builtin>', methods=['GET'])
def get_builtin(builtin=None):
if builtin is None or builtin not in choices:
return jsonify({'error', 'Resource not found'})
help_text = getattr(builtins, builtin).__doc__
resp = {'name': builtin, 'help_text': help_text}
return jsonify({'builtin': resp})
@app.errorhandler(404)
def not_found(error):
# REST Errors
if 'api/v1.0' in request.url:
return make_response(jsonify( { 'error': 'Not found' } ), 404)
return render_template('404.html'), 404
if __name__ == '__main__':
app.run(debug=True)