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)