/
app.py
59 lines (42 loc) · 1.31 KB
/
app.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
# -*- coding: utf-8 -*-
import io
import sys
import flask
import pygments.cmdline
class AnyIO(io.IOBase):
"""A combination of StringIO and BytesIO
that supports writing str and bytes."""
def __init__(self):
self.bytes_io = io.BytesIO()
self.string_io = io.TextIOWrapper(self.bytes_io)
def write(self, content):
if isinstance(content, str):
return self.string_io.write(content)
else:
return self.bytes_io.write(content)
def getvalue(self):
self.string_io.flush()
return self.bytes_io.getvalue()
def no_open(file, mode='r', *args, **kwargs):
"""Fake open() function that always return empty objects."""
if 'b' in mode:
return io.BytesIO()
else:
return io.StringIO()
real_open = open
app = flask.Flask(__name__)
@app.post('/')
def index():
try:
sys.stdin = io.BytesIO(flask.request.data)
sys.stdin.buffer = sys.stdin
sys.stdout = AnyIO()
sys.stdout.buffer = sys.stdout
__builtins__['open'] = no_open
args = flask.request.args.getlist('args')
pygments.cmdline.main(['pygmentize', *args])
return sys.stdout.getvalue()
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
__builtins__['open'] = real_open