diff --git a/api/ECNQueue.py b/api/ECNQueue.py index 88c7e0b..d0c8ea9 100644 --- a/api/ECNQueue.py +++ b/api/ECNQueue.py @@ -173,20 +173,32 @@ def __getContent(self) -> list: contentEnd = len(self.__rawItem) - 1 return self.__rawItem[ contentStart : contentEnd ] - # TODO: Implement section parsing. def __parseSections(self) -> list: sections = [] - initialMessageContent = [] - #headerEnd = self.__getHeaderBoundary() contentStart = self.__getHeaderBoundary() + 1 contentEnd = len(self.__rawItem) - 1 # Find line numbers where sections start sectionBoundaries = [ {"start": contentStart} ] + + # Parses the entire contents of the message, stores everything before any delimiter as the initial message + initialMessageContent = [] + endInitialMessage = False + for lineNumber in range(contentStart, contentEnd + 1): + line = self.__rawItem[lineNumber] + if line.startswith("***") or line.startswith("===") and not line.startswith("===="): + endInitialMessage = True + #Soters what line every delimeter starts/ends + sectionBoundaries.append({"start": lineNumber}) - directoryInfoPattern = ["\tName: ", + if endInitialMessage == False: + initialMessageContent.append(line) + + # All possible Directory Items + directoryInfoPattern = [ + "\tName: ", " Login: ", " Computer: ", " Location: ", @@ -197,37 +209,42 @@ def __parseSections(self) -> list: " Zero Dir: ", " User ECNDB: ", " Host ECNDB: ", - " Subject: "] + " Subject: " + ] directoryInfo = [] - - endInitialMessage = False - for lineNumber in range(contentStart, contentEnd + 1): - line = self.__rawItem[lineNumber] - if line.startswith("***") or line.startswith("===") and not line.startswith("===="): - endInitialMessage = True - sectionBoundaries.append({"start": lineNumber}) - - if endInitialMessage == False: - initialMessageContent.append(line) - - lineCounter = 0 + + # Stores line numbers for directory info that can be removed from initial content linesToRemove = [] - initialMessageParsed = False - for lineContents in initialMessageContent: + lineCounter = 0 + # Directory Info is stored + for lineContents in initialMessageContent: for itemsindirectory in directoryInfoPattern: if lineContents.startswith(itemsindirectory): directoryInfo.append(lineContents) linesToRemove.append(lineCounter) + # Breaks loop early if a non new line is ecnountered and if a directory Item wasn't found + elif lineContents != "\n": + break lineCounter = lineCounter + 1 - for stuff in sorted(linesToRemove, reverse=True): - initialMessageContent.pop(stuff) + # Appends Directory Information into the sections array + sections.append( + {"type": "directoryInformation", + "content": directoryInfo} + ) + + # Sorts the linesToRemove array in reverse and deletes the specified lines from the initial message + for lineNums in sorted(linesToRemove, reverse=True): + initialMessageContent.pop(lineNums) + # Gets the initial message date from the header initialMessageDateStr = self.__getMostRecentHeaderByType("Date") initialMessageFormattedDate = "" initialMessageFormattedTime = "" + + # Attempts to format the date and time into utc format try: dateObject = parse(initialMessageDateStr) initialMessageFormattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") @@ -236,30 +253,66 @@ def __parseSections(self) -> list: initialMessageFormattedDate = "invalid" initialMessageFormattedTime = "Invalid" - initialMessageCCMasterList =[] + # Parses the header looking for CC recipients of the initial message + initialMessageCCSection =[] initialMessageCCList = email.utils.getaddresses([self.__getMostRecentHeaderByType("CC")]) + # Parses the CC list and stores the cc recipient information in a list of dictionaries for ccRecipients in initialMessageCCList: - initialMessageCCMasterList.append( + initialMessageCCSection.append( {"name": ccRecipients[0], "email": ccRecipients[1]} ) - + # Appends the initial message information to the sections array sections.append( {"type": "initialMessage", "date": initialMessageFormattedDate, "time": initialMessageFormattedTime, "userName": self.__parseFromData(data="userName"), "userEmail": self.__parseFromData(data="userEmail"), - "ccRecipients": initialMessageCCMasterList, + "ccRecipients": initialMessageCCSection, "content": initialMessageContent} ) - if len(directoryInfo) > 0: - sections.append( - {"type": "directoryInformation", - "content": directoryInfo - } + # Stores all assignment history in a list of dictionaries + assignmentHistoryList = [] + assignedBy = "" + assignedDate = "" + assignedTime = "" + assignedTo = "" + + # Parses the header looking for assignment delimeters and stores info into their respective variables + for headerContent in range(0, contentStart): + line = self.__rawItem[headerContent] + if line.startswith("Assigned-To: "): + assignedTo = (re.search("(?<=Assigned-To: )(.*)", line)).group() + elif line.startswith("Assigned-To-Updated-Time: "): + + # Attempts to format the date and time into utc format + try: + dateFromLine = (re.search("(?<=Assigned-To-Updated-Time: )(.*)", line)).group() + dateObject = parse(dateFromLine) + assignedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") + assignedTime = dateObject.strftime("%H:%M:%S%z") + except: + initialMessageFormattedDate = "invalid" + initialMessageFormattedTime = "Invalid" + + # Checks for the ending delimiter for an assignmnet event and appends it to the assignment history list + elif line.startswith("Assigned-To-Updated-By: "): + assignedBy = (re.search("(?<=Assigned-To-Updated-By: )(.*)", line)).group() + assignmentHistoryList.append( + {"by": assignedBy, + "date": assignedDate, + "time": assignedTime, + "to": assignedTo} + ) + + # Appends the whole list of assignment history to the sections list + sections.append( + {"type": "assign", + "content": assignmentHistoryList} ) + sectionBoundaries.append({"start": contentEnd + 1}) # Set line number where section end @@ -269,9 +322,7 @@ def __parseSections(self) -> list: # Remove End of File boundary del sectionBoundaries[-1] - #for linestuff in range(contentStart, contentEnd): - #initialMessageContent.append(linestuff) - # Make list of sections and parse content + # Different delimiters for different sections delimiters = [ {"name": "edit", "pattern": "*** Edited"}, {"name": "status", "pattern": "*** Status"}, @@ -291,22 +342,18 @@ def __parseSections(self) -> list: sectionContent = self.__rawItem[boundary["start"] : boundary["end"]] - - - #if sectionType is None: - #sectionType = "initialMessage" - if sectionType == "edit": formattedDate = "" formattedTime = "" editedBy = "" - #parses for the author of the edit, which is located between the "*** Edited by: " and " at:" substrings + # Parses for the author of the edit, which is located between the "*** Edited by: " and " at:" substrings editedBy = (re.search("(?<=\*{3} Edited by: )(.*)(?= at:)", line)).group() - #parses for the date and time of the edit, which is located between the " at: " and "***\n" substrings + # Parses for the date and time of the edit, which is located between the " at: " and "***\n" substrings dateTimeString = (re.search("(?<= at: )(.*)(?= \*\*\*\n)", line)).group() + # Attempts to format the date and time into utc format try: dateObject = parse(dateTimeString) formattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") @@ -645,5 +692,5 @@ def getQueues() -> list: return queues if __name__ == "__main__": - item = Item("aae", 7) + item = Item("ce", 11) print()