Skip to content

Commit

Permalink
Updated in line commenting and code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Daniel Bennett committed Oct 20, 2020
1 parent acd0733 commit a5a7f9f
Showing 1 changed file with 40 additions and 58 deletions.
98 changes: 40 additions & 58 deletions api/ECNQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,15 @@ 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
for boundaryIndex in range(0, len(sectionBoundaries) - 1):

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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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):
Expand All @@ -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"

Expand All @@ -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

Expand Down

0 comments on commit a5a7f9f

Please sign in to comment.