Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
Added the option to customize the subjects colors in the INI file
Browse files Browse the repository at this point in the history
Took 44 minutes
  • Loading branch information
Sklyvan committed Jan 7, 2023
1 parent 7f051e2 commit 86d6ad9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
34 changes: 17 additions & 17 deletions UserPreferences.ini
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
[CONNECTION]
headers = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
Headers = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36

[BASIC_INFORMATION]
planestudio = 634
idiomapais = en.GB
trimestre = 1
plandocente = 2021
codigocentro = 337
codigoestudio = 3377
curso = 3
PlanEstudio = 634
Idiomapais = en.GB
Trimestre = 2
PlanDocente = 2022
CodigoCentro = 337
CodigoEstudio = 3377
Curso = 4

[SUBJECTS_INFORMATION]
asignaturas = False
gruposasignaturas = False
grupospracticas = False
gruposseminarios = False
espaiaulafilepath = ../res/EspaiAulaFiles/Grups.html
Asignaturas = False
GruposAsignaturas = False
GruposPracticas = False
GruposSeminarios = False
EspaiAulaFilepath = ../res/EspaiAulaFiles/Grups.html

[DATES]
inicio = 01/12/2021
final = 30/12/2021
Inicio = 01/01/2023
Ginal = 31/01/2023

[CALENDAR_CUSTOMIZATION]
calendarid = primary

CalendarID = primary
SubjectsColors = 5,3
2 changes: 2 additions & 0 deletions src/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def getEspaiAulaFilePath(UserPreferences):

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.
Expand Down
34 changes: 24 additions & 10 deletions src/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ def deleteGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, logMessages=No

return True

def addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, subjectsColors, logMessages=None, initialLoadingStatus=0, loadingWork=100):
def addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, logMessages=None, initialLoadingStatus=0, loadingWork=100):
"""
With this function we're going to add the generated events to the Google Calendar.
:param MyCalendar: That's the Google Calendar object were we're adding the events.
:param subjectsBlocks: List of subjects that we are going to add to the Google Calendar.
:param calendarID: The ID of the Google Calendar where we're going to add the events.
:param subjectsColors: Dictionary with the subjects and their colors.
:param logMessages: File where we're going to save the log messages, if it's None, the output goes to stdout.
:return: True in case of success, False in case of error.
"""
Expand All @@ -129,7 +128,7 @@ def addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, subjectsColors, l
addLogInformation(f"Adding {subject.name} to the calendar.")
try:
MyCalendar.addEvent(subject.subjectID, f"{subject.name} ({subject.type[0]})", subject.classroom, subject.getDescription(),
subject.start, subject.end, TIMEZONE, colorID=subjectsColors[str(subject.code)],
subject.start, subject.end, TIMEZONE, colorID=subject.colorID,
calendarID=calendarID)
except Exception as ErrorCode:
addLogInformation(f"Something went wrong while adding subject {subject.getDescription()} to calendar, "
Expand Down Expand Up @@ -186,10 +185,6 @@ def RunApplication(deleteMode=False, replaceMode=True):
loadingStatus += 5
yield loadingStatus # 14%

subjectsColors = dict(zip(fromSubjects, [str(x%GOOGLE_CALENDAR_API_MAX_COLORS+1) for x in range(len(fromSubjects))])) # Generating a dictionary[subjectCode] = assignedColor
loadingStatus += 2
yield loadingStatus # 16%

# Extracting the time and main information.
basicInformation, timeRange = extractRequestInformation(userPreferences)
DATA = generateData(fromSubjects, fromGroups, basicInformation)
Expand All @@ -203,9 +198,28 @@ def RunApplication(deleteMode=False, replaceMode=True):
Now, we're going to access the UPF website to extract the schedule information.
"""
if (jsonFile := downloadContent(DATA, fromDate, toDate, fromHeaders=getUserHeaders(CONFIG_FILE))):

filteredJSON = filter(lambda x: 'codAsignatura' in x, jsonFile)
diferentSubjects = set(map(lambda x: x['codAsignatura'], filteredJSON))

userDefinedColors = getColorList(userPreferences)
if userDefinedColors[0] == 'False':
# Generating a dictionary[subjectCode] = assignedColor
subjectsColors = dict(zip(diferentSubjects,
[str(x%GOOGLE_CALENDAR_API_MAX_COLORS+1) for x in range(len(diferentSubjects))]))
else:
# Generating a dictionary[subjectCode] = assignedColor
subjectsColors = dict(zip(diferentSubjects, userDefinedColors))
loadingStatus += 2
yield loadingStatus # 16%

if (n_downloadedSubjects := len(jsonFile)-1) > 0: # If we have downloaded at least one subject, we're going to process them.
addLogInformation(f"Downloaded {n_downloadedSubjects} subjects blocks.")
subjectsBlocks = generateBlocks(jsonFile, dict(zip(fromSubjects, zip(userSubjectsGroups, pGroups, sGroups))), n_downloadedSubjects)
subjectsBlocks = generateBlocks(jsonFile,
dict(zip(fromSubjects,
zip(userSubjectsGroups, pGroups, sGroups))),
subjectsColors,
n_downloadedSubjects)
addLogInformation(f"Using {len(subjectsBlocks)} subjects blocks.")
else:
addLogInformation("No subjects blocks have been downloaded, closing program.")
Expand All @@ -231,7 +245,7 @@ def RunApplication(deleteMode=False, replaceMode=True):
yield loadingStatus

mainLoop = True
functionIterator = addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, subjectsColors, initialLoadingStatus=66, loadingWork=33)
functionIterator = addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, initialLoadingStatus=66, loadingWork=33)
while mainLoop:
try: loadingStatus = next(functionIterator)
except StopIteration: mainLoop = False
Expand All @@ -247,7 +261,7 @@ def RunApplication(deleteMode=False, replaceMode=True):
yield loadingStatus
else: # Just adding events.
mainLoop = True
functionIterator = addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, subjectsColors, initialLoadingStatus=33, loadingWork=66)
functionIterator = addGeneratedEvents(MyCalendar, subjectsBlocks, calendarID, initialLoadingStatus=33, loadingWork=66)
while mainLoop:
try: loadingStatus = next(functionIterator)
except StopIteration: mainLoop = False
Expand Down
7 changes: 5 additions & 2 deletions src/ScheduleBlocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def __str__(self): # Just for debugging purposes.
else:
return f"{self.type} {self.name} at {self.classroom} | Group: {self.group} | Code: {self.code} | {self.start} - {self.end}"

def generateBlocks(jsonFile, subjectsGroups, toRead=False):
def generateBlocks(jsonFile, subjectsGroups, subjectsColors, toRead=False):
"""
Given a json file and a list of groups, this function
will generate the blocks of the schedule.
:param jsonFile: This file contains the subjects information.
:param subjectsGroups: This list contains the groups of the subjects.
:param subjectsColors: Dictionary with subjectsColors[subjectCode] = colorID
:param toRead: I don't understand this parameter and I don't remember why I'm using it :)
:return: List of SubjectBlocks.
"""
Expand Down Expand Up @@ -88,6 +89,8 @@ def generateBlocks(jsonFile, subjectsGroups, toRead=False):
extraInfo=observation)
addBlock = True

if addBlock: blocks.append(newBlock)
if addBlock:
newBlock.colorID = subjectsColors[newBlock.code]
blocks.append(newBlock)

return blocks

0 comments on commit 86d6ad9

Please sign in to comment.