Handle non-tuple IMAP fetch responses
The prompts used to arrive at this change since the previous commit Ik krijg de volgende foutmelding: AttributeError: 'int' object has no attribute 'decode' while running verplaats_bestaand.py --account backup --uitvoeren. Any special observations that may be relevant for version management for this version. Be brief. The actual backup run modified local verplaats_log.json with processed INBOX UID entries; that run-state was intentionally not committed. Rerunning should preserve it so already logged moves are skipped.
This commit is contained in:
+16
-4
@@ -132,6 +132,16 @@ def print_progress(folder: str, processed: int, total: int):
|
||||
print(f" Gescand: {processed}/{total} | {folder}")
|
||||
|
||||
|
||||
def fetch_body(data) -> bytes | None:
|
||||
"""Return the first bytes payload from an IMAP FETCH response."""
|
||||
if not data:
|
||||
return None
|
||||
for item in data:
|
||||
if isinstance(item, tuple) and len(item) >= 2 and isinstance(item[1], bytes):
|
||||
return item[1]
|
||||
return None
|
||||
|
||||
|
||||
def safe_select(session: ImapSession, log: dict, stap: str, folder: str, readonly: bool):
|
||||
for attempt in range(2):
|
||||
try:
|
||||
@@ -357,12 +367,13 @@ def stap2_bronmappen_routing(session: ImapSession, log: dict, dry: bool):
|
||||
readonly=dry,
|
||||
)
|
||||
mail = session.mail
|
||||
if status != "OK" or not data or not data[0]:
|
||||
raw_header = fetch_body(data)
|
||||
if status != "OK" or raw_header is None:
|
||||
mislukt += 1
|
||||
log_failure(log, "stap2", bron, uid, None, "fetch_header", status, data)
|
||||
continue
|
||||
|
||||
msg = email.message_from_bytes(data[0][1])
|
||||
msg = email.message_from_bytes(raw_header)
|
||||
from_raw = decode_hdr(msg.get("From", ""))
|
||||
subject = decode_hdr(msg.get("Subject", ""))
|
||||
date_str = msg.get("Date", "")
|
||||
@@ -486,12 +497,13 @@ def stap3_facturen(session: ImapSession, log: dict, dry: bool):
|
||||
readonly=dry,
|
||||
)
|
||||
mail = session.mail
|
||||
if status != "OK" or not data or not data[0]:
|
||||
raw_header = fetch_body(data)
|
||||
if status != "OK" or raw_header is None:
|
||||
mislukt += 1
|
||||
log_failure(log, "stap3", bron, uid, None, "fetch_header", status, data)
|
||||
continue
|
||||
|
||||
msg = email.message_from_bytes(data[0][1])
|
||||
msg = email.message_from_bytes(raw_header)
|
||||
date_str = msg.get("Date", "")
|
||||
doel = PREFIX + quarter_folder(date_str)
|
||||
gepland += 1
|
||||
|
||||
Reference in New Issue
Block a user