MailSweep Python Domain API Reference
MailSweep provides side-effect free domain modules in Python without external package dependencies.
1. Quick Start: One-Shot Pipeline Execution
The MailboxPipeline.connect() context manager coordinates IMAP connection lifecycles, scanning, classification, safety auditing, backup creation, and non-destructive trashing:
from mailsweep import MailboxPipeline, load_env
env_vars = load_env()
with MailboxPipeline.connect(env_vars) as pipeline:
# Run full sweep (Dry-Run by default)
summary = pipeline.sweep(
folders=["INBOX"],
action="trash",
backup=True,
dry_run=True
)
print(summary.render_text())
# Assert zero critical personal or financial messages were affected
assert summary.is_clean, "Safety audit failed!"
2. Step-by-Step Domain Lifecycle
You can execute each phase of the domain lifecycle independently on the pipeline:
from mailsweep import (
MailboxPipeline,
UnsubscribeHub,
SafetyAuditor,
load_env,
)
env_vars = load_env()
with MailboxPipeline.connect(env_vars) as pipeline:
# 1. Scan mailbox headers (in-memory, returns ScanResult)
scan_res = pipeline.scan(folders=["INBOX"])
# 2. Evaluate 13-language safety tiers and build a typed CleanupPlan
plan = pipeline.plan(scan_res)
# 3. Audit safety before execution (returns SafetyAuditReport)
audit_report = pipeline.audit(plan)
assert audit_report.is_clean, "Critical messages detected in cleanup targets!"
# 4. Create an offline SHA-256 verified backup archive (returns BackupArchive)
archive = pipeline.backup(
plan=plan,
output_dir="backups",
user_identifier="myuser"
)
# 5. Non-destructively move targeted clutter to Trash (returns CleanupReport)
cleanup_report = pipeline.clean(
plan=plan,
action="trash",
dry_run=False
)
# 6. Restore from an offline backup archive with SHA-256 integrity verification
restored_count = pipeline.restore(
archive=archive,
query="important"
)
# 7. Aggregate brand subscriptions and verify unsubscription URLs
hub = UnsubscribeHub()
brands = hub.aggregate([scan_res])
verified_brands = hub.verify_links(brands)
html_dashboard = hub.render_dashboard(verified_brands)
3. Core Domain Models & Modules
MailboxPipeline (mailsweep.pipeline)
The top-level coordinator managing end-to-end mailbox operations.
MailboxPipeline.connect(config, transport=None, rules=None, user_domains=(), property_keywords=())\rightarrowContext manager yielding connectedMailboxPipeline.MailboxPipeline.offline(config=None, rules=None, user_domains=(), property_keywords=())\rightarrowOfflineMailboxPipelineinstance.pipeline.sweep(folders, action, dest_folder, trash_folder, backup_dir, backup, create_zip, dry_run, save_snapshots, cache_dir, user_identifier, fresh, progress_callback)\rightarrowSweepSummaryReportpipeline.scan(folders, save_path, cache_dir, user_identifier, fresh, progress_callback)\rightarrowScanResultpipeline.plan(scan_data, save_path)\rightarrowCleanupPlanpipeline.audit(target)\rightarrowSafetyAuditReportpipeline.backup(plan, output_dir, user_identifier, create_zip, fresh, progress_callback)\rightarrowBackupArchivepipeline.clean(plan, action, dest_folder, trash_folder, dry_run, progress_callback)\rightarrowCleanupReportpipeline.restore(archive, query, sender, subject, dry_run, progress_callback)\rightarrowintpipeline.verify_backup(archive, scan_result=None, folders=None, trash_folder=None)\rightarrowBackupVerificationReportpipeline.unsubscribe(sources=None, summary_path=..., backups_dir=..., output_path=..., skip_check=False, default_user_email="")\rightarrowtuple[BrandSubscription, ...]
EmailHeader (mailsweep.gateway)
Immutable domain model encapsulating parsed message headers, attachments, and Gmail smart labels.
header.dedup_key\rightarrowstr(Canonical deduplication key across virtual folders)header.normalized_labels\rightarrowtuple[str, ...](Lowercase Gmail labels)header.has_label(label)\rightarrowboolheader.is_promotional\rightarrowboolheader.is_social\rightarrowboolheader.with_merged_labels(new_labels)\rightarrowEmailHeader(Returns new instance with merged labels)
ScanResult (mailsweep.gateway)
Immutable domain model encapsulating mailbox header scan data and UIDVALIDITY snapshots.
ScanResult.load(source)\rightarrowScanResultscan_result.save(output_path)\rightarrowNonescan_result.to_dict()\rightarrowdict
ClassificationEngine (mailsweep.classifier)
The heuristic evaluation engine applying 13-language safety tiers.
ClassificationEngine(rules=None, user_domains=(), property_keywords=(), extra_org_keywords=())ClassificationEngine.from_env_and_rules(env_vars=None, rules_path="rules.json")\rightarrowClassificationEngineengine.is_critical_subject(subject)\rightarrowboolengine.audit_item_safety(email_item)\rightarrow(finding_type: AuditFindingType, ClassificationResult)engine.audit_safety(items)\rightarrowlist[tuple[AuditFindingType, ClassificationResult]]engine.classify(email_item)\rightarrowClassificationResultengine.create_plan(scan_data)\rightarrowCleanupPlan
SafetyAuditor (mailsweep.audit)
The classification safety verification module.
SafetyAuditor(rules=None)auditor.audit_scan(scan_res)\rightarrowSafetyAuditReportauditor.audit_plan(plan)\rightarrowSafetyAuditReportauditor.audit_backup(archive_path)\rightarrowSafetyAuditReport
BackupArchive (mailsweep.archive)
The offline archive creator and verifier.
BackupArchive(path, is_zip=True, manifest=None)BackupArchive.open(path)\rightarrowBackupArchiveBackupArchive.calculate_sha256(path)\rightarrowstrarchive.verify_integrity()\rightarrow(bool, str)archive.iter_messages(query, sender, subject)\rightarrowGenerator ofArchiveMessage
UnsubscribeHub (mailsweep.unsubscribe)
The brand aggregator and unsubscription link health verifier.
UnsubscribeHub(health_checker=None)hub.aggregate(scan_results)\rightarrowlist[BrandSubscription]hub.verify_links(brands)\rightarrowlist[BrandSubscription]hub.render_dashboard(brands)\rightarrowstr(HTML)
RulesRegistry (mailsweep.rules)
The governance and synchronization module for categorization rules.
RulesRegistry(transport=None)registry.load_ruleset(path)\rightarrowRuleSetregistry.validate_rules(ruleset)\rightarrowRulesValidationReportregistry.sync_upstream(source_url)\rightarrowRuleSetregistry.sync_sources(manifest)\rightarrowRuleSet