Skip to content

Commit

Permalink
Use pyprint instead of print
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed Jun 12, 2024
1 parent aed507c commit 68e3d0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
20 changes: 7 additions & 13 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions fixity/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from datetime import datetime
from datetime import timezone
from uuid import UUID
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 68e3d0b

Please sign in to comment.