diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2024-06-02 21:56:48 +0200 |
---|---|---|
committer | Bryan Gardiner <bog@khumba.net> | 2024-07-19 20:30:21 -0700 |
commit | 8d49b3f8c81fa2a960f0121f33dc6ef61e55f2e0 (patch) | |
tree | 367b1ebad78d81d761633d17397bf83fea380881 | |
parent | bd29525b57a1105bc5fb2f0da5bfc61001aef6b9 (diff) | |
download | lazygl2srht-8d49b3f8c81fa2a960f0121f33dc6ef61e55f2e0.tar.gz |
refactor: move processing of smtp args to special function
-rwxr-xr-x | import_issues.py | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/import_issues.py b/import_issues.py index d3c154b..c0910ae 100755 --- a/import_issues.py +++ b/import_issues.py @@ -930,6 +930,7 @@ def parse_args(args_in: list[str]) -> dict[str, Any]: parser.add_argument( "--smtp-port", default=None, + type=int, help="SMTP port to use.", ) @@ -1027,6 +1028,41 @@ def parse_args(args_in: list[str]) -> dict[str, Any]: return vars(parser.parse_args(args_in)) +def process_smtp(args: dict[str, Any]) -> Optional[smtplib.SMTP]: + if args["mode"] != "send": + return None + else: + smtp_ssl = args["smtp_ssl"] + smtp_starttls = args["smtp_starttls"] + smtp_host = args["smtp_host"] or os.environ.get("SMTP_HOST", "localhost") + smtp_port = int( + args["smtp_port"] or os.environ.get("SMTP_PORT", 465 if smtp_ssl else 25) + ) + smtp_user = args["smtp_user"] or os.environ.get("SMTP_USER", None) + smtp_password = args["smtp_password"] or os.environ.get("SMTP_PASSWORD", None) + + assert smtp_user, "No SMTP user given." + assert smtp_password, "No SMTP password given." + + log.info("Connecting to %s:%d, user %r.", smtp_host, smtp_port, smtp_user) + + if smtp_ssl: + smtp: smtplib.SMTP = smtplib.SMTP_SSL(host=smtp_host, port=smtp_port) + else: + smtp = smtplib.SMTP(host=smtp_host, port=smtp_port) + + # If SMTP isn't working: + # smtp.set_debuglevel(2) + + if smtp_starttls: + smtp.starttls() + + if smtp_user: + smtp.login(smtp_user, smtp_password) + + return smtp + + def main(): args = parse_args(sys.argv[1:]) @@ -1079,36 +1115,7 @@ def main(): else: delay = float(delay) - if mode != "send": - smtp = None - else: - smtp_ssl = args["smtp_ssl"] - smtp_starttls = args["smtp_starttls"] - smtp_host = args["smtp_host"] or os.environ.get("SMTP_HOST", "localhost") - smtp_port = args["smtp_port"] or os.environ.get( - "SMTP_PORT", 465 if smtp_ssl else 25 - ) - smtp_user = args["smtp_user"] or os.environ.get("SMTP_USER", None) - smtp_password = args["smtp_password"] or os.environ.get("SMTP_PASSWORD", None) - - assert smtp_user, "No SMTP user given." - assert smtp_password, "No SMTP password given." - - log.info("Connecting to %s:%d, user %r.", smtp_host, smtp_port, smtp_user) - - if smtp_ssl: - smtp = smtplib.SMTP_SSL(host=smtp_host, port=smtp_port) - else: - smtp = smtplib.SMTP(host=smtp_host, port=smtp_port) - - # If SMTP isn't working: - # smtp.set_debuglevel(2) - - if smtp_starttls: - smtp.starttls() - - if smtp_user: - smtp.login(smtp_user, smtp_password) + smtp = process_smtp(args) try: run( |