broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard'
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'soft' if klass == '4' else 'hard'
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Temporary failures are treated as permanent bounces and unsubscribe the recipient.

A report is valid only when its type is multipart/report, its report-type parameter (name and value case-insensitive) is delivery-status, and its second part is message/delivery-status or message/global-delivery-status. Each recipient {final, action, status} gets: failed -> hard for status class 5, soft for 4, unknown otherwise; delayed -> delayed; delivered, relayed or expanded -> ok; anything else unknown (actions are case-insensitive and trimmed). has_original is true when a third part is message/rfc822 or text/rfc822-headers. Invalid reports give {valid False, recipients [], has_original False}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action']
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A capitalised or padded action field is classified as unknown.

A report is valid only when its type is multipart/report, its report-type parameter (name and value case-insensitive) is delivery-status, and its second part is message/delivery-status or message/global-delivery-status. Each recipient {final, action, status} gets: failed -> hard for status class 5, soft for 4, unknown otherwise; delayed -> delayed; delivered, relayed or expanded -> ok; anything else unknown (actions are case-insensitive and trimmed). has_original is true when a third part is message/rfc822 or text/rfc822-headers. Invalid reports give {valid False, recipients [], has_original False}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822',)
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed', 'expanded'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'message/global')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Reports that return only the original headers are treated as having no original.

A report is valid only when its type is multipart/report, its report-type parameter (name and value case-insensitive) is delivery-status, and its second part is message/delivery-status or message/global-delivery-status. Each recipient {final, action, status} gets: failed -> hard for status class 5, soft for 4, unknown otherwise; delayed -> delayed; delivered, relayed or expanded -> ok; anything else unknown (actions are case-insensitive and trimmed). has_original is true when a third part is message/rfc822 or text/rfc822-headers. Invalid reports give {valid False, recipients [], has_original False}.

split: open-access