4
4
import os
5
5
import logging
6
6
import warnings
7
+ from pathlib import Path
7
8
8
9
from traitlets .config import LoggingConfigurable , Config , get_config
9
10
from traitlets import Instance , Enum , Unicode , observe
@@ -123,7 +124,7 @@ def gradebook(self):
123
124
"""
124
125
return Gradebook (self .coursedir .db_url , self .course_id )
125
126
126
- def get_source_assignments (self ):
127
+ def get_source_assignments (self ) -> set [ str ] :
127
128
"""Get the names of all assignments in the `source` directory.
128
129
129
130
Returns
@@ -132,29 +133,14 @@ def get_source_assignments(self):
132
133
A set of assignment names
133
134
134
135
"""
135
- filenames = glob .glob (self .coursedir .format_path (
136
- self .coursedir .source_directory ,
137
- student_id = '.' ,
138
- assignment_id = '*' ))
139
-
140
- assignments = set ([])
141
- for filename in filenames :
142
- # skip files that aren't directories
143
- if not os .path .isdir (filename ):
144
- continue
145
136
146
- # parse out the assignment name
147
- regex = self .coursedir .format_path (
148
- self .coursedir .source_directory ,
149
- student_id = '.' ,
150
- assignment_id = '(?P<assignment_id>.*)' ,
151
- escape = True )
152
-
153
- matches = re .match (regex , filename )
154
- if matches :
155
- assignments .add (matches .groupdict ()['assignment_id' ])
156
-
157
- return assignments
137
+ return {
138
+ entry ['assignment_id' ] for entry in
139
+ self .coursedir .find_assignments (
140
+ nbgrader_step = self .coursedir .source_directory ,
141
+ student_id = "." ,
142
+ )
143
+ }
158
144
159
145
def get_released_assignments (self ):
160
146
"""Get the names of all assignments that have been released to the
@@ -194,32 +180,14 @@ def get_submitted_students(self, assignment_id):
194
180
A set of student ids
195
181
196
182
"""
197
- # get the names of all student submissions in the `submitted` directory
198
- filenames = glob .glob (self .coursedir .format_path (
199
- self .coursedir .submitted_directory ,
200
- student_id = '*' ,
201
- assignment_id = assignment_id ))
202
183
203
- students = set ([])
204
- for filename in filenames :
205
- # skip files that aren't directories
206
- if not os .path .isdir (filename ):
207
- continue
208
-
209
- # parse out the student id
210
- if assignment_id == "*" :
211
- assignment_id = ".*"
212
- regex = self .coursedir .format_path (
213
- self .coursedir .submitted_directory ,
214
- student_id = '(?P<student_id>.*)' ,
215
- assignment_id = assignment_id ,
216
- escape = True )
217
-
218
- matches = re .match (regex , filename )
219
- if matches :
220
- students .add (matches .groupdict ()['student_id' ])
221
-
222
- return students
184
+ return {
185
+ entry ['student_id' ] for entry in
186
+ self .coursedir .find_assignments (
187
+ nbgrader_step = self .coursedir .submitted_directory ,
188
+ assignment_id = assignment_id
189
+ )
190
+ }
223
191
224
192
def get_submitted_timestamp (self , assignment_id , student_id ):
225
193
"""Gets the timestamp of a submitted assignment.
@@ -243,10 +211,7 @@ def get_submitted_timestamp(self, assignment_id, student_id):
243
211
student_id ,
244
212
assignment_id ))
245
213
246
- timestamp_pth = os .path .join (assignment_dir , 'timestamp.txt' )
247
- if os .path .exists (timestamp_pth ):
248
- with open (timestamp_pth , 'r' ) as fh :
249
- return parse_utc (fh .read ().strip ())
214
+ return self .coursedir .get_existing_timestamp (assignment_dir )
250
215
251
216
def get_autograded_students (self , assignment_id ):
252
217
"""Get the ids of students whose submission for a given assignment
@@ -439,21 +404,14 @@ def get_notebooks(self, assignment_id):
439
404
440
405
# if it doesn't exist in the database
441
406
else :
442
- sourcedir = self .coursedir .format_path (
443
- self .coursedir .source_directory ,
444
- student_id = '.' ,
445
- assignment_id = assignment_id )
446
- escaped_sourcedir = self .coursedir .format_path (
407
+ sourcedir = Path (self .coursedir .format_path (
447
408
self .coursedir .source_directory ,
448
409
student_id = '.' ,
449
- assignment_id = assignment_id ,
450
- escape = True )
410
+ assignment_id = assignment_id ))
451
411
452
412
notebooks = []
453
- for filename in glob .glob (os .path .join (sourcedir , "*.ipynb" )):
454
- regex = re .escape (os .path .sep ).join ([escaped_sourcedir , "(?P<notebook_id>.*).ipynb" ])
455
- matches = re .match (regex , filename )
456
- notebook_id = matches .groupdict ()['notebook_id' ]
413
+ for filename in sourcedir .glob ("*.ipynb" ):
414
+ notebook_id = filename .stem
457
415
notebooks .append ({
458
416
"name" : notebook_id ,
459
417
"id" : None ,
0 commit comments