Add Course.robot attribute (for automatic email generation).
[pygrader.git] / pygrader / test / course.py
1 # Copright
2
3 import os as _os
4 import os.path as _os_path
5 import shutil as _shutil
6 import tempfile as _tempfile
7
8 from pygrader.storage import load_course as _load_course
9
10
11 COURSE_CONF = """
12 [course]
13 name: phys101
14 assignments: Attendance 1, Attendance 2, Attendance 3, Attendance 4,
15   Attendance 5, Attendance 6, Attendance 7, Attendance 8, Attendance 9,
16   Assignment 1, Assignment 2, Exam 1, Exam 2
17 robot: Robot101
18 professors: Gandalf
19 assistants: Sauron
20 students: Bilbo Baggins, Frodo Baggins, Aragorn
21
22 [Attendance 1]
23 points: 1
24 weight: 0.1/9
25 due: 2011-10-03
26
27 [Attendance 2]
28 points: 1
29 weight: 0.1/9
30 due: 2011-10-04
31
32 [Attendance 3]
33 points: 1
34 weight: 0.1/9
35 due: 2011-10-05
36
37 [Attendance 4]
38 points: 1
39 weight: 0.1/9
40 due: 2011-10-06
41
42 [Attendance 5]
43 points: 1
44 weight: 0.1/9
45 due: 2011-10-11
46
47 [Attendance 6]
48 points: 1
49 weight: 0.1/9
50 due: 2011-10-12
51
52 [Attendance 7]
53 points: 1
54 weight: 0.1/9
55 due: 2011-10-13
56
57 [Attendance 8]
58 points: 1
59 weight: 0.1/9
60 due: 2011-10-14
61
62 [Attendance 9]
63 points: 1
64 weight: 0.1/9
65 due: 2011-10-15
66
67 [Assignment 1]
68 points: 10
69 weight: 0.4/2
70 due: 2011-10-10
71
72 [Assignment 2]
73 points: 1
74 weight: 0.4/2
75 due: 2011-10-17
76
77 [Exam 1]
78 points: 10
79 weight: 0.4/2
80 due: 2011-10-10
81
82 [Exam 2]
83 points: 10
84 weight: 0.4/2
85 due: 2011-10-17
86
87 [Robot101]
88 nickname: phys-101 robot
89 emails: phys101@tower.edu
90 pgp-key: 4332B6E3
91
92 [Gandalf]
93 nickname: G-Man
94 emails: g@grey.edu
95 pgp-key: 0x0123456789ABCDEF
96
97 [Sauron]
98 nickname: Saury
99 emails: eye@tower.edu
100 pgp-key: 4332B6E3
101
102 [Bilbo Baggins]
103 nickname: Billy
104 emails: bb@shire.org, bb@greyhavens.net
105
106 [Frodo Baggins]
107 nickname: Frodo
108 emails: fb@shire.org
109
110 [Aragorn]
111 emails: a@awesome.gov
112 """
113
114
115 class StubCourse (object):
116     """Manage a course directory for testing.
117
118     >>> course = StubCourse()
119     >>> course.print_tree()
120     course.conf
121     >>> course.cleanup()
122     """
123     def __init__(self, load=True):
124         self.basedir = _tempfile.mkdtemp(prefix='pygrader-tmp-')
125         try:
126             self.mailbox = _os_path.join(self.basedir, 'mail')
127             course_conf = _os_path.join(self.basedir, 'course.conf')
128             with open(course_conf, 'w') as f:
129                 f.write(COURSE_CONF)
130             if load:
131                 self.course = _load_course(basedir=self.basedir)
132         except Exception:
133             self.cleanup()
134
135     def cleanup(self):
136         if self.basedir:
137             _shutil.rmtree(self.basedir)
138             self.basedir = None
139
140     def tree(self):
141         paths = []
142         for dirpath,dirnames,filenames in _os.walk(self.basedir):
143             for dirname in dirnames:
144                 paths.append(_os_path.join(dirpath, dirname))
145             for filename in filenames:
146                 paths.append(_os_path.join(dirpath, filename))
147         for i,path in enumerate(paths):
148             paths[i] = _os_path.relpath(path, self.basedir)
149         paths.sort()
150         return paths
151
152     def print_tree(self):
153         for path in self.tree():
154             print(path)