From 2ce3de6b58151cf253e3072407444326ed97c91c Mon Sep 17 00:00:00 2001 From: Timothy Allen Date: Fri, 2 Oct 2026 23:54:02 +0200 Subject: [PATCH] Remove unused store and CWA client code The two functions that add chapters share one insert. Nothing used Store.current_revisions and CwaClient.check. --- bindery/cwa.py | 5 ----- bindery/store.py | 55 +++++++++++++++++---------------------------- tests/test_store.py | 6 +++-- 3 files changed, 24 insertions(+), 42 deletions(-) diff --git a/bindery/cwa.py b/bindery/cwa.py index 3c796bc..6a684d7 100644 --- a/bindery/cwa.py +++ b/bindery/cwa.py @@ -105,11 +105,6 @@ class CwaClient: raise CwaError(f"CWA answered {response.status_code} for {url}") return response - def check(self) -> str: - """Log in to the OPDS feed. Returns the catalog title.""" - root = ElementTree.fromstring(self._get("opds").content) - return (root.findtext(f"{ATOM}title") or "CWA").strip() - def login_ok(self, username: str, password: str) -> bool: """True if CWA accepts this user name and password.""" try: diff --git a/bindery/store.py b/bindery/store.py index 1d39d07..af63993 100644 --- a/bindery/store.py +++ b/bindery/store.py @@ -242,18 +242,6 @@ class Store: ).fetchall() return [dict(r) for r in rows] - def current_revisions(self, story_id: int) -> dict[int, dict[str, Any]]: - """The rev, text_hash and source of the current revision of each chapter, by chapter id.""" - with self._connect() as conn: - rows = conn.execute( - """ - SELECT c.id, r.rev, r.text_hash, r.source FROM chapters c - JOIN revisions r ON r.chapter_id = c.id AND r.rev = c.current_rev - WHERE c.story_id = ?""", - (story_id,), - ).fetchall() - return {r["id"]: {"rev": r["rev"], "text_hash": r["text_hash"], "source": r["source"]} for r in rows} - def replace_revision(self, chapter_id: int, rev: int, html: str, text_hash: str) -> None: with self._connect() as conn: conn.execute( @@ -266,21 +254,29 @@ class Store: row = conn.execute("SELECT * FROM chapters WHERE id = ?", (chapter_id,)).fetchone() return dict(row) if row else None + @staticmethod + def _insert_chapter( + conn: sqlite3.Connection, story_id: int, chapter: dict[str, str], source: str, position: int + ) -> int: + """Store a chapter with its first revision. The chapter needs url, title, html and text_hash.""" + cur = conn.execute( + "INSERT INTO chapters (story_id, position, url, title, added_at) VALUES (?, ?, ?, ?, ?)", + (story_id, position, chapter["url"], chapter["title"], now()), + ) + chapter_id = int(cur.lastrowid) + conn.execute( + "INSERT INTO revisions (chapter_id, rev, html, text_hash, source, fetched_at) VALUES (?, 1, ?, ?, ?, ?)", + (chapter_id, chapter["html"], chapter["text_hash"], source, now()), + ) + return chapter_id + def add_chapter( self, story_id: int, url: str, title: str, html: str, text_hash: str, source: str, position: int ) -> int: + """Add one chapter at a position. The jobs use add_chapters, which also places the chapters.""" + chapter = {"url": url, "title": title, "html": html, "text_hash": text_hash} with self._connect() as conn: - cur = conn.execute( - "INSERT INTO chapters (story_id, position, url, title, added_at) VALUES (?, ?, ?, ?, ?)", - (story_id, position, url, title, now()), - ) - chapter_id = int(cur.lastrowid) - conn.execute( - "INSERT INTO revisions (chapter_id, rev, html, text_hash, source, fetched_at) " - "VALUES (?, 1, ?, ?, ?, ?)", - (chapter_id, html, text_hash, source, now()), - ) - return chapter_id + return self._insert_chapter(conn, story_id, chapter, source, position) def add_chapters( self, @@ -303,18 +299,7 @@ class Store: if callable(order): stored = conn.execute("SELECT url FROM chapters WHERE story_id = ? ORDER BY position, id", (story_id,)) order = order([r["url"] for r in stored]) - ids = [] - for c in chapters: - cur = conn.execute( - "INSERT INTO chapters (story_id, position, url, title, added_at) VALUES (?, 0, ?, ?, ?)", - (story_id, c["url"], c["title"], now()), - ) - ids.append(int(cur.lastrowid)) - conn.execute( - "INSERT INTO revisions (chapter_id, rev, html, text_hash, source, fetched_at) " - "VALUES (?, 1, ?, ?, ?, ?)", - (ids[-1], c["html"], c["text_hash"], source, now()), - ) + ids = [self._insert_chapter(conn, story_id, c, source, 0) for c in chapters] rows = conn.execute( "SELECT id, url FROM chapters WHERE story_id = ? ORDER BY position, id", (story_id,) ).fetchall() diff --git a/tests/test_store.py b/tests/test_store.py index 41dba87..cf76179 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -10,9 +10,11 @@ def test_chapters_revisions_and_rules(tmp_path): assert [c["url"] for c in store.chapters_for_build(sid)] == ["u2", "u1"] assert store.add_revision(c1, "

1b

", "h1b", "recheck") == 2 - assert store.current_revisions(sid)[c1] == {"rev": 2, "text_hash": "h1b", "source": "recheck"} + newest = store.list_revisions(c1)[-1] + assert (newest["rev"], newest["text_hash"], newest["source"]) == (2, "h1b", "recheck") + assert store.get_chapter(c1)["current_rev"] == 2 store.replace_revision(c1, 2, "

1c

", "h1c") - assert store.current_revisions(sid)[c1]["text_hash"] == "h1c" + assert store.list_revisions(c1)[-1]["text_hash"] == "h1c" assert store.revision_html(c1, 1) == "

1

" store.update_chapter(c1, current_rev=1, included=0) assert [c["url"] for c in store.chapters_for_build(sid)] == ["u2"]