Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contents-related commands #40

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions canvas_cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ users.tex
assignments.tex
submissions.tex
grades.tex
contents.tex
3 changes: 3 additions & 0 deletions canvas_cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ canvas.py: submissions.nw
canvas.pdf: grades.tex
canvas.py: grades.nw

canvas.pdf: contents.tex
canvas.py: contents.nw


.PHONY: all
all: canvas.bash
Expand Down
4 changes: 4 additions & 0 deletions canvas_cli/canvas.nw
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ if args.func:
\input{grades.tex}


\chapter{The contents commands}

\input{contents.tex}


\printbibliography{}

Expand Down
50 changes: 50 additions & 0 deletions canvas_cli/contents.nw
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
This chapter provides the subcommand [[contents]], which manages content in a
course.

\section{The [[contents]] subcommand and its options}

We add the subparser for [[contents]].
<<add arguments>>=
contents_parser = subp.add_parser("contents",
help="Lists contents of a course")
contents_parser.set_defaults(func=contents_command)
add_course_options(contents_parser)
@ Now, that [[contents_command]] function must take two arguments: [[canvas]]
and [[args]].
We use [[process_course_options]] to parse the options that we added with the
[[add_course_options]] function above.
<<functions>>=
def contents_command(canvas, args):
course_list = process_course_options(canvas, args)
<<get contents and print them>>
@

\subsection{Get and print the list of contents}

By default, we simply list the contents of the course.
We start with the modules and do a depth-first search through the
contents.
<<get contents and print them>>=
for course in course_list:
print(format_course(course), end="")
@

The format functions place their output in a string and returns it.
This way the calling function can concatenate the outputs.
We also want to specify some indentation using the [[indent]] parameter.
<<functions>>=
def format_course(course, indent=""):
out = f"{indent}{course.course_code} {course.name}\n"
for module in course.get_modules():
out += format_module(module)
return out

def format_module(module, indent=" "):
out = f"{indent}{module.name}\n"
for item in module.get_module_items():
out += format_item(item)
return out

def format_item(item, indent=" "):
return f"{indent}{item.type:12s} {item.title}\n"
@