This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.py
260 lines (231 loc) · 12.9 KB
/
Configuration.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
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
import configparser, os
from Constants import DECODE_HTML_FILE
from NetworkRequests import HTML_LocalFile
# This file contains the functions to read and write the configuration file.
def getUserHeaders(FILEPATH):
"""
Returns the headers used to generate the POST/GET requests.
:param FILEPATH: UserPreferences file path where the headers are stored.
:return: The dictionary with the headers.
"""
UserPreferences = configparser.ConfigParser()
UserPreferences.read(FILEPATH, encoding=DECODE_HTML_FILE)
return {"User-Agent": UserPreferences[UserPreferences.sections()[0]]['Headers']}
def getUserPreferences(FILEPATH):
"""
Returns a ConfigParser object with the user preferences.
:param FILEPATH: UserPreferences file path.
:return: ConfigParser object with the user preferences.
"""
UserPreferences = configparser.ConfigParser()
UserPreferences.read(FILEPATH, encoding=DECODE_HTML_FILE)
return UserPreferences
def insertUserPreferences(CONFIG_FILEPATH, planEstudio, idiomaPais, trimestre, planDocente, codigoCentro, codigoEstudio, curso):
"""
Inserts the user preferences into the UserPreferences.ini file.
:param CONFIG_FILEPATH: File path of the UserPreferences.ini file.
:param planEstudio: Identifier of the plan of study. (Integer)
:param idiomaPais: The subjects are going to be added in the selected language. (en.GB, es.ES, ca.ES)
:param trimestre: Number of the trimestre. (1, 2, 3)
:param planDocente: Year of the plan of study. (Integer)
:param codigoCentro: Identifier of the center. (Integer)
:param codigoEstudio: Identifier of the study. (Integer)
:param curso: Main subjects course number. (1, 2, 3, 4)
:return: True if the preferences were inserted, False otherwise.
"""
UserPreferences = getUserPreferences(CONFIG_FILEPATH)
BasicInformation = UserPreferences[UserPreferences.sections()[1]]
BasicInformation['PlanEstudio'] = planEstudio
BasicInformation['IdiomaPais'] = idiomaPais
BasicInformation['Trimestre'] = trimestre
BasicInformation['PlanDocente'] = planDocente
BasicInformation['CodigoCentro'] = codigoCentro
BasicInformation['CodigoEstudio'] = codigoEstudio
BasicInformation['Curso'] = curso
try:
with open(CONFIG_FILEPATH, 'w') as configurationFile: UserPreferences.write(configurationFile)
except Exception as ErrorCode:
print(f"Something went wrong while inserting user preferences into the file. {ErrorCode}")
return False
else:
return True
def insertFilePath(CONFIG_FILEPATH, filePath):
"""
Inserts the file path of the Groups.html file into the UserPreferences.ini file.
:param CONFIG_FILEPATH: Path of the UserPreferences.ini file.
:param filePath: HTML file path.
:return: True if the file path was inserted, False otherwise.
"""
UserPreferences = getUserPreferences(CONFIG_FILEPATH)
Subjects = UserPreferences[UserPreferences.sections()[2]]
Subjects['EspaiAulaFilePath'] = filePath
try:
with open(CONFIG_FILEPATH, 'w') as configurationFile: UserPreferences.write(configurationFile)
except Exception as ErrorCode:
print(f"Someting went wrong while inserting the HTML file path intro the UserPreferences.ini file. {ErrorCode}")
return False
else:
return True
def insertSubjectPreferences(CONFIG_FILEPATH, toInsert):
"""
Adds a subject to the .init file, it checks if is the first subject.
:param CONFIG_FILEPATH: .init file path.
:param toInsert: Dictionary that contains subject['Code'] as the subject ID, subject['T']
for the theory group, subject['S'] and subject['P'] for the seminars and practice groups.
:return: True if the subject information was inserted, False otherwise.
"""
UserPreferences = getUserPreferences(CONFIG_FILEPATH)
SubjectsInformation = UserPreferences[UserPreferences.sections()[2]]
if SubjectsInformation['Asignaturas'].lower() != 'false':
SubjectsInformation['Asignaturas'] = SubjectsInformation['Asignaturas'] + ',' + toInsert['Code']
SubjectsInformation['GruposAsignaturas'] = SubjectsInformation['GruposAsignaturas'] + ',' + toInsert['T']
SubjectsInformation['GruposPracticas'] = SubjectsInformation['GruposPracticas'] + ',' + toInsert['P']
SubjectsInformation['GruposSeminarios'] = SubjectsInformation['GruposSeminarios'] + ',' + toInsert['S']
else:
SubjectsInformation['Asignaturas'] = toInsert['Code']
SubjectsInformation['GruposAsignaturas'] = toInsert['T']
SubjectsInformation['GruposPracticas'] = toInsert['P']
SubjectsInformation['GruposSeminarios'] = toInsert['S']
try:
with open(CONFIG_FILEPATH, 'w') as configurationFile: UserPreferences.write(configurationFile)
except Exception as ErrorCode:
print(f"Something went wrong while inserting the subject information into the file. {ErrorCode}")
return False
else:
return True
def clearSubjectsPreferences(CONFIG_FILEPATH):
"""
Removes all the subjects information from the .ini file.
:param CONFIG_FILEPATH: File path of the UserPreferences.ini file.
:return: True if the subjects information was removed, False otherwise.
"""
UserPreferences = getUserPreferences(CONFIG_FILEPATH)
SubjectsInformation = UserPreferences[UserPreferences.sections()[2]]
SubjectsInformation['Asignaturas'] = 'False'
SubjectsInformation['GruposAsignaturas'] = 'False'
SubjectsInformation['GruposPracticas'] = 'False'
SubjectsInformation['GruposSeminarios'] = 'False'
try:
with open(CONFIG_FILEPATH, 'w') as configurationFile: UserPreferences.write(configurationFile)
except Exception as ErrorCode:
print(f"Something went wrong while removing the subjects information from the file. {ErrorCode}")
return False
else:
return True
def insertTimeRange(CONFIG_FILEPATH, fromDate, toDate):
"""
Inserting the initial and final date of the subjects.
:param CONFIG_FILEPATH: File path of the UserPreferences.ini file.
:param fromDate: Initial date of the subjects.
:param toDate: Final date of the subjects.
:return: True if the date were inserted, False otherwise.
"""
UserPreferences = getUserPreferences(CONFIG_FILEPATH)
Dates = UserPreferences[UserPreferences.sections()[3]]
Dates['Inicio'], Dates['Final'] = fromDate, toDate
try:
with open(CONFIG_FILEPATH, 'w') as configurationFile: UserPreferences.write(configurationFile)
except Exception as ErrorCode:
print(f"Something went wrong while inserting the date information into the file. {ErrorCode}")
return False
else:
return True
def isUsingEspaiAulaFilePath(UserPreferences):
# Function to check if the user is using the automatic mode.
return UserPreferences[UserPreferences.sections()[2]]['EspaiAulaFilePath'] != 'False'
def extractSubjectsPreferences(UserPreferences): # Reads the subjects information from the .ini file.
SubjectsGroups = UserPreferences[UserPreferences.sections()[2]]['GruposAsignaturas'].split(',')
PGroups, SGroups = UserPreferences[UserPreferences.sections()[2]]['GruposPracticas'], UserPreferences[UserPreferences.sections()[2]]['GruposSeminarios']
subjectsList = UserPreferences[UserPreferences.sections()[2]]['Asignaturas'].split(',')
PGroups, SGroups = PGroups.split(','), SGroups.split(',')
Groups = list(set(SubjectsGroups))
return Groups, subjectsList, SubjectsGroups, PGroups, SGroups
def extractRequestInformation(UserPreferences):
basicInformation = UserPreferences[UserPreferences.sections()[1]] # Information to generate the DATA request.
timeRange = tuple(UserPreferences[UserPreferences.sections()[3]].values()) # Tuple which contains fromDate and toDate.
return basicInformation, timeRange
def getEspaiAulaFilePath(UserPreferences):
espaiAulaFilePath = UserPreferences[UserPreferences.sections()[2]]['EspaiAulaFilePath']
if not os.path.isfile(espaiAulaFilePath): print(f"WARNING! It looks like the HTML file with subjects information does not exist at {espaiAulaFilePath}")
return espaiAulaFilePath
def getCalendarID(UserPreferences): return UserPreferences[UserPreferences.sections()[4]]['CalendarID']
def getColorList(UserPreferences): return UserPreferences[UserPreferences.sections()[4]]['SubjectsColors'].split(',')
def extractSubjectsPreferencesFromFile(searchOn):
"""
Extracts the subjects information from the .ini file.
:param searchOn: BeautifulSoup object with the server answer.
:return: Tuples with the subjects information.
"""
contentList = searchOn.findAllContent('td', 'lletrab')
contentList = [x for x in contentList if x != '..'] # Removing some trash data.
Groups, SubjectsGroups, PGroups, SGroups = set(), [], [], []
subjectsList = []
i = 0
while i < (len(contentList)-1):
if contentList[i+1][0] == 'P' and contentList[i+2][0] == 'S':
subject, practice, seminar = contentList[i], contentList[i+1], contentList[i+2]
separatorPosition = subject.find('-')
subjectCode, subjectGroup = subject[0:separatorPosition], subject[separatorPosition+1:separatorPosition+2]
practiceGroup, seminarGroup = practice[practice.find('-')+1:], seminar[seminar.find('-')+1:]
Groups.add(subjectGroup)
SubjectsGroups.append(subjectGroup)
PGroups.append(practiceGroup)
SGroups.append(seminarGroup)
subjectsList.append(subjectCode)
i += 2
elif contentList[i+1][0] == 'S':
subject, seminar = contentList[i], contentList[i + 1]
separatorPosition = subject.find('-')
subjectCode, subjectGroup = subject[0:separatorPosition], subject[separatorPosition + 1:separatorPosition + 2]
seminarGroup = seminar[seminar.find('-') + 1:]
Groups.add(subjectGroup)
SubjectsGroups.append(subjectGroup)
PGroups.append('-1')
SGroups.append(seminarGroup)
subjectsList.append(subjectCode)
i += 1
elif contentList[i+1][0] == 'P':
subject, practice = contentList[i], contentList[i + 1]
separatorPosition = subject.find('-')
subjectCode, subjectGroup = subject[0:separatorPosition], subject[separatorPosition + 1:separatorPosition + 2]
practiceGroup = practice[practice.find('-') + 1:]
Groups.add(subjectGroup)
SubjectsGroups.append(subjectGroup)
PGroups.append(practiceGroup)
SGroups.append('-1')
subjectsList.append(subjectCode)
i += 1
i += 1
return list(Groups), subjectsList, SubjectsGroups, PGroups, SGroups
def generateData(fromSubjects, fromGroups, BasicInformation):
"""
Generating the request data in POST/GET structure.
:param fromSubjects: List of subjects codes.
:param fromGroups: List of theory groups for every subject.
:param BasicInformation: Main information to generate the request.
:return: POST/GET request data.
"""
DATA = f"planEstudio={BasicInformation['PlanEstudio']}" \
f"&idiomaPais={BasicInformation['IdiomaPais']}" \
f"&trimestre=T/{BasicInformation['Trimestre']}" \
f"&planDocente={BasicInformation['PlanDocente']}" \
f"¢ro={BasicInformation['CodigoCentro']}" \
f"&estudio={BasicInformation['CodigoEstudio']}" \
f"&curso={BasicInformation['Curso']}" \
for group in fromGroups: DATA += f"&grupo{group}={group}"
for subject in fromSubjects: DATA += f"&asignatura{subject}={subject}"
return DATA
def checkEspaiAulaFileIntegrity(filePath):
"""
This function recieves the HTML file with the subject information
from the AulaGlobal website and checks if the file is valid.
:param filePath: HTML file path.
:return: The score of the file.
"""
if os.path.exists(filePath):
try: espaiAulaFile = HTML_LocalFile(filePath, DECODE_HTML_FILE)
except: return 50
try: extractSubjectsPreferencesFromFile(espaiAulaFile)
except: return 75
return 100
return 25