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