Fix pylint warnings: logging, globals, exceptions, imports, duplicates
Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/576427d4-4f6a-46d2-b75f-6862ecbcf526 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -236,20 +236,24 @@ class DMARCParser:
|
||||
|
||||
# Log parse results for debugging
|
||||
total_count = report["summary"]["total_count"]
|
||||
logger.info(f"Parsed DMARC report for domain: {report.get('domain')}")
|
||||
logger.info(f"Found {len(records)} record entries with {total_count} total messages")
|
||||
logger.info("Parsed DMARC report for domain: %s", report.get("domain"))
|
||||
logger.info(
|
||||
f"Messages passed: {report['summary']['passed_count']}, "
|
||||
f"failed: {report['summary']['failed_count']}"
|
||||
"Found %s record entries with %s total messages", len(records), total_count
|
||||
)
|
||||
logger.info(
|
||||
"Messages passed: %s, failed: %s",
|
||||
report["summary"]["passed_count"],
|
||||
report["summary"]["failed_count"],
|
||||
)
|
||||
if records:
|
||||
logger.info(
|
||||
f"Sample record - SPF: {records[0].get('spf_result')}, "
|
||||
f"DKIM: {records[0].get('dkim_result')}"
|
||||
"Sample record - SPF: %s, DKIM: %s",
|
||||
records[0].get("spf_result"),
|
||||
records[0].get("dkim_result"),
|
||||
)
|
||||
|
||||
return report
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error parsing DMARC XML: {str(e)}")
|
||||
raise ValueError(f"Error parsing DMARC XML: {str(e)}")
|
||||
logger.error("Error parsing DMARC XML: %s", str(e))
|
||||
raise ValueError(f"Error parsing DMARC XML: {str(e)}") from e
|
||||
|
||||
@@ -18,7 +18,7 @@ class IMAPClient:
|
||||
Client for retrieving DMARC reports from an IMAP mailbox
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
def __init__( # pylint: disable=too-many-positional-arguments,too-many-arguments
|
||||
self,
|
||||
server: str = None,
|
||||
port: int = None,
|
||||
@@ -63,7 +63,7 @@ class IMAPClient:
|
||||
if mailbox_name.startswith(" "):
|
||||
mailbox_name = mailbox_name[1:]
|
||||
available_mailboxes.append(mailbox_name)
|
||||
except Exception:
|
||||
except Exception: # pylint: disable=broad-exception-caught
|
||||
# Silently skip mailboxes that can't be parsed; they are simply
|
||||
# omitted from the returned list so callers should expect it may
|
||||
# be incomplete. Some IMAP servers return non-standard list
|
||||
@@ -130,8 +130,8 @@ class IMAPClient:
|
||||
}
|
||||
|
||||
return True, "Connection successful", stats
|
||||
except Exception as e:
|
||||
logger.error(f"IMAP connection test failed: {str(e)}")
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
logger.error("IMAP connection test failed: %s", str(e))
|
||||
return False, f"Connection failed: {str(e)}", {}
|
||||
|
||||
def _process_single_email(self, mail, email_id: bytes, stats: dict) -> None:
|
||||
@@ -139,7 +139,7 @@ class IMAPClient:
|
||||
try:
|
||||
status, msg_data = mail.fetch(email_id, "(RFC822)")
|
||||
if status != "OK":
|
||||
logger.error(f"Error fetching email ID {email_id}")
|
||||
logger.error("Error fetching email ID %s", email_id)
|
||||
return
|
||||
|
||||
raw_email = msg_data[0][1]
|
||||
@@ -155,7 +155,7 @@ class IMAPClient:
|
||||
mail.store(email_id, "+FLAGS", "\\Deleted")
|
||||
|
||||
stats["processed"] += 1
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
error_msg = f"Error processing email ID {email_id}: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
stats["errors"].append(error_msg)
|
||||
@@ -225,8 +225,8 @@ class IMAPClient:
|
||||
|
||||
return stats
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching DMARC reports: {str(e)}")
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
logger.error("Error fetching DMARC reports: %s", str(e))
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Error connecting to mailbox: {str(e)}",
|
||||
@@ -339,12 +339,12 @@ class IMAPClient:
|
||||
|
||||
# Check content type
|
||||
content_type = part.get_content_type()
|
||||
if (
|
||||
content_type == "application/zip"
|
||||
or content_type == "application/gzip"
|
||||
or content_type == "application/x-gzip"
|
||||
or content_type == "application/xml"
|
||||
or content_type == "text/xml"
|
||||
if content_type in (
|
||||
"application/zip",
|
||||
"application/gzip",
|
||||
"application/x-gzip",
|
||||
"application/xml",
|
||||
"text/xml",
|
||||
):
|
||||
return True
|
||||
|
||||
@@ -390,8 +390,10 @@ class IMAPClient:
|
||||
self.report_store.add_report(report)
|
||||
|
||||
reports_found += 1
|
||||
logger.info(f"Successfully processed DMARC report: {filename}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing attachment {filename}: {str(e)}")
|
||||
logger.info("Successfully processed DMARC report: %s", filename)
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
logger.error(
|
||||
"Error processing attachment %s: %s", filename, str(e)
|
||||
)
|
||||
|
||||
return reports_found
|
||||
|
||||
@@ -155,7 +155,7 @@ class ReportStore:
|
||||
return sorted_reports[:limit]
|
||||
return sorted_reports
|
||||
|
||||
def get_domain_sources(self, domain: str, days: int = 30) -> List[Dict[str, Any]]:
|
||||
def get_domain_sources(self, domain: str, _days: int = 30) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get sending sources for a domain
|
||||
|
||||
@@ -206,6 +206,6 @@ class ReportStore:
|
||||
self.domain_summary.pop(domain, None)
|
||||
self.domain_sources.pop(domain, None)
|
||||
return True
|
||||
except Exception:
|
||||
except Exception: # pylint: disable=broad-exception-caught
|
||||
# If any exception occurs during deletion, return False
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user