Skip to content

Commit

Permalink
Merge pull request #91 from smkent/sendmail-arguments
Browse files Browse the repository at this point in the history
Add `--sendmail` argument for custom sendmail path, arguments
  • Loading branch information
smkent committed Jul 11, 2023
2 parents 79baf1a + 28d7f41 commit 5c58b84
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
13 changes: 13 additions & 0 deletions safeway_coupons/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import shlex
import sys
import traceback
from http.client import HTTPConnection
Expand Down Expand Up @@ -53,6 +54,17 @@ def _parse_args() -> argparse.Namespace:
metavar="number",
help="Maximum number of coupons to clip (default: all)",
)
arg_parser.add_argument(
"--sendmail",
metavar="path/to/sendmail",
dest="sendmail",
type=shlex.split,
default="/usr/sbin/sendmail",
help=(
"Path to sendmail and any additional arguments to use when "
"sending email (default: %(default)s)"
),
)
arg_parser.add_argument(
"-n",
"--no-email",
Expand Down Expand Up @@ -99,6 +111,7 @@ def main() -> None:
HTTPConnection.debuglevel = 1
sc = SafewayCoupons(
send_email=args.send_email,
sendmail=args.sendmail,
debug_level=args.debug_level,
debug_dir=Path(args.debug_dir) if args.debug_dir else None,
sleep_level=args.sleep_level,
Expand Down
11 changes: 8 additions & 3 deletions safeway_coupons/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


def _send_email(
sendmail: List[str],
account: Account,
subject: str,
mail_message: List[str],
Expand Down Expand Up @@ -46,13 +47,13 @@ def _send_email(
subtype=sub,
)
p = subprocess.Popen(
["/usr/sbin/sendmail", "-f", account.mail_to, "-t"],
stdin=subprocess.PIPE,
sendmail + ["-f", account.mail_to, "-t"], stdin=subprocess.PIPE
)
p.communicate(bytes(msg.as_string(), "UTF-8"))


def email_clip_results(
sendmail: List[str],
account: Account,
offers: List[Offer],
error: Optional[Error],
Expand All @@ -72,10 +73,13 @@ def email_clip_results(
mail_message.append(
f" {offer_type.name}: {len(offers_this_type)} coupons"
)
_send_email(account, mail_subject, mail_message, debug_level, send_email)
_send_email(
sendmail, account, mail_subject, mail_message, debug_level, send_email
)


def email_error(
sendmail: List[str],
account: Account,
error: Error,
debug_level: int,
Expand All @@ -91,6 +95,7 @@ def email_error(
for offer in error.clipped_offers:
mail_message += str(offer)
_send_email(
sendmail,
account,
mail_subject,
mail_message,
Expand Down
4 changes: 4 additions & 0 deletions safeway_coupons/safeway.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SafewayCoupons:
def __init__(
self,
send_email: bool = True,
sendmail: Optional[List[str]] = None,
debug_level: int = 0,
debug_dir: Optional[Path] = None,
sleep_level: int = 0,
Expand All @@ -23,6 +24,7 @@ def __init__(
max_clip_errors: int = CLIP_ERROR_MAX,
) -> None:
self.send_email = send_email
self.sendmail = sendmail or ["/usr/sbin/sendmail"]
self.debug_level = debug_level
self.debug_dir = debug_dir
self.sleep_level = sleep_level
Expand Down Expand Up @@ -82,6 +84,7 @@ def clip_for_account(self, account: Account) -> None:

print(f"Clipped {len(clipped_offers)} coupons")
email_clip_results(
self.sendmail,
account,
clipped_offers,
error=None,
Expand All @@ -91,6 +94,7 @@ def clip_for_account(self, account: Account) -> None:
)
except Error as e:
email_error(
self.sendmail,
account,
error=e,
debug_level=self.debug_level,
Expand Down
40 changes: 40 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_app_error(
[],
dict(
send_email=True,
sendmail=["/usr/sbin/sendmail"],
debug_level=0,
debug_dir=Path("."),
sleep_level=0,
Expand All @@ -68,17 +69,53 @@ def test_app_error(
["-d", "-d", "-SS"],
dict(
send_email=True,
sendmail=["/usr/sbin/sendmail"],
debug_level=2,
debug_dir=Path("."),
sleep_level=2,
dry_run=False,
max_clip_count=0,
),
),
(
["--sendmail", "/my/special/sendmail"],
dict(
send_email=True,
sendmail=["/my/special/sendmail"],
debug_level=0,
debug_dir=Path("."),
sleep_level=0,
dry_run=False,
max_clip_count=0,
),
),
(
[
"--sendmail",
(
"/my/special/sendmail "
'--do-the-thing "with an argument value with spaces"'
),
],
dict(
send_email=True,
sendmail=[
"/my/special/sendmail",
"--do-the-thing",
"with an argument value with spaces",
],
debug_level=0,
debug_dir=Path("."),
sleep_level=0,
dry_run=False,
max_clip_count=0,
),
),
(
["-n"],
dict(
send_email=False,
sendmail=["/usr/sbin/sendmail"],
debug_level=0,
debug_dir=Path("."),
sleep_level=0,
Expand All @@ -90,6 +127,7 @@ def test_app_error(
["-p", "--max-clip", "42"],
dict(
send_email=True,
sendmail=["/usr/sbin/sendmail"],
debug_level=0,
debug_dir=Path("."),
sleep_level=0,
Expand All @@ -101,6 +139,8 @@ def test_app_error(
ids=[
"Default values",
"Debug and sleep levels 2",
"Custom sendmail",
"Custom sendmail with arguments",
"No email",
"Dry run and max clip count",
],
Expand Down

0 comments on commit 5c58b84

Please sign in to comment.