-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2l.py
executable file
·106 lines (86 loc) · 3.24 KB
/
d2l.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# d2l - A library for working with Desire2Learn
# Copyright (C) 2014 Stoney Jackson <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import shutil
import pathlib
import datetime
import unzipr
def prep(zipFile, toDir=None):
toDir = unzipr.unzipFile(zipFile, toDir)
toDir = Directory(toDir)
toDir.deleteAllButLastSubmissions()
toDir.renameSubmissions()
toDir = str(toDir)
unzipr.unzipFilesInDirectoryRecursively(toDir)
unzipr.deleteZipFilesFromDirectoryRecursively(toDir)
class Directory:
def __init__(self, directory):
self.directory = pathlib.Path(directory)
def renameSubmissions(self):
files = self._getFiles()
for f in files:
parts = f.studentName.split()
lastName = parts[-1]
allButLastName = '-'.join(parts[:-1])
name = lastName + '_' + allButLastName
name += ''.join(f.namePath.suffixes)
pathlib.Path(str(f)).rename(self.directory/name)
def deleteAllButLastSubmissions(self):
files = self._getFiles()
self._sortFilesByDateTime(files)
files.reverse()
seen = set()
for f in files:
if f.studentId not in seen:
seen.add(f.studentId)
else:
(self.directory / f.namePath).unlink()
def _getFiles(self):
files = [File(f) for f in self.directory.iterdir() if File.isFile(f)]
return files
def _sortFilesByDateTime(self, fileList):
fileList.sort(key=File.getDateTime)
def __repr__(self):
return str(self.directory)
class File:
'''
A Desire2Learn file's name has the following structure:
studentId-courseId - studentFullName - dateTimeSubmitted - fileName
'''
@staticmethod
def isFile(path):
return str(path).count(' - ') == 3
def __init__(self, file):
self.fullPath = pathlib.Path(file)
self.namePath = pathlib.Path(self.fullPath.name)
parts = str(self.namePath).split(' - ')
self.studentId, self.postid = parts[0].split('-')
self.studentName = parts[1]
(mon, day, year, hm, ampm) = [p.strip(',') for p in parts[2].split()]
minutes = hm[-2:]
hours = hm[:-2]
hours = hours.zfill(2)
self.datetime = ' '.join([mon, day, year, hours, minutes, ampm])
self.studentFilename = parts[3]
def getDateTime(self):
return datetime.datetime.strptime(self.datetime, '%b %d %Y %I %M %p')
def __repr__(self):
return str(self.fullPath)
import sys
zipFile = sys.argv[1]
toDir = None
if len(sys.argv) > 1:
toDir = sys.argv[2]
prep(zipFile, toDir)