-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.py
130 lines (113 loc) · 5.1 KB
/
FileManager.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import sys
import cPickle as pickle
from datastructures import Course, Professor, Time, Semester, User
from datetime import datetime
import xlsxwriter
import re
class FileManager(object):
"""Crash course object to handle the loading and saving of course, professor, and user data"""
def __init__(self):
pass
def load_courses(self):
"""
load_courses opens the courses.csv files and loads each course in the
file one by one and initializes a new course object for each line of
data. The course objects are appended to the variable catalog which
is a list of all course objects. This catalog is returned.
"""
if not os.path.isfile('courses.csv'):
print("Housten we")
return None
catalog = []
with open('courses.csv', 'r') as courses:
lines = courses.readlines()
for line in lines:
c = line.split("/")
try:
code = c[0] #Course code as integer e.g. 1100
name = c[1] #Course name as string e.g. "Modeling and Simulation"
keyw = eval(c[2]) #Course keywords as list of strings e.g. ["modsim","modeling", ...]
prof = c[3] #Course professor as string e.g. "Jessica Townsend"
cred = eval(c[4]) #Course credits as dictionary e.g. {"ENGR":4, ... }
rcre = c[5] #Real credits as integer e.g. 4
seme = eval(c[6]) #semester held as list of lists i.e. [[year,semester], ...] e.g. [[2014,'f'], ... ]
time = eval(c[7]) #time of day held as list of lists i.e. [[day,hour,minutes,duration], ...] e.g. [['m',13,30,100],['r',13,30,100]]
desc = c[8] #description as string
prer = c[9] #Prerequisites as list of integer course codes e.g. [1100,1200, ...]
pnre = eval(c[10]) #Pass/norecordable as boolean
except:
print("Load error on the following line:")
print(line)
return False
else:
#generate time objects from time list
TIMES = []
for t in time:
TIMES.append(Time(t[0],t[1],t[2],t[3]))
#generate semester objects from seme list
SEMESTERS = []
for s in seme:
SEMESTERS.append(Semester(s[0],s[1]))
input_course = Course(code,name,keyw,prof,cred,rcre,SEMESTERS,TIMES,desc,prer,pnre)
catalog.append(input_course)
return catalog
def load_profs(self):
faculty = []
with open('profs.csv', 'r') as profs:
lines = profs.readlines()
for line in lines:
c = line.split("/")
try:
name = c[0] #Prof name as string e.g. "Jessica Townsend"
subj = c[1] #Prof subject as string e.g. "Thermodynamics"
cour = eval(c[2]) #courses taught as list of course codes e.g. [1100,1200, ...]
styl = c[3] #Prof style as string
except:
print("Load error on the following line:")
print(line)
return False
else:
input_prof = Professor(name,subj,cour,styl)
faculty.append(input_prof)
return faculty
def load_stats(self):
with open('stats.sts', 'rb') as statsfile:
last_updated = pickle.load(statsfile)
distribution = pickle.load(statsfile)
return last_updated,distribution
def load_user(self,username,password):
with open('user.usr', 'rb') as userfile:
try:
user = pickle.load(userfile)
except:
print("User file does not exist")
return False
else:
if username == user.username and password == user.password:
return user
else:
return None
def save_user(self,user):
user.last_updated = datetime.now()
with open('user.usr', 'wb') as userfile:
pickle.dump(user, userfile, -1)
def export_user(self,user):
filename = ""
for c in user.name:
if re.match("[a-zA-Z]", c):
filename += c
workbook = xlsxwriter.Workbook(filename + 'CoursePlan.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, "Course Plan for " + user.name)
worksheet.write(1, 0, "Class of " + str(user.grad_year))
worksheet.write(2, 0, "Majoring in " + user.major)
workbook.close()
filemanager = FileManager()
catalog = filemanager.load_courses()
#user = filemanager.load_user('ihill','crashcourse')
#filemanager.export_user(user)
# last_updated,distribution = filemanager.load_stats()
# print(last_updated)
# for k in distribution:
# print(str(k) + str(distribution[k]))