Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if len(found) == 0:
self.log.warning("No notebooks were matched by '%s'", notebook_glob)
continue
self.assignments[assignment] = found
if len(self.assignments) == 0:
msg = "No notebooks were matched by '%s'" % assignment_glob
self.log.error(msg)
assignment_glob2 = self._format_source("*", self.coursedir.student_id)
found = glob.glob(assignment_glob2)
if found:
scores = sorted([(fuzz.ratio(assignment_glob, x), x) for x in found])
self.log.error("Did you mean: %s", scores[-1][1])
raise NbGraderException(msg)
def _clean_old_notebooks(self, assignment_id: str, student_id: str) -> None:
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
assignment = gb.find_assignment(assignment_id)
regexp = re.escape(os.path.sep).join([
self._format_source("(?P.*)", "(?P.*)", escape=True),
"(?P.*).ipynb"
])
# find a set of notebook ids for new notebooks
new_notebook_ids = set([])
for notebook in self.notebooks:
m = re.match(regexp, notebook)
if m is None:
raise NbGraderException("Could not match '%s' with regexp '%s'", notebook, regexp)
gd = m.groupdict()
if gd['assignment_id'] == assignment_id and gd['student_id'] == student_id:
new_notebook_ids.add(gd['notebook_id'])
# pull out the existing notebook ids
old_notebook_ids = set(x.name for x in assignment.notebooks)
# no added or removed notebooks, so nothing to do
if old_notebook_ids == new_notebook_ids:
return
# some notebooks have been removed, but there are submissions associated
# with the assignment, so we don't want to overwrite stuff
if len(assignment.submissions) > 0:
msg = "Cannot modify existing assignment '%s' because there are submissions associated with it" % assignment
self.log.error(msg)
notebooks = []
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
for notebook in self.notebooks:
notebook_id = os.path.splitext(os.path.basename(notebook))[0]
try:
gb.find_notebook(notebook_id, assignment_id)
except MissingEntry:
self.log.warning("Skipping unknown notebook: %s", notebook)
continue
else:
notebooks.append(notebook)
self.notebooks = notebooks
if len(self.notebooks) == 0:
msg = "No notebooks found, did you forget to run 'nbgrader generate_assignment'?"
self.log.error(msg)
raise NbGraderException(msg)
# check for missing notebooks and give them a score of zero if they
# do not exist
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
assignment = gb.find_assignment(assignment_id)
for notebook in assignment.notebooks:
path = os.path.join(self.coursedir.format_path(
self.coursedir.submitted_directory,
student_id,
assignment_id), "{}.ipynb".format(notebook.name))
if not os.path.exists(path):
self.log.warning("No submitted file: {}".format(path))
submission = gb.find_submission_notebook(
notebook.name, assignment_id, student_id)
for grade in submission.grades:
grade.auto_score = 0
def init_single_notebook_resources(self, notebook_filename: str) -> typing.Dict[str, typing.Any]:
regexp = re.escape(os.path.sep).join([
self._format_source("(?P.*)", "(?P.*)", escape=True),
"(?P.*).ipynb"
])
m = re.match(regexp, notebook_filename)
if m is None:
msg = "Could not match '%s' with regexp '%s'" % (notebook_filename, regexp)
self.log.error(msg)
raise NbGraderException(msg)
gd = m.groupdict()
self.log.debug("Student: %s", gd['student_id'])
self.log.debug("Assignment: %s", gd['assignment_id'])
self.log.debug("Notebook: %s", gd['notebook_id'])
resources = {}
resources['unique_key'] = gd['notebook_id']
resources['output_files_dir'] = '%s_files' % gd['notebook_id']
resources['nbgrader'] = {}
resources['nbgrader']['student'] = gd['student_id']
resources['nbgrader']['assignment'] = gd['assignment_id']
resources['nbgrader']['notebook'] = gd['notebook_id']
resources['nbgrader']['db_url'] = self.coursedir.db_url
for assignment_id, student_id in errors:
self.log.error(
"There was an error processing assignment '{}' for student '{}'".format(
assignment_id, student_id))
if self.logfile:
msg = (
"Please see the error log ({}) for details on the specific "
"errors on the above failures.".format(self.logfile))
else:
msg = (
"Please see the the above traceback for details on the specific "
"errors on the above failures.")
self.log.error(msg)
raise NbGraderException(msg)
def _clean_old_notebooks(self, assignment_id, student_id):
with Gradebook(self.coursedir.db_url) as gb:
assignment = gb.find_assignment(assignment_id)
regexp = re.escape(os.path.sep).join([
self._format_source("(?P.*)", "(?P.*)", escape=True),
"(?P.*).ipynb"
])
# find a set of notebook ids for new notebooks
new_notebook_ids = set([])
for notebook in self.notebooks:
m = re.match(regexp, notebook)
if m is None:
raise NbGraderException("Could not match '%s' with regexp '%s'", notebook, regexp)
gd = m.groupdict()
if gd['assignment_id'] == assignment_id and gd['student_id'] == student_id:
new_notebook_ids.add(gd['notebook_id'])
# pull out the existing notebook ids
old_notebook_ids = set(x.name for x in assignment.notebooks)
# no added or removed notebooks, so nothing to do
if old_notebook_ids == new_notebook_ids:
return
# some notebooks have been removed, but there are submissions associated
# with the assignment, so we don't want to overwrite stuff
if len(assignment.submissions) > 0:
msg = "Cannot modify existing assignment '%s' because there are submissions associated with it" % assignment
self.log.error(msg)
if gd['assignment_id'] == assignment_id and gd['student_id'] == student_id:
new_notebook_ids.add(gd['notebook_id'])
# pull out the existing notebook ids
old_notebook_ids = set(x.name for x in assignment.notebooks)
# no added or removed notebooks, so nothing to do
if old_notebook_ids == new_notebook_ids:
return
# some notebooks have been removed, but there are submissions associated
# with the assignment, so we don't want to overwrite stuff
if len(assignment.submissions) > 0:
msg = "Cannot modify existing assignment '%s' because there are submissions associated with it" % assignment
self.log.error(msg)
raise NbGraderException(msg)
# remove the old notebooks
for notebook_id in (old_notebook_ids - new_notebook_ids):
self.log.warning("Removing notebook '%s' from the gradebook", notebook_id)
gb.remove_notebook(notebook_id, assignment_id)
if student or self.create_student:
if 'id' in student:
del student['id']
self.log.info("Creating/updating student with ID '%s': %s", student_id, student)
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
gb.update_or_create_student(student_id, **student)
else:
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
try:
gb.find_student(student_id)
except MissingEntry:
msg = "No student with ID '%s' exists in the database" % student_id
self.log.error(msg)
raise NbGraderException(msg)
# make sure the assignment exists
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
try:
gb.find_assignment(assignment_id)
except MissingEntry:
msg = "No assignment with ID '%s' exists in the database" % assignment_id
self.log.error(msg)
raise NbGraderException(msg)
# try to read in a timestamp from file
src_path = self._format_source(assignment_id, student_id)
timestamp = self.coursedir.get_existing_timestamp(src_path)
with Gradebook(self.coursedir.db_url, self.coursedir.course_id) as gb:
if timestamp:
submission = gb.update_or_create_submission(