import json class CheckReporter: def __init__(self, output_passes): self.checks = list() self.output_passes = output_passes def append(self, check): self.checks.append(check) def failed(self): return list(r for r in self.checks if not r.check_successful) def num_failed(self): return len(self.failed()) def report(self): ... class JSONReporter(CheckReporter): def __make_check_serialisable(check): """ Converts a CertCheckResult for json serialisation :param check: CertCheckResult as output by checks :return: check as dict() with appropriate type conversions for json.dumps """ ret = check._asdict() if check.expiration_date: ret["expiration_date"] = check.expiration_date.strftime("%c %Z") return ret def report(self): return json.dumps( list( map( lambda check: JSONReporter.__make_check_serialisable(check), self.checks if self.output_passes else self.failed(), ) ), indent=4, ) class DefaultReporter(CheckReporter): def append(self, check): super().append(check) if check.check_successful and not self.output_passes: return result = f"[{'PASS' if check.check_successful else 'FAIL'}] Check host {check.hostname}" if check.debug: result += f" - {check.debug}" if check.expiration_date: result += f" - Certificate expires on {check.expiration_date} {check.expiration_date.tzname()}" print(result)