From 8015e6a25c56727bcd4ae58377eaf2791e46e6c5 Mon Sep 17 00:00:00 2001 From: Sean Whalen Date: Tue, 6 Feb 2018 10:56:06 -0500 Subject: [PATCH 1/2] Update build script --- build.sh | 8 ++++++++ makedocs.sh | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100755 build.sh delete mode 100644 makedocs.sh diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..217ba35 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +. ~/venv/domainaware/bin/activate +cd docs && make html && cp -r _build/html/* ../../parsedmarc-docs/ +cd .. +rm -rf dist/ build/ +python setup.py bdist_wheel + diff --git a/makedocs.sh b/makedocs.sh deleted file mode 100644 index 4ad87ec..0000000 --- a/makedocs.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -. ~/venv/domainaware/bin/activate -cd docs && make html && cp -r build/html/* ../../parsedmarc-docs/ From ff4e32e43d1732753e796cb48a268472d81df611 Mon Sep 17 00:00:00 2001 From: Sean Whalen Date: Thu, 8 Feb 2018 15:13:35 -0500 Subject: [PATCH 2/2] 1.1.0 --- CHANGELOG.md | 4 ++++ parsedmarc.py | 34 ++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da12225..4b81b34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +1.1.0 +----- +- Add `extract_xml()` and `human_timespamp_to_datetime` methods + 1.0.5 ----- - Prefix public suffix and GeoIP2 database filenames with `.` diff --git a/parsedmarc.py b/parsedmarc.py index ef3d025..ca90b5a 100644 --- a/parsedmarc.py +++ b/parsedmarc.py @@ -30,7 +30,7 @@ from requests import get import geoip2.database import geoip2.errors -__version__ = "1.0.5" +__version__ = "1.1.0" logger = logging.getLogger(__name__) logger.setLevel(logging.WARNING) @@ -161,7 +161,7 @@ def _timestamp_to_human(timestamp): return _timestamp_to_datetime(timestamp).strftime("%Y-%m-%d %H:%M:%S") -def _human_timestamp_to_datetime(human_timestamp): +def human_timestamp_to_datetime(human_timestamp): """ Converts a human-readable timestamp into a Python ``DateTime`` object @@ -428,17 +428,17 @@ def parse_aggregate_report_xml(xml, nameservers=None, timeout=6.0): "{0}".format(error.__str__())) -def parse_aggregate_report_file(_input, nameservers=None, timeout=6.0): - """Parses a file at the given path, a file-like object. or bytes as a - aggregate DMARC report +def extract_xml(_input): + """ + Extracts xml from a zip or gzip file at the given path, file-like object, + or bytes. Args: _input: A path to a file, a file like object, or bytes - nameservers (list): A list of one or more nameservers to use - timeout (float): Sets the DNS timeout in seconds Returns: - OrderedDict: The parsed DMARC aggregate report + str: The extracted XML + """ if type(_input) == str or type(_input) == unicode: file_object = open(_input, "rb") @@ -461,10 +461,28 @@ def parse_aggregate_report_file(_input, nameservers=None, timeout=6.0): raise InvalidAggregateReport("Not a valid zip, gzip, or xml file") file_object.close() + except UnicodeDecodeError: raise InvalidAggregateReport("File objects must be opened in binary " "(rb) mode") + return xml + + +def parse_aggregate_report_file(_input, nameservers=None, timeout=6.0): + """Parses a file at the given path, a file-like object. or bytes as a + aggregate DMARC report + + Args: + _input: A path to a file, a file like object, or bytes + nameservers (list): A list of one or more nameservers to use + timeout (float): Sets the DNS timeout in seconds + + Returns: + OrderedDict: The parsed DMARC aggregate report + """ + xml = extract_xml(_input) + return parse_aggregate_report_xml(xml, nameservers=nameservers, timeout=timeout)