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

Commit 35b5d0c

Browse files
committed
Merge branch 'ascholerChemeketa-python_check-endpoint'
2 parents 5f4c400 + 1f0f759 commit 35b5d0c

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
tags=["books"],
5656
)
5757

58-
5958
# Options for static asset renderers:
6059
#
6160
# - `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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
@router.post("/python_check")
37+
async def python_check(request: Request):
38+
"""
39+
Takes a chunk of Python code and runs a syntax checker (currently
40+
Pyflakes) on it to provide more detailed advice than is available
41+
via Skulpt.
42+
"""
43+
code_bytes = await request.body()
44+
code = code_bytes.decode("utf-8")
45+
46+
filename = "program.py"
47+
48+
resultMessage = ""
49+
try:
50+
tree = ast.parse(code, filename=filename)
51+
w = pyflakes_checker.Checker(tree, filename=filename)
52+
w.messages.sort(key=lambda m: m.lineno)
53+
for m in w.messages:
54+
resultMessage = resultMessage + str(m) + "\n"
55+
except SyntaxError as e:
56+
resultMessage = (
57+
filename
58+
+ ":"
59+
+ str(e.lineno)
60+
+ ":"
61+
+ str(e.offset)
62+
+ ": "
63+
+ e.args[0]
64+
+ "\n"
65+
)
66+
67+
return resultMessage

bookserver/routers/rslogging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ async def updatelastpage(
337337
practice_settings = await fetch_course_practice(user.course_name)
338338
if RS_info:
339339
values = json.loads(RS_info)
340-
tz_offset = float(values["tz_offset"])
340+
341+
tz_offset = float(values.get("tz_offset", 0))
341342
else:
342343
tz_offset = 0
343344

0 commit comments

Comments
 (0)