Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 3cf0f0e

Browse files
Move python_check to new router file
1 parent 39d04af commit 3cf0f0e

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

bookserver/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .routers import books
3737
from .routers import rslogging
3838
from .routers import discuss
39+
from .routers import coach
3940
from .session import auth_manager
4041

4142

@@ -64,6 +65,7 @@
6465
app.include_router(assessment.router)
6566
app.include_router(auth.router)
6667
app.include_router(discuss.router)
68+
app.include_router(coach.router)
6769

6870
# We can mount various "apps" with mount. Anything that gets to this server with /staticAssets
6971
# will serve staticfiles - StaticFiles class implements the same interface as a FastAPI app.

bookserver/routers/books.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import random
1818
import socket
1919
from typing import Optional
20-
import ast
21-
import io
2220

2321
# Third-party imports
2422
# -------------------
@@ -27,7 +25,6 @@
2725
from fastapi.templating import Jinja2Templates
2826
from jinja2.exceptions import TemplateNotFound
2927
from pydantic import constr
30-
from pyflakes import checker as pyflakes_checker
3128

3229
# Local application imports
3330
# -------------------------
@@ -58,33 +55,6 @@
5855
tags=["books"],
5956
)
6057

61-
62-
63-
@router.post("/python_check")
64-
async def python_check(request: Request):
65-
"""
66-
Takes a chunk of Python code and runs a syntax checker (currently
67-
Pyflakes) on it to provide more detailed advice than is available
68-
via Skulpt.
69-
"""
70-
code_bytes = await request.body()
71-
code = code_bytes.decode("utf-8")
72-
73-
filename = "program.py"
74-
75-
resultMessage = ""
76-
try:
77-
tree = ast.parse(code, filename=filename)
78-
w = pyflakes_checker.Checker(tree, filename=filename)
79-
w.messages.sort(key=lambda m: m.lineno)
80-
for m in w.messages:
81-
resultMessage = resultMessage + str(m) + "\n"
82-
except SyntaxError as e:
83-
resultMessage = filename + ":" + str(e.lineno) + ":" + str(e.offset) + ": " + e.args[0] + "\n"
84-
85-
return resultMessage
86-
87-
8858
# Options for static asset renderers:
8959
#
9060
# - `StaticFiles <https://fastapi.tiangolo.com/tutorial/static-files/?h=+staticfiles#use-staticfiles>`_. However, this assumes the static routes are known *a priori*, in contrast to books (with their static assets) that are dynamically added and removed.

bookserver/routers/coach.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ***********************************
2+
# |docname| - Provide code advice
3+
# ***********************************
4+
# Endpoints to provide various kinds of advice (syntax/style/etc...)
5+
# about code samples
6+
#
7+
# Imports
8+
# =======
9+
# These are listed in the order prescribed by `PEP 8`_.
10+
#
11+
# Standard library
12+
# ----------------
13+
import ast
14+
15+
# Third-party imports
16+
# -------------------
17+
from fastapi import APIRouter, Request
18+
from pyflakes import checker as pyflakes_checker
19+
20+
# Local application imports
21+
# -------------------------
22+
23+
# .. _APIRouter config:
24+
#
25+
# Routing
26+
# =======
27+
# Setup the router object for the endpoints defined in this file. These will
28+
# be `connected <included routing>` to the main application in `../main.py`.
29+
router = APIRouter(
30+
# shortcut so we don't have to repeat this part
31+
prefix="/coach",
32+
tags=["coach"],
33+
)
34+
35+
36+
37+
@router.post("/python_check")
38+
async def python_check(request: Request):
39+
"""
40+
Takes a chunk of Python code and runs a syntax checker (currently
41+
Pyflakes) on it to provide more detailed advice than is available
42+
via Skulpt.
43+
"""
44+
code_bytes = await request.body()
45+
code = code_bytes.decode("utf-8")
46+
47+
filename = "program.py"
48+
49+
resultMessage = ""
50+
try:
51+
tree = ast.parse(code, filename=filename)
52+
w = pyflakes_checker.Checker(tree, filename=filename)
53+
w.messages.sort(key=lambda m: m.lineno)
54+
for m in w.messages:
55+
resultMessage = resultMessage + str(m) + "\n"
56+
except SyntaxError as e:
57+
resultMessage = filename + ":" + str(e.lineno) + ":" + str(e.offset) + ": " + e.args[0] + "\n"
58+
59+
return resultMessage

0 commit comments

Comments
 (0)