/
app.py
154 lines (121 loc) · 3.94 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import json
import base64
import lzma
import black
import urllib
from flask import Flask, render_template, request, redirect, url_for, jsonify
from flask_cors import cross_origin
BASE_URL = "https://black.now.sh"
BLACK_VERSION = os.getenv("BLACK_VERSION")
def get_black_version():
lockfile = json.load(open("./Pipfile.lock"))
package = lockfile["default"]["black"]
version = package.get("version")
ref = package.get("ref")
if version:
return version.lstrip("==")
if ref:
return ref[0:6]
app = Flask(__name__)
black_version = get_black_version()
def compress_state(data):
compressed = lzma.compress(json.dumps(data).encode("utf-8"))
return base64.urlsafe_b64encode(compressed).decode("utf-8")
def decompress_state(state):
compressed = base64.urlsafe_b64decode(state)
return json.loads(lzma.decompress(compressed))
def format_code(source, configuration):
try:
mode = black.FileMode(**configuration)
formatted = black.format_str(source, mode=mode)
except Exception as exc:
formatted = f"{exc}"
return formatted
@app.route("/", methods=["POST", "GET"])
@cross_origin()
def index():
if request.method == "POST":
data = request.get_json()
source = data.get("source")
options = data.get("options", {})
line_length = int(options.get("line_length", 88))
skip_string_normalization = bool(
options.get("skip_string_normalization", False)
)
py36 = bool(options.get("py36", False))
pyi = bool(options.get("pyi", False))
else:
state = request.args.get("state")
if state:
state = decompress_state(state)
source = state.get("sc")
line_length = state.get("ll")
skip_string_normalization = state.get("ssn")
py36 = state.get("py36")
pyi = state.get("pyi")
else:
source = render_template("source.py")
line_length = 88
skip_string_normalization = False
py36 = False
pyi = False
formatted = format_code(
source,
configuration={
"target_versions": black.PY36_VERSIONS if py36 else set(),
"line_length": line_length,
"is_pyi": pyi,
"string_normalization": not skip_string_normalization
},
)
state = compress_state(
{
"sc": source,
"ll": line_length,
"ssn": skip_string_normalization,
"py36": py36,
"pyi": pyi,
}
)
options = [f"`--line-length={line_length}`"]
if skip_string_normalization:
options.append("`--skip-string-normalization`")
if py36:
options.append("`--py36`")
if pyi:
options.append("`--pyi`")
if BLACK_VERSION == "stable":
version = f"v{black_version}"
else:
version = f"https://github.com/python/black/commit/{black_version}"
issue_data = {
"source_code": source,
"formatted_code": formatted,
"options": "\n".join(options),
"version": version,
"playground_link": f"{BASE_URL}/?version={BLACK_VERSION}&state={state}",
}
issue_body = urllib.parse.quote_plus(render_template("issue.md", **issue_data))
issue_link = f"https://github.com/python/black/issues/new?body={issue_body}"
return jsonify(
{
"source_code": source,
"formatted_code": formatted,
"options": {
"line_length": line_length,
"skip_string_normalization": skip_string_normalization,
"py36": py36,
"pyi": pyi,
},
"state": state,
"issue_link": issue_link,
"version": black_version,
}
)
@app.route("/version", methods=["GET"])
@cross_origin()
def version():
return jsonify({"version": black_version})
if __name__ == "__main__":
app.run(debug=True)