From a5a7f9f22ec6566952fb75d36bdf5c6b84909cef Mon Sep 17 00:00:00 2001 From: Jacob Daniel Bennett Date: Tue, 20 Oct 2020 14:42:57 -0400 Subject: [PATCH] Updated in line commenting and code readability --- api/ECNQueue.py | 98 ++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 58 deletions(-) diff --git a/api/ECNQueue.py b/api/ECNQueue.py index eb6e78d..bb82f73 100644 --- a/api/ECNQueue.py +++ b/api/ECNQueue.py @@ -245,6 +245,7 @@ def __parseSections(self) -> list: sectionBoundaries.append({"start": lineNumber, "type": "initial_message"}) initialMessageSection = False + # Used to set the end line of the last delimiter sectionBoundaries.append({"start": contentEnd + 1}) # Sets the end of the section boundary to the begining of the next section boundary @@ -252,7 +253,7 @@ def __parseSections(self) -> list: sectionBoundaries[boundaryIndex]["end"] = sectionBoundaries[boundaryIndex + 1]["start"] - # Remove End of File boundary + # Remove End of File boundary since the line number has been assigned to the last delimiter del sectionBoundaries[-1] # Parses through all the boundaries in section boundaries @@ -264,69 +265,57 @@ def __parseSections(self) -> list: # Returns all of the lines within the current section sectionContent = self.__rawItem[boundary["start"] : boundary["end"]] + # Appends an initial message dictionary to sections if boundary["type"] == "initial_message": initialMessageDictionary = self.__initialMessageParsing(sectionContent) sections.append(initialMessageDictionary) - - # Checks for each section type - elif boundary["type"] == "edit": - + + elif boundary["type"] == "edit": # Returns a dictionary with edit information editInfo = self.__editParsing(sectionContent, boundary["start"]) - # Checks for a parse error and appends it to sections and exits the function + # Checks for a parse error and appends it, returning the sections list which stops the parsing if editInfo["type"] == "parse_error": - sections.append(editInfo) - return sections # Appends the edit dictionary to sections sections.append(editInfo) elif boundary["type"] == "replyToUser": - # Returns a dictionary with reply-to information replyToInfo = self.__replyToParsing(sectionContent, boundary["start"]) - + + # Checks for a parse error and appends it, returning the sections list which stops the parsing if replyToInfo["type"] == "parse_error": - sections.append(replyToInfo) - return sections # Appends the reply-to to sections sections.append(replyToInfo) elif boundary["type"] == "status": - # Returns a dictionary with status information statusInfo = self.__statusParsing(sectionContent, boundary["start"]) if statusInfo["type"] == "parse_error": - sections.append(statusInfo) - return sections # Appends the status to sections sections.append(statusInfo) elif boundary["type"] == "replyFromUser": - # Returns a dictionary with userReply information replyFromInfo = self.__userReplyParsing(sectionContent, boundary["start"]) if replyFromInfo["type"] == "parse_error": - sections.append(replyFromInfo) - return sections # Appends the replyFrom to sections sections.append(replyFromInfo) - return sections def __directoryParsing(self, directoryStartLine: int) -> dict: @@ -393,6 +382,10 @@ def __directoryParsing(self, directoryStartLine: int) -> dict: return directoryInformation def __assignmentParsing(self, contentStart) -> list: + """Returns a list with assignment information dictionaries + + Returns: + """ assignmentList =[] # Assignment Information @@ -436,7 +429,8 @@ def __initialMessageParsing(self, content: list) -> dict: """Returns a dictionary with initial message information Returns: - dictionary: "type": "initial_message", + dictionary: + "type": "initial_message", "datetime": utcdate, "from_name": from_name, "from_email": user_email, @@ -500,13 +494,20 @@ def __editParsing(self, content: list, lineNum: int) -> dict: Returns: dictionary: "type": "edit", by, datetime and content """ + # Edit Info dictionary + editInfo = {} - formattedDateTime = "" - editedBy = "" + for count, line in enumerate(content): + if line.startswith("===="): + errorMessage = "Reply-from-user ending delimter encountered without Reply-from-user starting delimter" + return self.__errorParsing(line, lineNum + count + 1, errorMessage) + + editInfo["type"] = "edit" + delimiterLine = content[0] # Parses for the author of the edit, which is located between the "*** Edited by: " and " at:" substrings try: - editedBy = (re.search("(?<=\*{3} Edited by: )(.*)(?= at:)", delimiterLine)).group() + editInfo["by"] = (re.search("(?<=\*{3} Edited by: )(.*)(?= at:)", delimiterLine)).group() except: errorMessage = "*** Edited by: [username] at: [date and time] ***\n" return self.__errorParsing(delimiterLine, lineNum, errorMessage) @@ -520,17 +521,10 @@ def __editParsing(self, content: list, lineNum: int) -> dict: return self.__errorParsing(delimiterLine, lineNum, errorMessage) # Attempts to format the date and time into utc format - formattedDateTime = self.__getFormattedDate(dateTimeString) + editInfo["datetime"] = self.__getFormattedDate(dateTimeString) # Remove the delimiter String and unecessary newlines - formattedContent = self.__getFormattedMessageContent(content) - - editInfo = { - "type": "edit", - "datetime": formattedDateTime, - "by": editedBy, - "content": formattedContent - } + editInfo["content"] = self.__getFormattedMessageContent(content) return editInfo @@ -541,19 +535,20 @@ def __replyToParsing(self, content: list, lineNum: int) -> dict: dictionary: "type": "replyToUser", by, datetime and content """ - formattedDateTime = "" - repliedBy = "" + replyInfo = {} + + replyInfo["type"] = "reply_to_user" + delimiterLine = content[0] for count, line in enumerate(content): if line.startswith("===="): errorMessage = "Reply-from-user ending delimter encountered without Reply-from-user starting delimter" return self.__errorParsing(line, lineNum + count + 1, errorMessage) - #tech112 try: # Parses for the author of the reply, which is located between the "*** Replied by: " and " at:" substrings - repliedBy = (re.search("(?<=\*{3} Replied by: )(.*)(?= at:)", delimiterLine)).group() + replyInfo["by"] = (re.search("(?<=\*{3} Replied by: )(.*)(?= at:)", delimiterLine)).group() except: errorMessage = "*** Replied by: [username] at: [date and time] ***\n" return self.__errorParsing(delimiterLine, lineNum, errorMessage) @@ -566,16 +561,9 @@ def __replyToParsing(self, content: list, lineNum: int) -> dict: return self.__errorParsing(delimiterLine, lineNum, errorMessage) # Formats date to UTC - formattedDateTime = self.__getFormattedDate(dateTimeString) + replyInfo["datetime"] = self.__getFormattedDate(dateTimeString) - formattedContent = self.__getFormattedMessageContent(content) - - replyInfo = { - "type": "reply_to_user", - "datetime": formattedDateTime, - "by": repliedBy, - "content": formattedContent - } + replyInfo["content"] = self.__getFormattedMessageContent(content) return replyInfo @@ -586,8 +574,10 @@ def __statusParsing(self, content: list, lineNum: int) -> dict: dictionary: "type": "status", by, datetime and content """ - formattedDateTime = "" - updatedBy = "" + statusInfo = {} + + statusInfo["type"] = "status" + delimiterLine = content[0] for count, line in enumerate(content): @@ -597,7 +587,7 @@ def __statusParsing(self, content: list, lineNum: int) -> dict: # Parses for the author of the status change, which is located between the "*** Status updated by: " and " at:" substrings try: - updatedBy = (re.search("(?<=\*{3} Status updated by: )(.*)(?= at:)", delimiterLine)).group() + statusInfo["by"] = (re.search("(?<=\*{3} Status updated by: )(.*)(?= at:)", delimiterLine)).group() except: errorMessage = "*** Status updated by: [username] at: [date and time] ***\n" @@ -612,18 +602,10 @@ def __statusParsing(self, content: list, lineNum: int) -> dict: return self.__errorParsing(delimiterLine, lineNum, errorMessage) # Formats the date to UTC - formattedDateTime = self.__getFormattedDate(dateTimeString) + statusInfo["datetime"] = self.__getFormattedDate(dateTimeString) # Remove the delimiter String and unecessary newlines - formattedContent = self.__getFormattedMessageContent(content) - - statusInfo = { - "type": "status", - "datetime": formattedDateTime, - "by": updatedBy, - "content": formattedContent - } - + statusInfo["content"] = self.__getFormattedMessageContent(content) return statusInfo