Remove unused store and CWA client code
The two functions that add chapters share one insert. Nothing used Store.current_revisions and CwaClient.check.
This commit is contained in:
@@ -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:
|
||||
|
||||
+16
-31
@@ -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,22 +254,30 @@ class Store:
|
||||
row = conn.execute("SELECT * FROM chapters WHERE id = ?", (chapter_id,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
def add_chapter(
|
||||
self, story_id: int, url: str, title: str, html: str, text_hash: str, source: str, position: int
|
||||
@staticmethod
|
||||
def _insert_chapter(
|
||||
conn: sqlite3.Connection, story_id: int, chapter: dict[str, str], source: str, position: int
|
||||
) -> int:
|
||||
with self._connect() as conn:
|
||||
"""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, url, title, now()),
|
||||
(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, html, text_hash, source, now()),
|
||||
"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:
|
||||
return self._insert_chapter(conn, story_id, chapter, source, position)
|
||||
|
||||
def add_chapters(
|
||||
self,
|
||||
story_id: int,
|
||||
@@ -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()
|
||||
|
||||
+4
-2
@@ -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, "<p>1b</p>", "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, "<p>1c</p>", "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) == "<p>1</p>"
|
||||
store.update_chapter(c1, current_rev=1, included=0)
|
||||
assert [c["url"] for c in store.chapters_for_build(sid)] == ["u2"]
|
||||
|
||||
Reference in New Issue
Block a user