sidea/b interface
This commit is contained in:
parent
7ab727bf7c
commit
3cc3e8c9d8
10
README.md
10
README.md
@ -10,6 +10,13 @@ pip install temp-index
|
|||||||
|
|
||||||
## use it
|
## use it
|
||||||
|
|
||||||
|
`make_cards` accepts the following arguments:
|
||||||
|
|
||||||
|
- **filepath**: where the pdf will go
|
||||||
|
- **metadata db**: where your metadata db is
|
||||||
|
- **side_a**: the fields for the side a of the card
|
||||||
|
- **side_b**: the fields for the side b of the card
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from temp_index import make_cards
|
from temp_index import make_cards
|
||||||
|
|
||||||
@ -17,6 +24,7 @@ from temp_index import make_cards
|
|||||||
make_cards(
|
make_cards(
|
||||||
"/home/me/catalogue.pdf",
|
"/home/me/catalogue.pdf",
|
||||||
"sqlite:///metadata.db",
|
"sqlite:///metadata.db",
|
||||||
["title", "timestamp", "comments", "authors", "tags"]
|
["title", "timestamp", "comments", "authors"],
|
||||||
|
["tags"],
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
@ -14,64 +14,71 @@ from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer
|
|||||||
CWD = Path().resolve()
|
CWD = Path().resolve()
|
||||||
|
|
||||||
|
|
||||||
def make_cards(filepath, db_path, fields):
|
def make_cards(filepath, db_path, side_a, side_b):
|
||||||
"""The main entrypoint for card generation."""
|
"""The main entrypoint for card generation."""
|
||||||
filename = os.path.basename(filepath)
|
filename = os.path.basename(filepath)
|
||||||
doc = create_doc(filename)
|
doc = create_doc(filename)
|
||||||
content = get_fields(db_path, fields)
|
content = get_fields(db_path, side_a, side_b)
|
||||||
doc.build(content)
|
doc.build(content)
|
||||||
shutil.move(os.path.join(CWD, filename), filepath)
|
shutil.move(os.path.join(CWD, filename), filepath)
|
||||||
|
|
||||||
|
|
||||||
def get_fields(db_path, fields):
|
def select_fields(fields, content, styles, book):
|
||||||
|
if "title" in fields:
|
||||||
|
tag = "<font size=12>{}</font>".format(book.title)
|
||||||
|
ptitle = Paragraph(tag, styles["Italic"])
|
||||||
|
content.append(ptitle)
|
||||||
|
content.append(Spacer(1, 12))
|
||||||
|
|
||||||
|
if "timestamp" in fields:
|
||||||
|
tag = "<font size=10>Timestamp: {}</font>".format(book.timestamp)
|
||||||
|
ptime = Paragraph(tag, styles["Normal"])
|
||||||
|
content.append(ptime)
|
||||||
|
content.append(Spacer(1, 12))
|
||||||
|
|
||||||
|
if "comments" in fields:
|
||||||
|
comments = ", ".join(
|
||||||
|
[
|
||||||
|
shorten(comment.text, width=50, placeholder="...")
|
||||||
|
for comment in book.comments
|
||||||
|
]
|
||||||
|
)
|
||||||
|
tag = "<font size=10>{}</font>".format(comments)
|
||||||
|
pcomments = Paragraph(tag)
|
||||||
|
content.append(pcomments)
|
||||||
|
|
||||||
|
if "authors" in fields:
|
||||||
|
format_string = "<font size=12>{}</font>"
|
||||||
|
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(Spacer(6, 12))
|
||||||
|
|
||||||
|
if "tags" in fields:
|
||||||
|
format_string = "<font size=10>{}</font>"
|
||||||
|
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(Spacer(6, 12))
|
||||||
|
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def get_fields(db_path, side_a, side_b):
|
||||||
"""Retrieve fields from the metadata."""
|
"""Retrieve fields from the metadata."""
|
||||||
content = []
|
content = []
|
||||||
styles = getSampleStyleSheet()
|
styles = getSampleStyleSheet()
|
||||||
session = init_session(db_path)
|
session = init_session(db_path)
|
||||||
|
|
||||||
for book in session.query(Book).all():
|
for book in session.query(Book).all():
|
||||||
if "title" in fields:
|
content = select_fields(side_a, content, styles, book)
|
||||||
tag = "<font size=12>{}</font>".format(book.title)
|
content.append(PageBreak())
|
||||||
ptitle = Paragraph(tag, styles["Italic"])
|
content = select_fields(side_b, content, styles, book)
|
||||||
content.append(ptitle)
|
content.append(PageBreak())
|
||||||
content.append(Spacer(1, 12))
|
|
||||||
|
|
||||||
if "timestamp" in fields:
|
|
||||||
tag = "<font size=10>Timestamp: {}</font>".format(book.timestamp)
|
|
||||||
ptime = Paragraph(tag, styles["Normal"])
|
|
||||||
content.append(ptime)
|
|
||||||
content.append(Spacer(1, 12))
|
|
||||||
|
|
||||||
if "comments" in fields:
|
|
||||||
comments = ", ".join(
|
|
||||||
[
|
|
||||||
shorten(comment.text, width=50, placeholder="...")
|
|
||||||
for comment in book.comments
|
|
||||||
]
|
|
||||||
)
|
|
||||||
tag = "<font size=10>{}</font>".format(comments)
|
|
||||||
pcomments = Paragraph(tag)
|
|
||||||
content.append(pcomments)
|
|
||||||
|
|
||||||
if "authors" in fields:
|
|
||||||
format_string = "<font size=12>{}</font>"
|
|
||||||
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 = "<font size=10>{}</font>"
|
|
||||||
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
|
return content
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user