Skip to content

Commit

Permalink
Helper function __getFormattedMessageContent created to remove delimi…
Browse files Browse the repository at this point in the history
…ters and leading and trailing newlines from a message
  • Loading branch information
Jacob Daniel Bennett committed Oct 2, 2020
1 parent 19eb144 commit 0b3ed98
Showing 1 changed file with 84 additions and 36 deletions.
120 changes: 84 additions & 36 deletions api/ECNQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,36 +245,9 @@ def __parseSections(self) -> list:
initialMessageContent.append(line)

# Removes unecessary newlines from the begining and the end of the initial message

newLinebegining = True
newLineEnd = True

while (newLinebegining or newLineEnd) and len(initialMessageContent) > 1:
# Initializes the Length of Message content each iteration of the loop
initialmessagecontentLength = len(initialMessageContent)

# Checks if the last line is a newline
if initialMessageContent[initialmessagecontentLength -1] == "\n":

# Deletes the last line if it is a newline
initialMessageContent.pop(initialmessagecontentLength - 1)

# If the previous condition failed, then set the new line end to False if it isn't false already
elif newLineEnd == True:

newLineEnd = False
initialMessageContent = self.__getFormattedMessageContent(initialMessageContent)

# Checks if the first line in message content is a newline
if initialMessageContent[0] == "\n":

# Removes the first line in message content if it is a newline
initialMessageContent.pop(0)

# If the previous condition failed, then set the new line begining to False if it isn't false already
elif newLinebegining == True:

newLinebegining = False

# Gets the initial message date from the header
initialMessageDateStr = self.__getMostRecentHeaderByType("Date")

Expand Down Expand Up @@ -385,8 +358,8 @@ def __parseSections(self) -> list:
# Returns a dictionary with edit information
editInfo = self.__editParsing(line)

# Remove the delimiter String
sectionContent.remove(line)
# Remove the delimiter String and unecessary newlines
sectionContent = self.__getFormattedMessageContent(sectionContent)

# Appends content of the edit message to the dictionary
editInfo["content"] = sectionContent
Expand All @@ -401,6 +374,9 @@ def __parseSections(self) -> list:
# Returns a dictionary with reply-to information
replyToInfo = self.__replyToParsing(line)

# Removes the begining delimiter
sectionContent = self.__getFormattedMessageContent(sectionContent)

# Appends content of the reply-to message to the dicionary
replyToInfo['content'] = sectionContent

Expand All @@ -413,6 +389,9 @@ def __parseSections(self) -> list:

# Returns a dictionary with status information
statusInfo = self.__statusParsing(line)

# Removes the begining delimiter
sectionContent = self.__getFormattedMessageContent(sectionContent)

# Appends content to empty content key to avoid passing large amounts of info that isnt used within the function
statusInfo['content'] = sectionContent
Expand All @@ -427,9 +406,6 @@ def __parseSections(self) -> list:
# Returns a dictionary with userReply information
replyFromInfo = self.__userReplyParsing(sectionContent)

# Appends content to empty content key to avoid passing large amounts of info that isnt used within the function
replyFromInfo['content'] = sectionContent

# Appends the replyFrom to sections
sections.append(replyFromInfo)

Expand Down Expand Up @@ -574,8 +550,13 @@ def __userReplyParsing(self, replyContent: list) -> dict:
ccRecipientsList = []
newLineCounter = 0

#Parses the section content looking for any line that starts with a metadata
for line in replyContent:
# Delimiter information line numbers to remove from reply from user
linesToRemove =[]


# Parses the section content looking for any line that starts with a metadata, also tracks the line
# number with the enumerate function
for lineNum, line in enumerate(replyContent):

#Checks for a newline and breaks for loop on second occurance of a newline
if line == "\n":
Expand All @@ -590,6 +571,10 @@ def __userReplyParsing(self, replyContent: list) -> dict:
# Matches everything after "Subject: " in the line
subject = (re.search("(?<=Subject: )(.*)", line)).group()

linesToRemove.append(lineNum)

continue

elif line.startswith("From: "):

# Returns a list of tuples with name and email information
Expand All @@ -601,6 +586,10 @@ def __userReplyParsing(self, replyContent: list) -> dict:
# The email is stored in the second index of the tuple
repliedByEmail = emailList[0][1]

linesToRemove.append(lineNum)

continue

elif line.startswith("Date: "):

# Matches everything after "Date: "
Expand All @@ -609,6 +598,10 @@ def __userReplyParsing(self, replyContent: list) -> dict:
# Formatts the date to UTC
formattedDateTime = self.__getFormattedDate(dateStr)

linesToRemove.append(lineNum)

continue

elif line.startswith("Cc: "):

# Returns a list of tuples with email information
Expand All @@ -622,19 +615,68 @@ def __userReplyParsing(self, replyContent: list) -> dict:
{"name":cc[0],
"email":cc[1]}
)

linesToRemove.append(lineNum)

continue

# Deletes reduntant lines from the message content in reverse order
for lineNum in sorted(linesToRemove, reverse = True):
replyContent.pop(lineNum)

# Strips any unnecessary newlines or any delimiters frm the message content
replyContent = self.__getFormattedMessageContent(replyContent)

replyFromInfo = {
"type": "replyFromUser",
"datetime": formattedDateTime,
"subject": subject,
"userName": repliedByName,
"userEmail": repliedByEmail,
"content": "",
"content": replyContent,
"ccRecipients": ccRecipientsList
}

return replyFromInfo

def __getFormattedMessageContent(self, messageContent: list) -> list:
"""Returns a list with message content that is stripped of unnecessary newlines and begining delimiters
Returns:
list: formattedMessageContent
"""
# Parses looking for the reply-from-user ending delimiter adn removes the first line that contains the ending delimiter.


# Continually loops while looking at the first line of messageContent, removing it from messageContent
# if its a newline. Doesn't run if there is only one line in message content.

while len(messageContent) > 1:
if messageContent[0] == "\n" or messageContent[0].startswith("***") or messageContent[0].startswith("===") :
messageContent.pop(0)

else:
# Breaks the loop if the first line isn't a newline
break

while len(messageContent) > 1:

# Initializes the Length of messageContent each iteration of the loop
messagecontentLength = len(messageContent)

# Checks if the last line is a newline
if messageContent[messagecontentLength -1] == "\n" or messageContent[messagecontentLength -1].startswith("===="):

# Deletes the last line if it is a newline
messageContent.pop(messagecontentLength - 1)

else:
# Breaks the loop if the last line isn't a newline
break

return messageContent


def __isLocked(self) -> Union[str, bool]:
"""Returns a string info about the lock if true and a bool False if false
Expand Down Expand Up @@ -825,3 +867,9 @@ def getQueues() -> list:

return queues

if __name__ == "__main__":
item = Item("aae", 2)
print()
# for queue in getQueues():
# for item in queue.items:
# print(f"${item.queue} ${item.number}")

0 comments on commit 0b3ed98

Please sign in to comment.