-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (69 loc) · 2.48 KB
/
main.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
import os
import subprocess
import tempfile
import sus2ymst
import uvicorn
from fastapi import FastAPI, Form, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
def notation_text_to_svg(notation_text: str) -> str:
with tempfile.TemporaryDirectory() as temp_dir:
text_file_path = os.path.join(temp_dir, "notation.txt")
with open(text_file_path, "w") as f:
f.write(notation_text)
ret = subprocess.run(["php", "svg.php", temp_dir])
if ret.returncode != 0:
raise HTTPException(status_code=500, detail="Failed to convert")
svg_file_path = os.path.join(temp_dir, "notation.svg")
with open(svg_file_path, "r", encoding="utf-8") as f:
svg_text = f.read()
return svg_text
@app.post("/convert")
async def convert(
request: Request,
chart: str = Form(),
textFlag: bool = Form(),
laneFlag: bool = Form(),
sampleFlag: bool = Form(),
):
# サンプルの場合は、サンプルを返す
if sampleFlag:
with open("sample.sus", "r", encoding="utf-8") as f:
chart = f.read()
notation_txt, error_messages = sus2ymst.loads(chart)
svg_text = notation_text_to_svg(notation_txt)
return templates.TemplateResponse(
"convert.html",
{"request": request, "svg_text": svg_text},
)
if textFlag:
svg_text = notation_text_to_svg(chart)
return templates.TemplateResponse(
"convert.html", {"request": request, "svg_text": svg_text}
)
else:
lane_offset = 0 if laneFlag else 2
try:
notation_txt, error_messages = sus2ymst.loads(chart, lane_offset)
svg_text = notation_text_to_svg(notation_txt)
except Exception:
raise HTTPException(status_code=500)
return templates.TemplateResponse(
"convert.html",
{
"request": request,
"svg_text": svg_text,
"error_messages": error_messages,
},
)
@app.get("/")
async def index(request: Request):
client_host = request.client.host
return templates.TemplateResponse(
"index.html", {"request": request, "host": client_host}
)
if __name__ == "__main__":
uvicorn.run("main:app", port=8080, reload=True)