From 5adb3285cb2458f050dee0ae96e8e8bc1b08bfb4 Mon Sep 17 00:00:00 2001 From: Jacob Daniel Bennett Date: Mon, 5 Oct 2020 10:10:32 -0400 Subject: [PATCH] Creation of helper function for formating error parsing output --- api/ECNQueue.py | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/api/ECNQueue.py b/api/ECNQueue.py index 5cdc431..0ee74ca 100644 --- a/api/ECNQueue.py +++ b/api/ECNQueue.py @@ -227,17 +227,12 @@ def __parseSections(self) -> list: # Checks for nesteded delimiters within the reply from user elif replyFromUserDelimiter == True and (line.startswith("===") or line.startswith("***")) and not line.startswith("===="): - - parseErrorMessage = ("Nested delimiter encountered on line " - + str(lineNumber) + ":\n" - + "\t " + line) - # Creates a dictionary with information regarding to the nested delimiter - errorDictionary = { - "type": "parseError", - "datetime": self.__getFormattedDate(str(datetime.datetime.now())), - "content": parseErrorMessage - } + columnNum = 0 + + errorMessage = "Nested delimiter encountered" + + errorDictionary = self.__errorParsing(line, lineNumber, columnNum, errorMessage) # Appends the error dictionary to sections sections.append(errorDictionary) @@ -622,8 +617,10 @@ def __userReplyParsing(self, replyContent: list) -> dict: elif line.startswith("Date: "): # Matches everything after "Date: " - dateStr = (re.search("(?<=Date: )(.*)", line)).group() - + try: + dateStr = (re.search("(?<=Date: )(.*)", line)).group() + except: + dateStr = "" # Formatts the date to UTC formattedDateTime = self.__getFormattedDate(dateStr) @@ -705,6 +702,34 @@ def __getFormattedMessageContent(self, messageContent: list) -> list: return messageContent + def __errorParsing(self, line: str, lineNum: int, lineColumn: int, errorMessage: str) -> dict: + """Returns a dictionary with error parse information + + Returns: + { + dictionary: "type": "parse_error", + datetime: time_of_execution, + content: [ + 'expected value item_row_num:item_column_num', + 'current line in item' + ] + } + """ + + errorDictionary = { + "type": "parse_error", + "datetime": self.__getFormattedDate(str(datetime.datetime.now())), + "content": [] + } + + # Error message with line and column numbers + errorMessage = errorMessage + " at " + str(lineNum) + ":" + str(lineColumn) + + errorDictionary["content"].append(errorMessage) + + errorDictionary["content"].append(line) + + return errorDictionary def __isLocked(self) -> Union[str, bool]: """Returns a string info about the lock if true and a bool False if false