-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConfiguration.py
More file actions
311 lines (271 loc) · 9.61 KB
/
Copy pathConfiguration.py
File metadata and controls
311 lines (271 loc) · 9.61 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import Professor
import StudentsGroup
import Course
import Room
import CourseClass
# Reads configration file and stores parsed objects
class Configuration:
global instance
# Initialize data
def __init__(self):
self.isEmpty = True
self.professors = {}
self.studentGroups = {}
self.courses = {}
self.rooms = {}
self.courseClasses = []
Room.Room.RestartIDs()
# Returns reference to global instance of configuration
def GetInstance():
instance = Configuration()
return instance
# Parse file and store parsed object
def Parsefile(self, fileName):
# clear previously parsed objects
self.professors = {}
self.studentGroups = {}
self.courses = {}
self.rooms = {}
self.courseClasses = []
Room.Room.RestartIDs()
# open file
counter = 0
with open(fileName, "r") as input:
for line in input:
counter = counter + 1
# get type of object, parse obect and store it
strippedLine = line.strip()
if strippedLine == '#prof':
p = self.__ParseProfessor(input)
if p:
self.professors[p.GetId()] = p
elif strippedLine == '#group':
g = self.__ParseStudentsGroup(input)
if g:
self.studentGroups[g.GetId()] = g
elif strippedLine == '#course':
c = self.__ParseCourse(input)
if c:
self.courses[c.GetId()] = c
elif strippedLine == '#room':
r = self.__ParseRoom(input)
if r:
self.rooms[r.GetId()] = r
elif strippedLine == '#class':
c = self.__ParseCourseClass(input)
if c:
self.courseClasses.append(c)
input.close()
self.isEmpty = False
# Returns pointer to professor with specified ID
# If there is no professor with such ID method returns NULL
def GetProfessorById(self, id):
if id in self.professors.keys():
return self.professors[id]
return None
# Returns number of parsed professors
def GetNumberOfProfessors(self):
return len(self.professors.keys())
# Returns pointer to student group with specified ID
# If there is no student group with such ID method returns NULL
def GetStudentsGroupById(self, id):
if id in self.studentGroups.keys():
return self.studentGroups[id]
return None
# Returns number of parsed student groups
def GetNumberOfStudentGroups(self):
return len(self.studentGroups)
# Returns pointer to course with specified ID
# If there is no course with such ID method returns NULL
def GetCourseById(self, id):
if id in self.courses.keys():
return self.courses[id]
return None
# Returns number of parsed courses
def GetNumberOfCourses(self):
return len(self.courses)
# Returns pointer to room with specified ID
# If there is no room with such ID method returns NULL
def GetRoomById(self, id):
if id in self.rooms.keys():
return self.rooms[id]
return None
# Returns number of parsed rooms
def GetNumberOfRooms(self):
return len(self.rooms)
# Returns reference to list of parsed classes
def GetCourseClasses(self):
return self.courseClasses
# Returns number of parsed classes
def GetNumberOfCourseClasses(self):
return len(self.courseClasses)
# Returns TRUE if configuration is not parsed yet
def isEmpty(self):
return self.isEmpty
# Reads professor's data from config file, makes object and returns pointer to it
# Returns NULL if method cannot parse configuration data
def __ParseProfessor(self, file):
newFile = file
id = 0
name = ''
dictConfig = {}
while True:
line = newFile.readline()
line = line.strip()
if line == "" or line == '#end':
break
key = ''
value = ''
p = line.find('=')
if p != -1:
# key
key = line[:p].strip()
# value
dictConfig[key] = line[p + 1:].strip()
for key in dictConfig.keys():
if key == 'id':
id = dictConfig[key]
elif key == 'name':
name = dictConfig[key]
# make object and return pointer to it
if id == 0:
return None
return Professor.Professor(id, name)
# Reads professor's data from config file, makes object and returns pointer to it
# Returns NULL if method cannot parse configuration data
def __ParseStudentsGroup(self, file):
newFile = file
id = 0
number = 0
name = ''
dictConfig = {}
while True:
line = newFile.readline()
line = line.strip()
if line == "" or line == '#end':
break
key = ''
value = ''
p = line.find('=')
if p != -1:
# key
key = line[:p].strip()
# value
dictConfig[key] = line[p + 1:].strip()
for key in dictConfig.keys():
if key == 'id':
id = dictConfig[key]
elif key == 'name':
name = dictConfig[key]
elif key == 'size':
number = dictConfig[key]
# make object and return pointer to it
if id == 0:
return None
return StudentsGroup.StudentsGroup( id, name, number )
# Reads course's data from config file, makes object and returns pointer to it
# Returns NULL if method cannot parse configuration data
def __ParseCourse(self, file):
print("parse course")
newFile = file
id = 0
name = ''
dictConfig = {}
while True:
line = newFile.readline()
line = line.strip()
if line == "" or line == '#end':
break
key = ''
value = ''
p = line.find('=')
if p != -1:
# key
key = line[:p].strip()
# value
dictConfig[key] = line[p + 1:].strip()
for key in dictConfig.keys():
if key == 'id':
id = dictConfig[key]
elif key == 'name':
name = dictConfig[key]
# make object and return pointer to it
if id == 0:
return None
return Course.Course(id, name)
# Reads rooms's data from config file, makes object and returns pointer to it
# Returns NULL if method cannot parse configuration data
def __ParseRoom(self, file):
newFile = file
number = 0
lab = False
name = ''
dictConfig = {}
while True:
line = newFile.readline()
line = line.strip()
if line == "" or line == '#end':
break
key = ''
value = ''
p = line.find('=')
if p != -1:
# key
key = line[:p].strip()
# value
dictConfig[key] = line[p + 1:].strip()
for key in dictConfig.keys():
if key == 'name':
name = dictConfig[key]
elif key == 'lab':
lab = dictConfig[key]
elif key == 'size':
number = dictConfig[key]
# make object and return pointer to it
if number == 0:
return None
return Room.Room( name, lab, number )
# Reads class' data from config file, makes object and returns pointer to it
# Returns NULL if method cannot parse configuration data
def __ParseCourseClass(self, file):
newFile = file
pid = 0
cid = 0
dur = 1
lab = False
groups = []
dictConfig = {}
while True:
line = newFile.readline()
line = line.strip()
if line == "" or line == '#end':
break
key = ''
value = ''
p = line.find('=')
if p != -1:
# key
key = line[:p].strip()
# value
dictConfig[key] = line[p + 1:].strip()
for key in dictConfig.keys():
if key == 'professor':
pid = dictConfig[key]
elif key == 'course':
cid = dictConfig[key]
elif key == 'lab':
lab = dictConfig[key]
elif key == 'duration':
dur = dictConfig[key]
elif key == 'group':
g = self.GetStudentsGroupById(dictConfig[key])
if g:
groups.append(g)
# get professor who teaches class and course to which this class belongs
p = self.GetProfessorById(pid)
c = self.GetCourseById(cid)
# does professor and class exists
if not c or not p:
return None
# make object and return pointer to it
return CourseClass.CourseClass( p, c, groups, lab, dur )