Skip to content

Commit

Permalink
Merge pull request #135 from dbosk/submission-shell-option
Browse files Browse the repository at this point in the history
Submission shell option
  • Loading branch information
dbosk authored Feb 21, 2024
2 parents bb8e70b + ffa3200 commit 4818b30
Showing 1 changed file with 81 additions and 35 deletions.
116 changes: 81 additions & 35 deletions src/canvaslms/cli/submissions.nw
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,16 @@ for submission in submission_list:
history=args.history,
tmpdir=tmpdir/subdir)

<<write [[output]] to file in [[tmpdir/subdir]]>>

if args.output_dir:
<<write [[output]] to file in [[tmpdir/subdir]]>>
pass
elif args.open == "shell":
<<spawn shell in [[tmpdir/subdir]] for the user>>
elif sys.stdout.isatty():
<<check if we should use styles>>
<<open [[tmpdir/subdir]] for the user>>
if args.open == "open":
<<open [[tmpdir/subdir]] for the user>>
with console.pager(styles=styles):
console.print(rich.markdown.Markdown(output,
code_theme="manni"))
Expand All @@ -200,29 +205,31 @@ for submission in submission_list:
@ Note that we use the theme [[manni]] for the code, as this works well in both
dark and light terminals.

\subsection{Optional history}

Now, let's turn to that [[args.history]] argument.
We want to exclude it sometimes, for instance, when we want to get to the
comments only.
So we default to off, since it's only occasionally that we want to see the
history.
<<add options for printing a submission>>=
submission_parser.add_argument("-H", "--history", action="store_true",
help="Include submission history.")
@

\subsection{Specifying an output directory}

Next we have the [[args.output_dir]] argument.
If we specify the [[output_dir]] we want to write the output to files and not
have it printed to stdout.
<<add options for printing a submission>>=
submission_parser.add_argument("-o", "--output-dir",
required=False, default=None,
help="Write output to files in directory the given directory.")
help="Write output to files in directory the given directory. "
"If not specified, print to stdout. "
"If specified, do not print to stdout.")
@

We also have the open option, that has a choice of two alternatives.
<<add submission command options>>=
submission_parser.add_argument("--open", required=False,
nargs="?", default=None, const="open", choices=["open", "shell"],
help="Open the directory containing the files using "
"the default file manager (`open`) or "
"spawn a shell in the directory (`shell`). "
"With `open`, the pager will be used to display the output as usual. "
"With `shell`, we just drop into the shell, the output can be "
"found in the metadata.md file in the shell's working directory. "
"Default: %(default)s")
@

\subsection{Specifying an output directory}

If the user specified an output directory, we will not create a temporary
directory, but rather let [[tmpdir]] be the output directory.
<<create [[tmpdir]]>>=
Expand Down Expand Up @@ -272,17 +279,22 @@ with open(tmpdir/subdir/"metadata.md", "w") as f:
f.write(output)
@

\subsection{Opening the directory with the files}

We want to open the directory with the files if we use a pager.
However, we provide this as an option.
<<add submission command options>>=
submission_parser.add_argument("--open", required=False, default=False,
action="store_true",
help="Open the directory with the files in the default file manager.")
\subsection{Opening the directory containing the files}

Sometimes we want to open the directory.
There are several ways we can do this.
We can open the directory with the system file explorer, that way the user can
open files while reading the stdout output using a pager.
We can also spawn a shell in the directory so that the user can run the Python
code.
<<spawn shell in [[tmpdir/subdir]] for the user>>=
print(f"---> Opening a shell ({os.environ['SHELL']}) in {tmpdir/subdir}")
subprocess.run([
"sh", "-c", f"cd '{tmpdir/subdir}' && exec {os.environ['SHELL']}"
])
print(f"<--- canvaslms submission shell terminated.")
<<open [[tmpdir/subdir]] for the user>>=
if args.open:
subprocess.run(["open", tmpdir/subdir])
subprocess.run(["open", tmpdir/subdir])
@


Expand Down Expand Up @@ -310,6 +322,18 @@ the `-r` or `-R` flag is passed to `less`, it uses colours in the output.

@

\subsection{Optional history}

Now, let's turn to that [[args.history]] argument.
We want to exclude it sometimes, for instance, when we want to get to the
comments only.
So we default to off, since it's only occasionally that we want to see the
history.
<<add options for printing a submission>>=
submission_parser.add_argument("-H", "--history", action="store_true",
help="Include submission history.")
@


\section{Selecting a submission on the command line}%
\label{submission-options}
Expand Down Expand Up @@ -468,7 +492,7 @@ def format_submission(submission, history=False,
"""
Formats submission for printing to stdout. Returns a string.

If history is True, also include submission history.
If history is True, include all submission versions from history.
"""
student = submission.assignment.course.get_user(submission.user_id)

Expand All @@ -477,11 +501,12 @@ def format_submission(submission, history=False,
<<add metadata to output>>
<<add rubric assessment to output>>
<<add comments to output>>
<<add body to output>>
<<add quiz answers to output>>
<<add attachments to output>>
if history:
<<add submission history to output>>
else:
<<add body to output>>
<<add quiz answers to output>>
<<add attachments to output>>

return formatted_submission
@
Expand Down Expand Up @@ -598,7 +623,7 @@ try:
for attachment in submission.attachments:
<<let [[contents]] be the converted [[attachment]]>>
formatted_submission += format_section(attachment.filename,
contents)
contents)
except AttributeError:
pass
@
Expand All @@ -618,6 +643,7 @@ directory for all attachments and potentially all users.
tmpdir = None,
<<attachment iteration variables>>=
tmpdir = pathlib.Path(tmpdir or tempfile.mkdtemp())
tmpdir.mkdir(parents=True, exist_ok=True)
@

We'll use [[tmpdir]] as the temporary directory to store the files when we try
Expand Down Expand Up @@ -704,15 +730,35 @@ except Exception as err:

\subsection{Submission history}

The history contains all the student's submissions, including the current
submission.
We want to keep track of what belongs to the different versions.
This becomes important when we rely on the [[tmpdir]] to store the files.
We don't want the files of one version overwrite the files of another.
Then we can't be sure which version we're looking at.

To produce the history, we'll modify [[tmpdir]] to create a subdirectory for
each version.
We'll write a [[metadata.md]] file for each version.
Then we'll return all of those concatenated.
<<add submission history to output>>=
try:
if submission.submission_history:
for prev_submission in submission.submission_history:
for version, prev_submission in enumerate(submission.submission_history):
version = str(version)
prev_submission = canvasapi.submission.Submission(
submission._requester, prev_submission)
prev_submission.assignment = submission.assignment

formatted_submission += "\n\n" + format_submission(prev_submission)
version_dir = tmpdir / f"version-{version}"

prev_metadata = format_submission(prev_submission,
tmpdir=version_dir)

with open(version_dir/"metadata.md", "w") as f:
f.write(prev_metadata)

formatted_submission += "\n\n" + prev_metadata
except AttributeError:
pass
@
Expand Down

0 comments on commit 4818b30

Please sign in to comment.