71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from .maildir import parse_maildir
|
|
|
|
|
|
def parse_arguments() -> argparse.Namespace:
|
|
"""Parse command line arguments
|
|
|
|
Returns: Namespace object corresponding to parsed arguments
|
|
|
|
"""
|
|
parser = argparse.ArgumentParser(
|
|
description="Analyze email metadata from a maildir directory"
|
|
)
|
|
parser.add_argument(
|
|
"maildir", type=str, help="Path to the maildir directory to analyze"
|
|
)
|
|
parser.add_argument(
|
|
"--top",
|
|
"-t",
|
|
type=int,
|
|
default=5,
|
|
help="Number of top senders to display (default: 5)",
|
|
)
|
|
parser.add_argument(
|
|
"--verbose", "-v", action="store_true", help="Enable verbose output"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
def cli():
|
|
args = parse_arguments()
|
|
|
|
maildir_path = Path(args.maildir)
|
|
if not maildir_path.exists() or not maildir_path.is_dir():
|
|
print(f"Error: {args.maildir} is not a valid directory", file=sys.stderr)
|
|
return 1
|
|
|
|
if args.verbose:
|
|
print(f"Analyzing emails in {maildir_path}...")
|
|
|
|
maildir = parse_maildir(maildir_path)
|
|
if args.verbose:
|
|
print(f"Found {len(maildir._df)} emails")
|
|
|
|
top_senders = maildir.get_top_n_senders(args.top)
|
|
|
|
if not top_senders:
|
|
print("No senders found in the maildir", file=sys.stderr)
|
|
return 0
|
|
|
|
result = []
|
|
for i, sender in enumerate(top_senders, 1):
|
|
names_str = ", ".join(sender.names[:5]) # Limit to first 5 names
|
|
if len(sender.names) > 5:
|
|
names_str += f" and {len(sender.names) - 5} more"
|
|
|
|
result.append(f"{i}. {sender.email} - Names used: {names_str}")
|
|
|
|
output = "\n".join(
|
|
[f"Top {len(top_senders)} senders in {maildir_path}:", "=" * 40, *result]
|
|
)
|
|
|
|
print(output)
|
|
|
|
return 0
|