Skip to content

Commit

Permalink
Merge pull request #82 from ECN/enhancement-item-body-section-parsing
Browse files Browse the repository at this point in the history
enhancement-sort-item-sections-by-date
  • Loading branch information
campb303 authored Oct 23, 2020
2 parents b807be4 + 787ec0a commit fb71878
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions api/ECNQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#------------------------------------------------------------------------------#
import os, time, email, re, datetime
from dateutil.parser import parse
from dateutil import tz
from typing import Union
import json

Expand Down Expand Up @@ -284,7 +285,7 @@ def __parseSections(self) -> list:
# 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
return self.__getSortedSections(sections)

# Appends the edit dictionary to sections
sections.append(editInfo)
Expand All @@ -296,7 +297,7 @@ def __parseSections(self) -> list:
# 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
return self.__getSortedSections(sections)

# Appends the reply-to to sections
sections.append(replyToInfo)
Expand All @@ -307,7 +308,7 @@ def __parseSections(self) -> list:

if statusInfo["type"] == "parse_error":
sections.append(statusInfo)
return sections
return self.__getSortedSections(sections)

# Appends the status to sections
sections.append(statusInfo)
Expand All @@ -318,12 +319,15 @@ def __parseSections(self) -> list:

if replyFromInfo["type"] == "parse_error":
sections.append(replyFromInfo)
return sections
return self.__getSortedSections(sections)

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

return sections

sortedSections = self.__getSortedSections(sections)

return sortedSections
#return sections

def __directoryParsing(self, directoryStartLine: int) -> dict:
"""Returns a dictionary with directory information
Expand Down Expand Up @@ -965,6 +969,46 @@ def __errorParsing(self, line: str, lineNum: int, expectedSyntax: str) -> dict:
# returns the error dictionary
return errorDictionary

def __getSortedSections(self, sectionsList: list) -> list:
"""Sorts the sections chronologically by datetime
Example:
[example] need to do
Args:
sections (list): the list of sections to be sorted
Returns:
list: a list of sections sorted by datetime
"""
sectionsLength = len(sectionsList)
sortedSections = []
oldestSection = {}

while len(sortedSections) < sectionsLength:

for iteration, currentSection in enumerate(sectionsList):

if currentSection["type"] == "directory_information":
sortedSections.append(currentSection)
sectionsList.remove(currentSection)
break

if iteration == 0:
oldestSection = currentSection

#datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')

elif parse(currentSection["datetime"]) < parse(oldestSection["datetime"]):
oldestSection = currentSection

if iteration == len(sectionsList) - 1:
sortedSections.append(oldestSection)
sectionsList.remove(oldestSection)

return sortedSections


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 @@ -1065,7 +1109,8 @@ def __getFormattedDate(self, date: str) -> str:
str: Properly formatted date/time recieved or empty string.
"""
try:
parsedDate = parse(date)
parsedDate = parse(date, default= datetime.datetime(2017, 10, 13, tzinfo=tz.gettz('EDT')))
#parsedDate = parse(date, default= datetime.datetime(2017, 10, 13, tzinfo=datetime.timezone.)
except:
return ""

Expand Down

0 comments on commit fb71878

Please sign in to comment.