Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions maillogger/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Dict, Optional


REGEX = r'(?P<month>[A-Z][a-z]{2}) (?P<day>[0-9]{,2}) ' \
+ r'(?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}) mail postfix/[a-z]+\[[0-9]+\]: ' \
REGEX = r'(?P<month>[A-Z][a-z]{2}) +(?P<day>[0-9]{,2}) ' \
+ r'(?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}) (?P<hostname>[A-Za-z0-9-]+) postfix/[a-z/]+\[[0-9]+\]: ' \
+ r'(?P<mail_id>[A-Z0-9]+): to=<(?P<to_address>.*@.*)>, ' \
+ r'relay=(?P<relay>.*), delay=(?P<delay>[0-9.]+), ' \
+ r'delays=(?P<delays>[0-9][0-9/.]+), dsn=(?P<dsn>[0-9].[0-9].[0-9]), ' \
Expand Down Expand Up @@ -53,6 +53,7 @@ class ParseResult:
month: InitVar[str]
day: InitVar[str]
time: InitVar[str]
hostname: InitVar[str]

mail_id: str
to_address: str
Expand All @@ -65,13 +66,14 @@ class ParseResult:

datetime: str = field(init=False)

def __post_init__(self, month: str, day: str, time: str) -> None:
def __post_init__(self, month: str, day: str, time: str, hostname: str) -> None:
self.datetime = self.convert2dateime(month, day, time)

def to_dict(self) -> ParseResultType:
return asdict(self)

@staticmethod
def convert2dateime(month: str, day: str, time: str) -> str:
day = day.rjust(2, '0')
tmp = datetime.strptime(f'{month}{day}{time}', '%b%d%H:%M:%S')
return tmp.replace(year=datetime.now().year).strftime('%Y%m%d%H%M%S')