|
@ -14,22 +14,16 @@ 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): |
|
|
"""Retrieve fields from the metadata.""" |
|
|
|
|
|
content = [] |
|
|
|
|
|
styles = getSampleStyleSheet() |
|
|
|
|
|
session = init_session(db_path) |
|
|
|
|
|
|
|
|
|
|
|
for book in session.query(Book).all(): |
|
|
|
|
|
if "title" in fields: |
|
|
if "title" in fields: |
|
|
tag = "<font size=12>{}</font>".format(book.title) |
|
|
tag = "<font size=12>{}</font>".format(book.title) |
|
|
ptitle = Paragraph(tag, styles["Italic"]) |
|
|
ptitle = Paragraph(tag, styles["Italic"]) |
|
@ -60,7 +54,6 @@ def get_fields(db_path, fields): |
|
|
|
|
|
|
|
|
p = Paragraph(glued_together, styles["Normal"]) |
|
|
p = Paragraph(glued_together, styles["Normal"]) |
|
|
content.append(p) |
|
|
content.append(p) |
|
|
content.append(PageBreak()) |
|
|
|
|
|
content.append(Spacer(6, 12)) |
|
|
content.append(Spacer(6, 12)) |
|
|
|
|
|
|
|
|
if "tags" in fields: |
|
|
if "tags" in fields: |
|
@ -70,12 +63,26 @@ def get_fields(db_path, fields): |
|
|
|
|
|
|
|
|
p = Paragraph(tags_glued_together, styles["Normal"]) |
|
|
p = Paragraph(tags_glued_together, styles["Normal"]) |
|
|
content.append(p) |
|
|
content.append(p) |
|
|
content.append(PageBreak()) |
|
|
|
|
|
content.append(Spacer(6, 12)) |
|
|
content.append(Spacer(6, 12)) |
|
|
|
|
|
|
|
|
return content |
|
|
return content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fields(db_path, side_a, side_b): |
|
|
|
|
|
"""Retrieve fields from the metadata.""" |
|
|
|
|
|
content = [] |
|
|
|
|
|
styles = getSampleStyleSheet() |
|
|
|
|
|
session = init_session(db_path) |
|
|
|
|
|
|
|
|
|
|
|
for book in session.query(Book).all(): |
|
|
|
|
|
content = select_fields(side_a, content, styles, book) |
|
|
|
|
|
content.append(PageBreak()) |
|
|
|
|
|
content = select_fields(side_b, content, styles, book) |
|
|
|
|
|
content.append(PageBreak()) |
|
|
|
|
|
|
|
|
|
|
|
return content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_doc(filename): |
|
|
def create_doc(filename): |
|
|
"""Build the Report Lab document template.""" |
|
|
"""Build the Report Lab document template.""" |
|
|
return SimpleDocTemplate( |
|
|
return SimpleDocTemplate( |
|
|