From 86d6ad92221b89c29f10efe1b5f4e64ed6ca5335 Mon Sep 17 00:00:00 2001 From: Sklyvan Date: Sat, 7 Jan 2023 13:15:53 +0100 Subject: [PATCH] Added the option to customize the subjects colors in the INI file Took 44 minutes --- UserPreferences.ini | 34 +++++++++++++++++----------------- src/Configuration.py | 2 ++ src/Main.py | 34 ++++++++++++++++++++++++---------- src/ScheduleBlocks.py | 7 +++++-- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/UserPreferences.ini b/UserPreferences.ini index 9840f44..368dd55 100644 --- a/UserPreferences.ini +++ b/UserPreferences.ini @@ -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 diff --git a/src/Configuration.py b/src/Configuration.py index c101063..0747fa9 100644 --- a/src/Configuration.py +++ b/src/Configuration.py @@ -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. diff --git a/src/Main.py b/src/Main.py index 83c1b3e..3ca8a30 100755 --- a/src/Main.py +++ b/src/Main.py @@ -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. """ @@ -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, " @@ -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) @@ -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.") @@ -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 @@ -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 diff --git a/src/ScheduleBlocks.py b/src/ScheduleBlocks.py index 0ce5ee1..4059d5f 100755 --- a/src/ScheduleBlocks.py +++ b/src/ScheduleBlocks.py @@ -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. """ @@ -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