-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanarydata.py
49 lines (44 loc) · 1.25 KB
/
canarydata.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
'''
canarydata.py
# Created on Sun Aug 21 2011 11:24 GMT -0600 by Arctem
'''
import os.path
import pickle
from student import Student
import canary
phonebookdir = 'dat/phonebook.candat'
phonebook = []
def load_phonebook(debug):
global phonebook
ensure_dir('dat/')
if not os.path.exists(phonebookdir):
phonebook_file = open(phonebookdir, 'w')
try:
pickle.dump(phonebook, phonebook_file, 2)
phonebook_file.close()
except:
phonebook_file.close()
os.remove(phonebookdir)
print 'fatal: Phonebook file could not be found or created.'
exit()
phonebook_file = open(phonebookdir, 'r')
phonebook = pickle.load(phonebook_file)
phonebook_file.close()
if debug:
print phonebook
else:
print 'Loaded phonebook data for {} students.'.format(len(phonebook))
def save_phonebook():
phonebook_file = open(phonebookdir, 'w')
try:
pickle.dump(phonebook, phonebook_file, 2)
except:
print 'Error saving phonebook.'
phonebook_file.close()
def add_student(student):
phonebook.append(student)
save_phonebook()
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)