From 68e3d0b27bbcf737982ddcac3799aec6648ff0dc Mon Sep 17 00:00:00 2001 From: Dhwani Patel Date: Tue, 11 Jun 2024 14:53:23 -0600 Subject: [PATCH] Use pyprint instead of print --- fixity/fixity.py | 20 +++++++------------- fixity/utils.py | 8 ++++++++ tests/test_fixity.py | 5 ++--- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/fixity/fixity.py b/fixity/fixity.py index 067d1b3..176d9cb 100644 --- a/fixity/fixity.py +++ b/fixity/fixity.py @@ -135,7 +135,7 @@ def scan( session_id=session_id, ) except reporting.ReportServiceException: - print(f"Unable to POST pre-scan report to {report_url}", file=sys.stderr) + utils.pyprint(f"Unable to POST pre-scan report to {report_url}") try: status, report = storage_service.scan_aip( @@ -148,9 +148,9 @@ def scan( force_local=force_local, ) report_data = json.loads(report.report) - print(scan_message(aip, status, report_data["message"]), file=sys.stderr) + utils.pyprint(scan_message(aip, status, report_data["message"])) except Exception as e: - print(str(e), file=sys.stderr) + utils.pyprint(str(e)) status = None if hasattr(e, "report") and e.report: report = e.report @@ -181,10 +181,7 @@ def scan( aip, report, report_url, report_auth=report_auth, session_id=session_id ) except reporting.ReportServiceException: - print( - f"Unable to POST report for AIP {aip} to remote service", - file=sys.stderr, - ) + utils.pyprint(f"Unable to POST report for AIP {aip} to remote service") if report: session.add(report) @@ -240,17 +237,14 @@ def scanall( if not scan_success: success = False except Exception as e: - print( - "Internal error encountered while scanning AIP {} ({})".format( - aip["uuid"], type(e).__name__ - ) + utils.pyprint( + f"Internal error encountered while scanning AIP {aip['uuid']} ({type(e).__name__})" ) - if throttle_time: sleep(throttle_time) if count > 0: - print("Successfully scanned", count, "AIPs", file=sys.stderr) + utils.pyprint(f"Successfully scanned {count} AIPs") return success diff --git a/fixity/utils.py b/fixity/utils.py index 9d8b8d1..0fdc74d 100644 --- a/fixity/utils.py +++ b/fixity/utils.py @@ -1,3 +1,4 @@ +import sys from datetime import datetime from datetime import timezone from uuid import UUID @@ -33,3 +34,10 @@ def check_valid_uuid(uuid): def utcnow(): return datetime.now(timezone.utc) + + +def pyprint(message): + if message.startswith("Internal error encountered"): + print(message, file=sys.stdout) + else: + print(message, file=sys.stderr) diff --git a/tests/test_fixity.py b/tests/test_fixity.py index 8e2be18..0033fc9 100644 --- a/tests/test_fixity.py +++ b/tests/test_fixity.py @@ -462,6 +462,5 @@ def test_main_handles_exceptions_if_scanall_fails(_get, monkeypatch, capsys): captured = capsys.readouterr() assert ( - captured.out.strip() - == f"Internal error encountered while scanning AIP {aip_id} (StorageServiceError)" - ) + f"Internal error encountered while scanning AIP {aip_id} (StorageServiceError)\n" + ) in captured.out