storage: add save_grade (useful for upcoming `grade` handler).
authorW. Trevor King <wking@tremily.us>
Sun, 2 Sep 2012 18:12:15 +0000 (14:12 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 2 Sep 2012 18:27:48 +0000 (14:27 -0400)
pygrader/storage.py

index fb618c78930bae7bc457dbf92ab1120c4a1b3e82..34af7dcf08e7d43c35a4797ec08bccc84e3931ab 100644 (file)
@@ -341,6 +341,21 @@ def set_late(basedir, assignment, person):
     Lpath = _os_path.join(path, 'late')
     _touch(Lpath)
 
+def save_grade(basedir, grade):
+    "Save a grade into a course directory"
+    path = assignment_path(
+        basedir=basedir, assignment=grade.assignment, person=grade.student)
+    if not _os_path.isdir(path):
+        _os.makedirs(path)
+    gpath = _os_path.join(path, 'grade')
+    with _io.open(gpath, 'w', encoding=_ENCODING) as f:
+        f.write('{}\n'.format(grade.points))
+        if grade.comment:
+            f.write('\n{}\n'.format(grade.comment.strip()))
+    set_notified(basedir=basedir, grade=grade)
+    set_late(
+        basedir=basedir, assignment=grade.assignment, person=grade.student)
+
 def _touch(path):
     """Touch a file (`path` is created if it doesn't already exist)