Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def gradebook(self):
gb = self.settings['nbgrader_gradebook']
if gb is None:
self.log.debug("creating gradebook")
gb = Gradebook(self.db_url, self.coursedir.course_id)
self.settings['nbgrader_gradebook'] = gb
return gb
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'])
def start(self):
super(DbStudentRemoveApp, self).start()
if len(self.extra_args) != 1:
self.fail("No student id provided.")
student_id = self.extra_args[0]
with Gradebook(self.coursedir.db_url) as gb:
try:
student = gb.find_student(student_id)
except MissingEntry:
self.fail("No such student: '%s'", student_id)
if len(student.submissions) > 0:
if self.force:
self.log.warning("Removing associated grades")
else:
self.log.warning("!!! There are grades in the database for student '%s'.", student_id)
self.log.warning("!!! Removing this student will also remove these grades.")
self.fail("!!! If you are SURE this is what you want to do, rerun with --force.")
self.log.info("Removing student with ID '%s'", student_id)
gb.remove_student(student_id)
self.notebook_id = resources['nbgrader']['notebook']
self.assignment_id = resources['nbgrader']['assignment']
self.db_url = resources['nbgrader']['db_url']
if self.notebook_id == '':
raise ValueError("Invalid notebook id: '{}'".format(self.notebook_id))
if self.assignment_id == '':
raise ValueError("Invalid assignment id: '{}'".format(self.assignment_id))
# create a place to put new cell information
self.new_grade_cells = {}
self.new_solution_cells = {}
self.new_source_cells = {}
# connect to the database
self.gradebook = Gradebook(self.db_url)
try:
nb, resources = super(SaveCells, self).preprocess(nb, resources)
# create the notebook and save it to the database
self._create_notebook()
finally:
self.gradebook.close()
return nb, resources
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(
assignment_id, student_id, timestamp=timestamp)
self.log.info("%s submitted at %s", submission, timestamp)
def start(self):
super(DbStudentListApp, self).start()
with Gradebook(self.coursedir.db_url) as gb:
print("There are %d students in the database:" % len(gb.students))
for student in gb.students:
print("%s (%s, %s) -- %s" % (student.id, student.last_name, student.first_name, student.email))
if not self.no_database:
assignment = {}
for a in self.coursedir.db_assignments:
if a['name'] == assignment_id:
assignment = a.copy()
break
if assignment or self.create_assignment:
if 'name' in assignment:
del assignment['name']
self.log.info("Updating/creating assignment '%s': %s", assignment_id, assignment)
with Gradebook(self.coursedir.db_url) as gb:
gb.update_or_create_assignment(assignment_id, **assignment)
else:
with Gradebook(self.coursedir.db_url) as gb:
try:
gb.find_assignment(assignment_id)
except MissingEntry:
msg = "No assignment called '%s' exists in the database" % assignment_id
self.log.error(msg)
raise NbGraderException(msg)
# check if there are any extra notebooks in the db that are no longer
# part of the assignment, and if so, remove them
if self.coursedir.notebook_id == "*":
self._clean_old_notebooks(assignment_id, student_id)
def preprocess(self, nb: NotebookNode, resources: ResourcesDict) -> Tuple[NotebookNode, ResourcesDict]:
# pull information from the resources
self.notebook_id = resources['nbgrader']['notebook']
self.assignment_id = resources['nbgrader']['assignment']
self.student_id = resources['nbgrader']['student']
self.db_url = resources['nbgrader']['db_url']
# init the plugin
self.init_plugin()
# connect to the database
self.gradebook = Gradebook(self.db_url)
with self.gradebook:
# process the late submissions
nb, resources = super(AssignLatePenalties, self).preprocess(nb, resources)
assignment = self.gradebook.find_submission(
self.assignment_id, self.student_id)
notebook = self.gradebook.find_submission_notebook(
self.notebook_id, self.assignment_id, self.student_id)
# reset to None (zero)
notebook.late_submission_penalty = None
if assignment.total_seconds_late > 0:
self.log.warning("{} is {} seconds late".format(
assignment, assignment.total_seconds_late))
def init_assignment(self, assignment_id, student_id):
super(AssignApp, self).init_assignment(assignment_id, student_id)
# try to get the assignment from the database, and throw an error if it
# doesn't exist
if not self.no_database:
assignment = None
for a in self.db_assignments:
if a['name'] == assignment_id:
assignment = a.copy()
break
if assignment is not None:
del assignment['name']
self.log.info("Updating/creating assignment '%s': %s", assignment_id, assignment)
gb = Gradebook(self.db_url)
gb.update_or_create_assignment(assignment_id, **assignment)
gb.close()
# check if there are any extra notebooks in the db that are no longer
# part of the assignment, and if so, remove them
if self.notebook_id == "*":
self._clean_old_notebooks(assignment_id, student_id)
else:
self.fail("No assignment called '%s' exists in the config", assignment_id)