From 5d2ba100cf97727edfb46eab75943ac4913b1874 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Mon, 29 Nov 2021 11:57:13 +0100 Subject: [PATCH] forming up the interface --- README.md | 10 +++++++ temp_index.py | 80 +++++++++++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index e69de29..85ef415 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,10 @@ +# temp-index + +## hacking + +```python +from temp_index import make_cards + + +make_cards("mycards.pdf", "sqlite:///metadata.db", ["title"]) +``` diff --git a/temp_index.py b/temp_index.py index 387a7f5..e19a8b1 100644 --- a/temp_index.py +++ b/temp_index.py @@ -7,54 +7,58 @@ from reportlab.pdfgen import canvas from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer -def make_cards(): +def make_cards(filename, db_path, fields): """The main entrypoint for card generation.""" - # TODO: thread arguments into this logic - doc = create_doc("text.pdf") - content = get_fields() + doc = create_doc(filename) + content = get_fields(db_path, fields) doc.build(content) -def get_fields(): +def get_fields(db_path, fields): """Retrieve fields from the metadata.""" content = [] styles = getSampleStyleSheet() - session = init_session("sqlite:///metadata.db") + session = init_session(db_path) for book in session.query(Book).all(): for comment in session.query(Comment).all(): - ptitle = Paragraph( - "{}".format(book.title), styles["Italic"] - ) - ptime = Paragraph( - "Uploaded: {}".format(book.timestamp), - styles["Normal"], - ) - pcomments = Paragraph("{}".format(comment.text)) - - format_string = "{}" - all_authors = [author.name for author in book.authors] - glued_together = format_string.format(", ".join(all_authors)) - - format_string = "{}" - all_tags = [tag.name for tag in book.tags] - tags_glued_together = format_string.format(", ".join(all_tags)) - - content.append(ptitle) - content.append(Spacer(1, 12)) - content.append(ptime) - content.append(Spacer(1, 12)) - content.append(pcomments) - - p = Paragraph(glued_together, styles["Normal"]) - content.append(p) - content.append(PageBreak()) - content.append(Spacer(6, 12)) - - p = Paragraph(tags_glued_together, styles["Normal"]) - content.append(p) - content.append(PageBreak()) - content.append(Spacer(6, 12)) + + if "title" in fields: + tag = "{}".format(book.title) + ptitle = Paragraph(tag, styles["Italic"]) + content.append(ptitle) + content.append(Spacer(1, 12)) + + if "uploaded_at" in fields: + tag = "Uploaded: {}".format(book.timestamp) + ptime = Paragraph(tag, styles["Normal"]) + content.append(ptime) + content.append(Spacer(1, 12)) + + if "comments" in fields: + tag = "{}".format(comment.text) + pcomments = Paragraph(tag) + content.append(pcomments) + + if "authors" in fields: + format_string = "{}" + all_authors = [author.name for author in book.authors] + glued_together = format_string.format(", ".join(all_authors)) + + p = Paragraph(glued_together, styles["Normal"]) + content.append(p) + content.append(PageBreak()) + content.append(Spacer(6, 12)) + + if "tags" in fields: + format_string = "{}" + all_tags = [tag.name for tag in book.tags] + tags_glued_together = format_string.format(", ".join(all_tags)) + + p = Paragraph(tags_glued_together, styles["Normal"]) + content.append(p) + content.append(PageBreak()) + content.append(Spacer(6, 12)) return content