Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #59: Take Stamp from Email Date #59

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- New `NotificationData` class that enables easier data injection for simple and complex data objects, such as
emails.
- Tests refactor to make them more specific to each type of data, mocking interfaces between different classes.
- #59 - Added a new parser `EmailDateParser` that uses the temail `Date` to get the `Stamp` and use in most of the `Providers` via the `CombinedProcessor`. Also, `Maintenance.stamp` attribute is mandatory.

### Fixed

Expand Down
9 changes: 4 additions & 5 deletions circuit_maintenance_parser/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from enum import Enum

from typing import List, Optional
from typing import List

from pydantic import BaseModel, validator, StrictStr, StrictInt, Extra

Expand Down Expand Up @@ -99,10 +99,10 @@ class Maintenance(BaseModel, extra=Extra.forbid):
status: defines the overall status or confirmation for the maintenance
start: timestamp that defines the start date of the maintenance in GMT
end: timestamp that defines the end date of the maintenance in GMT
stamp: timestamp that defines the update date of the maintenance in GMT
organizer: defines the contact information included in the original notification

Optional attributes:
stamp: timestamp that defines the update date of the maintenance in GMT
summary: description of the maintenace notification
uid: specific unique identifier for each notification
sequence: sequence number - initially zero - to serialize updates in case they are received or processed out of
Expand All @@ -123,7 +123,7 @@ class Maintenance(BaseModel, extra=Extra.forbid):
... summary="This is a maintenance notification",
... uid="1111",
... )
Maintenance(provider='A random NSP', account='12345000', maintenance_id='VNOC-1-99999999999', circuits=[CircuitImpact(circuit_id='123', impact=<Impact.NO_IMPACT: 'NO-IMPACT'>), CircuitImpact(circuit_id='456', impact=<Impact.OUTAGE: 'OUTAGE'>)], status=<Status.COMPLETED: 'COMPLETED'>, start=1533704400, end=1533712380, organizer='[email protected]', stamp=1533595768, uid='1111', sequence=1, summary='This is a maintenance notification')
Maintenance(provider='A random NSP', account='12345000', maintenance_id='VNOC-1-99999999999', circuits=[CircuitImpact(circuit_id='123', impact=<Impact.NO_IMPACT: 'NO-IMPACT'>), CircuitImpact(circuit_id='456', impact=<Impact.OUTAGE: 'OUTAGE'>)], status=<Status.COMPLETED: 'COMPLETED'>, start=1533704400, end=1533712380, stamp=1533595768, organizer='[email protected]', uid='1111', sequence=1, summary='This is a maintenance notification')
"""

provider: StrictStr
Expand All @@ -133,11 +133,10 @@ class Maintenance(BaseModel, extra=Extra.forbid):
status: Status
start: StrictInt
end: StrictInt
stamp: StrictInt
organizer: StrictStr

# Non mandatory attributes

stamp: Optional[StrictInt] = None
uid: StrictStr = "0"
sequence: StrictInt = 1
summary: StrictStr = ""
Expand Down
19 changes: 19 additions & 0 deletions circuit_maintenance_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import quopri
from typing import Iterable, Union, Dict, List
from email.utils import parsedate_tz, mktime_tz

import bs4 # type: ignore
from bs4.element import ResultSet # type: ignore
Expand Down Expand Up @@ -161,3 +162,21 @@ def clean_line(line):
line = line.strip()
# TODO: below may not be needed if we use `quopri.decodestring()` on the initial email file?
return line.replace("=C2", "").replace("=A0", "").replace("\r", "").replace("=", "").replace("\n", "")


class EmailDateParser(Parser):
"""Parser for Email Date."""

_data_types = ["email-header-date"]

def parse(self, raw: bytes) -> List[Dict]:
"""Method that returns a list of Maintenance objects."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this method only support specific formats of dates?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only supports Email Date format

try:
parsed_date = parsedate_tz(raw.decode())
if parsed_date:
result = [{"stamp": mktime_tz(parsed_date)}]
logger.debug("Successful parsing for %s", self.__class__.__name__)
return result
raise ParserError("Not parsed_date available.")
except Exception as exc:
raise ParserError from exc
18 changes: 9 additions & 9 deletions circuit_maintenance_parser/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from circuit_maintenance_parser.output import Maintenance
from circuit_maintenance_parser.data import NotificationData
from circuit_maintenance_parser.parser import ICal
from circuit_maintenance_parser.parser import ICal, EmailDateParser
from circuit_maintenance_parser.errors import ProcessorError, ProviderError
from circuit_maintenance_parser.processor import SimpleProcessor, GenericProcessor
from circuit_maintenance_parser.processor import CombinedProcessor, SimpleProcessor, GenericProcessor

from circuit_maintenance_parser.parsers.cogent import HtmlParserCogent1
from circuit_maintenance_parser.parsers.gtt import HtmlParserGTT1
Expand Down Expand Up @@ -100,7 +100,7 @@ class Cogent(GenericProvider):
"""Cogent provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserCogent1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserCogent1]),
]
_default_organizer = "[email protected]"

Expand All @@ -115,7 +115,7 @@ class GTT(GenericProvider):
"""GTT provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserGTT1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserGTT1]),
]
_default_organizer = "[email protected]"

Expand All @@ -124,7 +124,7 @@ class Lumen(GenericProvider):
"""Lumen provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserLumen1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserLumen1]),
]
_default_organizer = "[email protected]"

Expand All @@ -133,7 +133,7 @@ class Megaport(GenericProvider):
"""Megaport provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserMegaport1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserMegaport1]),
]
_default_organizer = "[email protected]"

Expand Down Expand Up @@ -161,7 +161,7 @@ class Telstra(GenericProvider):

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[ICal]),
SimpleProcessor(data_parsers=[HtmlParserTelstra1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserTelstra1]),
]
_default_organizer = "[email protected]"

Expand All @@ -170,7 +170,7 @@ class Turkcell(GenericProvider):
"""Turkcell provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserTurkcell1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserTurkcell1]),
]
_default_organizer = "[email protected]"

Expand All @@ -179,7 +179,7 @@ class Verizon(GenericProvider):
"""Verizon provider custom class."""

_processors: List[GenericProcessor] = [
SimpleProcessor(data_parsers=[HtmlParserVerizon1]),
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserVerizon1]),
]
_default_organizer = "[email protected]"

Expand Down
1 change: 1 addition & 0 deletions tests/unit/data/date/email_date_1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mon, 1 Feb 2021 09:33:34 +0000
5 changes: 5 additions & 0 deletions tests/unit/data/date/email_date_1_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"stamp": 1612172014
}
]
Loading