Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bookserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .routers import books
from .routers import rslogging
from .routers import discuss
from .routers import coach
from .session import auth_manager


Expand Down Expand Up @@ -64,6 +65,7 @@
app.include_router(assessment.router)
app.include_router(auth.router)
app.include_router(discuss.router)
app.include_router(coach.router)

# We can mount various "apps" with mount. Anything that gets to this server with /staticAssets
# will serve staticfiles - StaticFiles class implements the same interface as a FastAPI app.
Expand Down
1 change: 0 additions & 1 deletion bookserver/routers/books.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
tags=["books"],
)


# Options for static asset renderers:
#
# - `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.
Expand Down
59 changes: 59 additions & 0 deletions bookserver/routers/coach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ***********************************
# |docname| - Provide code advice
# ***********************************
# Endpoints to provide various kinds of advice (syntax/style/etc...)
# about code samples
#
# Imports
# =======
# These are listed in the order prescribed by `PEP 8`_.
#
# Standard library
# ----------------
import ast

# Third-party imports
# -------------------
from fastapi import APIRouter, Request
from pyflakes import checker as pyflakes_checker

# Local application imports
# -------------------------

# .. _APIRouter config:
#
# Routing
# =======
# Setup the router object for the endpoints defined in this file. These will
# be `connected <included routing>` to the main application in `../main.py`.
router = APIRouter(
# shortcut so we don't have to repeat this part
prefix="/coach",
tags=["coach"],
)



@router.post("/python_check")
async def python_check(request: Request):
"""
Takes a chunk of Python code and runs a syntax checker (currently
Pyflakes) on it to provide more detailed advice than is available
via Skulpt.
"""
code_bytes = await request.body()
code = code_bytes.decode("utf-8")

filename = "program.py"

resultMessage = ""
try:
tree = ast.parse(code, filename=filename)
w = pyflakes_checker.Checker(tree, filename=filename)
w.messages.sort(key=lambda m: m.lineno)
for m in w.messages:
resultMessage = resultMessage + str(m) + "\n"
except SyntaxError as e:
resultMessage = filename + ":" + str(e.lineno) + ":" + str(e.offset) + ": " + e.args[0] + "\n"

return resultMessage