Commit d5b1b5d10b
d5b1b5d10b2d9e9f24ef796d71ae95acb2a58de1
parent: 87148bd456
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-22 19:07 UTC
Add Qlty config; pass a Poem to build_message
Layout: unified · split
.qlty/.gitignore
added
+7
| @@ -0,0 +1,7 @@ |
| 1 | * |
| 2 | !configs |
| 3 | !configs/** |
| 4 | !hooks |
| 5 | !hooks/** |
| 6 | !qlty.toml |
| 7 | !.gitignore |
.qlty/qlty.toml
added
+44
| @@ -0,0 +1,44 @@ |
| 1 | config_version = "0" |
| 2 | |
| 3 | exclude_patterns = [ |
| 4 | "**/vendor/**", |
| 5 | "**/node_modules/**", |
| 6 | "**/testdata/**", |
| 7 | "**/dist/**", |
| 8 | "**/.build/**", |
| 9 | "**/build/**", |
| 10 | "**/*.min.*", |
| 11 | "**/generated/**", |
| 12 | ".claude/**", |
| 13 | "**/Pods/**", |
| 14 | ] |
| 15 | |
| 16 | test_patterns = [ |
| 17 | "**/test/**", |
| 18 | "**/tests/**", |
| 19 | "**/Tests/**", |
| 20 | "**/*_test.*", |
| 21 | "**/test_*.*", |
| 22 | "**/*.test.*", |
| 23 | "**/*Tests.swift", |
| 24 | "e2e/**", |
| 25 | ] |
| 26 | |
| 27 | [[source]] |
| 28 | name = "default" |
| 29 | default = true |
| 30 | |
| 31 | [[plugin]] |
| 32 | name = "osv-scanner" |
| 33 | |
| 34 | [[plugin]] |
| 35 | name = "trufflehog" |
| 36 | |
| 37 | # Early returns are guard clauses, which keep the happy path unindented. |
| 38 | [smells.return_statements] |
| 39 | enabled = false |
| 40 | |
| 41 | # Total file complexity is the sum of its functions, which function_complexity |
| 42 | # already checks. |
| 43 | [smells.file_complexity] |
| 44 | enabled = false |
main.py
+14 −9
| @@ -1,6 +1,7 @@ |
| 1 | 1 | # Fetch a random poem from PoetryDB and mail it. |
| 2 | 2 | import smtplib |
| 3 | 3 | from email.mime.text import MIMEText |
| 4 | from typing import NamedTuple |
| 4 | 5 | |
| 5 | 6 | import requests |
| 6 | 7 | |
| @@ -12,30 +13,34 @@ SENDER_EMAIL = "" |
| 12 | 13 | RECIPIENT_EMAILS = "" |
| 13 | 14 | |
| 14 | 15 | |
| 16 | class Poem(NamedTuple): |
| 17 | title: str |
| 18 | author: str |
| 19 | line_count: str |
| 20 | lines: str |
| 21 | |
| 22 | |
| 15 | 23 | def fetch_poem(url=POETRY_URL): |
| 16 | | """Return (title, author, line_count, body) for a random poem.""" |
| 24 | """Return a random poem.""" |
| 17 | 25 | json_data = requests.get(url, timeout=30).json() |
| 18 | 26 | poem = json_data[0] |
| 19 | 27 | lines = "" |
| 20 | 28 | for line in poem["lines"]: |
| 21 | 29 | lines = lines + line + "\n" |
| 22 | | return poem["title"], poem["author"], poem["linecount"], lines |
| 30 | return Poem(poem["title"], poem["author"], poem["linecount"], lines) |
| 23 | 31 | |
| 24 | 32 | |
| 25 | | def build_message(title, author, line_count, lines, sender, recipient): |
| 33 | def build_message(poem, sender, recipient): |
| 26 | 34 | """Return a plaintext MIMEText message for one poem.""" |
| 27 | | msg = MIMEText(title + "\n" + author + "\n\n" + lines) |
| 28 | | msg["Subject"] = "Your Daily Poem (" + line_count + " lines)" |
| 35 | msg = MIMEText(poem.title + "\n" + poem.author + "\n\n" + poem.lines) |
| 36 | msg["Subject"] = "Your Daily Poem (" + poem.line_count + " lines)" |
| 29 | 37 | msg["From"] = sender |
| 30 | 38 | msg["To"] = recipient |
| 31 | 39 | return msg |
| 32 | 40 | |
| 33 | 41 | |
| 34 | 42 | def main(): |
| 35 | | title, author, line_count, lines = fetch_poem() |
| 36 | | msg = build_message( |
| 37 | | title, author, line_count, lines, SENDER_EMAIL, RECIPIENT_EMAILS |
| 38 | | ) |
| 43 | msg = build_message(fetch_poem(), SENDER_EMAIL, RECIPIENT_EMAILS) |
| 39 | 44 | |
| 40 | 45 | # Send via the local SMTP server, without the envelope header. |
| 41 | 46 | s = smtplib.SMTP(SMTP_SERVER) |
tests/test_main.py
+3 −2
| @@ -1,7 +1,7 @@ |
| 1 | 1 | import unittest |
| 2 | 2 | from unittest.mock import patch |
| 3 | 3 | |
| 4 | | from main import build_message, fetch_poem |
| 4 | from main import Poem, build_message, fetch_poem |
| 5 | 5 | |
| 6 | 6 | |
| 7 | 7 | class FakeResponse: |
| @@ -14,7 +14,8 @@ class FakeResponse: |
| 14 | 14 | |
| 15 | 15 | class TestBuildMessage(unittest.TestCase): |
| 16 | 16 | def test_headers_and_body(self): |
| 17 | | msg = build_message("A Title", "An Author", "4", "one\ntwo\n", "from@x", "to@y") |
| 17 | poem = Poem("A Title", "An Author", "4", "one\ntwo\n") |
| 18 | msg = build_message(poem, "from@x", "to@y") |
| 18 | 19 | |
| 19 | 20 | self.assertEqual(msg["Subject"], "Your Daily Poem (4 lines)") |
| 20 | 21 | self.assertEqual(msg["From"], "from@x") |