Skip to content

Commit

Permalink
Refactor to use new format_date_string function
Browse files Browse the repository at this point in the history
  • Loading branch information
benne238 committed Jun 28, 2021
1 parent dc11658 commit 12407b0
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions src/webqueue2api/parser/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from .config import config
from .errors import ItemDoesNotExistError
from .utils import format_date_string



Expand Down Expand Up @@ -76,7 +77,7 @@ def __init__(self, queue: str, number: int, headers_only: bool = False) -> None:
self.priority = self.__get_most_recent_header_by_type("Priority")
self.department = self.__get_most_recent_header_by_type("Department")
self.building = self.__get_most_recent_header_by_type("Building")
self.date_received = self.__get_formatted_date(self.__get_most_recent_header_by_type("Date"))
self.date_received = format_date_string(self.__get_most_recent_header_by_type("Date"))
self.json_data = self.__generate_json_data()

def __generate_json_data(self) -> dict:
Expand Down Expand Up @@ -134,7 +135,7 @@ def __get_time_last_updated(self) -> str:
"""
unix_time = os.path.getmtime(self.path)
formatted_time = time.strftime('%m-%d-%y %I:%M %p', time.localtime(unix_time))
return self.__get_formatted_date(formatted_time)
return format_date_string(formatted_time)

def __get_raw_item(self) -> list:
"""Returns a list of all lines in the item file
Expand Down Expand Up @@ -210,7 +211,7 @@ def __parse_headers(self) -> list:
]

for key in message.keys():
headers.append({"type": key, "content": self.__get_formatted_date(message[key]) if key in date_headers else message[key]})
headers.append({"type": key, "content": format_date_string(message[key]) if key in date_headers else message[key]})

return headers

Expand Down Expand Up @@ -531,7 +532,7 @@ def __assignmentParsing(self, contentStart: int) -> list:
dateFromLine = (
re.search("(?<=Assigned-To-Updated-Time: )(.*)", line)).group()

assignedDateTime = self.__get_formatted_date(dateFromLine)
assignedDateTime = format_date_string(dateFromLine)

# Gets who assigned the Item
elif line.startswith("Assigned-To-Updated-By: "):
Expand Down Expand Up @@ -579,7 +580,7 @@ def __initialMessageParsing(self, content: list) -> dict:
rawMessageDateStr = self.__get_most_recent_header_by_type("Date")

# Sets datetime in the intialMessage dictionary to UTC formatted date
initialMessageDictionary["datetime"] = self.__get_formatted_date(
initialMessageDictionary["datetime"] = format_date_string(
rawMessageDateStr)

initialMessageDictionary["from_name"] = self.__parse_from_data(
Expand Down Expand Up @@ -679,7 +680,7 @@ def __editParsing(self, content: list, lineNum: int) -> dict:
return self.__errorParsing(delimiterLine, lineNum, errorMessage)

# Attempts to format the date and time into utc format
editInfo["datetime"] = self.__get_formatted_date(dateTimeString)
editInfo["datetime"] = format_date_string(dateTimeString)

# Remove the delimiter String and unecessary newlines
editInfo["content"] = self.__getFormattedSectionContent(content)
Expand Down Expand Up @@ -737,7 +738,7 @@ def __replyToParsing(self, content: list, lineNum: int) -> dict:
return self.__errorParsing(delimiterLine, lineNum, errorMessage)

# Formats date to UTC
replyInfo["datetime"] = self.__get_formatted_date(dateTimeString)
replyInfo["datetime"] = format_date_string(dateTimeString)

replyInfo["content"] = self.__getFormattedSectionContent(content)

Expand Down Expand Up @@ -791,7 +792,7 @@ def __statusParsing(self, content: list, lineNum: int) -> dict:
return self.__errorParsing(delimiterLine, lineNum, errorMessage)

# Formats the date to UTC
statusInfo["datetime"] = self.__get_formatted_date(dateTimeString)
statusInfo["datetime"] = format_date_string(dateTimeString)

# Remove the delimiter String and unecessary newlines
statusInfo["content"] = self.__getFormattedSectionContent(content)
Expand Down Expand Up @@ -920,7 +921,7 @@ def __userReplyParsing(self, replyContent: list, lineNumber: int) -> dict:
return self.__errorParsing(line, lineNumber + lineNum + 1, errorMessage)

# Formatts the date to UTC
replyFromInfo["datetime"] = self.__get_formatted_date(dateStr)
replyFromInfo["datetime"] = format_date_string(dateStr)

elif line.startswith("Cc: ") and newLineCounter == 1:

Expand Down Expand Up @@ -1025,7 +1026,7 @@ def __errorParsing(self, line: str, lineNum: int, expectedSyntax: str) -> dict:
errorDictionary["type"] = "parse_error"

# Dateime of the parse error
errorDictionary["datetime"] = self.__get_formatted_date(
errorDictionary["datetime"] = format_date_string(
str(datetime.datetime.now()))

# Item filepath
Expand Down Expand Up @@ -1175,25 +1176,6 @@ def __get_user_alias(self) -> str:

return email_user if email_domain.endswith("purdue.edu") else ""

def __get_formatted_date(self, date: str) -> str:
"""Returns the date/time formatted as RFC 8601 YYYY-MM-DDTHH:MM:SS+00:00.
Returns empty string if the string argument passed to the function is not a datetime.
See: https://en.wikipedia.org/wiki/ISO_8601
Returns:
str: Properly formatted date/time recieved or empty string.
"""
try:
# This date is never meant to be used. The default attribute is just to set timezone.
parsed_date = parse(date, default=datetime.datetime(
1970, 1, 1, tzinfo=tz.gettz('EDT')))
except:
return ""

parsed_date_string = parsed_date.strftime("%Y-%m-%dT%H:%M:%S%z")

return parsed_date_string

def to_json(self) -> dict:
"""Returns a JSON safe representation of the item.
Expand Down

0 comments on commit 12407b0

Please sign in to comment.