-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbcleanup.py
executable file
·97 lines (78 loc) · 3.51 KB
/
bbcleanup.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
#!/usr/bin/env python3
# Copyright (C) 2013-2014 Karl R. Wurst
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#######################################################################
# Cleans up assignment files downloaded from Blackboard's gradebook by:
# - deleting all .txt files with no student text data or comments
# - renaming remaining .txt files to username.txt
# - renaming all other files to username-userfilename.ext
# Call as:
# python bbcleanup.py working-directory
import os
import sys
import re
def bbcleanup():
changeToWorkingDirectory()
deleteContentFreeBlackboardGeneratedFiles(getAttemptFiles())
renameBlackboardFiles(getAttemptFiles())
def deleteContentFreeBlackboardGeneratedFiles(filenameList):
deleteList = filterForContentFreeBlackboardGeneratedFiles(filenameList)
for filename in deleteList:
os.remove(filename)
def filterForContentFreeBlackboardGeneratedFiles(filenameList):
return [f for f in filenameList if isContentFreeBlackboardGeneratedFile(f)]
def isContentFreeBlackboardGeneratedFile(filename):
if isBlackboardGeneratedFile(filename):
contents = getFileContents(filename)
if 'There are no student comments for this assignment' in contents and \
'There is no student submission text data for this assignment.' in contents:
return filename
def getFileContents(filename):
with open(filename) as file:
return file.read()
def renameBlackboardFiles(filenameList):
for filename in filenameList:
os.rename(filename, fixBlackboardFilename(filename))
def fixBlackboardFilename(filename):
filename = removeSpacesAndParentheses(filename)
username = getUsername(filename)
submittedFilename = getSubmittedFilename(filename)
return username + submittedFilename
def removeSpacesAndParentheses(string):
return string.replace(' ', '').replace('(', '').replace(')', '')
def getUsername(filename):
firstUnderscore = filename.find('_')
secondUnderscore = filename.find('_', firstUnderscore + 1)
return filename[firstUnderscore + 1:secondUnderscore]
def getSubmittedFilename(filename):
if isBlackboardGeneratedFile(filename):
return '.txt'
else:
lastUnderscore = filename.rfind('_')
return '-' + filename[lastUnderscore + 1:]
def isBlackboardGeneratedFile(filename):
dateTimePattern = re.compile('[^_]+_[^_]+_attempt_\d{4}\-\d{2}\-\d{2}\-\d{2}\-\d{2}\-\d{2}\.txt')
return dateTimePattern.match(filename)
def getAttemptFiles():
return filterForAttemptFiles(os.listdir('.'))
def filterForAttemptFiles(directoryContentsList):
return [ f for f in directoryContentsList if os.path.isfile(f) and isAttemptFile(f) ]
def isAttemptFile(filename):
return '_attempt_' in filename
def changeToWorkingDirectory():
os.chdir(sys.argv[1])
if __name__ == '__main__':
bbcleanup()