Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: refactor: improve main.py and data structures #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions paperless_asn_qr_codes/label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""This module provides classes for building a single paperless ASN label with a given layout."""
from dataclasses import dataclass, KW_ONLY
from reportlab.lib.units import mm
from reportlab_qrcode import QRCodeImage

@dataclass
class LabelInfo:
"""Class for modeling label sheet info"""

_: KW_ONLY
digits: int

class Label:
"""Class for building the label"""

def __init__(self, start_asn):
self.current = start_asn

def render(self, canvas, x_size, y_size):
barcode_value = f"ASN{self.current:0{7}d}"

qr = QRCodeImage(barcode_value, size=y_size * 0.9)
qr.drawOn(canvas, 1 * mm, y_size* 0.05)
canvas.setFont("Helvetica", 2 * mm)
canvas.drawString(y_size, (y_size - 2 * mm) / 2, barcode_value)
self.current += 1
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
# pylint: disable=invalid-name,too-many-instance-attributes
"""This module is used to generate label PDFs for Avery labels and other label types."""
"""This module is used to generate label PDFs for label sheets and other label types."""
from dataclasses import dataclass, KW_ONLY
from collections.abc import Iterator
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import LETTER, A4
from reportlab.lib.units import mm, inch


# Usage:
# label = AveryLabels.AveryLabel(5160)
# label.open( "labels5160.pdf" )
# label.render( RenderAddress, 30 )
# label.close()
#
# 'render' can either pass a callable, which receives the canvas object
# (with X,Y=0,0 at the lower right) or a string "form" name of a form
# previously created with canv.beginForm().


@dataclass
class LabelInfo:
"""Class for modeling label info"""
class LabelSheetInfo:
"""Class for modeling label sheet info"""

_: KW_ONLY
labels_horizontal: int
Expand All @@ -31,16 +18,16 @@ class LabelInfo:
pagesize: tuple[float, float]


labelInfo: dict[str, LabelInfo] = {
"averyL4731": LabelInfo(
labelSheetInfo: dict[str, LabelSheetInfo] = {
"averyL4731": LabelSheetInfo(
labels_horizontal=7,
labels_vertical=27,
label_size=(25.4 * mm, 10 * mm),
gutter_size=(2.5 * mm, 0),
margin=(9 * mm, 13.5 * mm),
pagesize=A4,
),
"averyL4732": LabelInfo(
"averyL4732": LabelSheetInfo(
labels_horizontal=5,
labels_vertical=16,
label_size=(35.6 * mm, 16.9 * mm),
Expand All @@ -49,15 +36,15 @@ class LabelInfo:
pagesize=A4,
),
# 2.6 x 1 address labels
"avery5160": LabelInfo(
"avery5160": LabelSheetInfo(
labels_horizontal=3,
labels_vertical=10,
label_size=(187, 72),
gutter_size=(11, 0),
margin=(14, 36),
pagesize=LETTER,
),
"avery5161": LabelInfo(
"avery5161": LabelSheetInfo(
labels_horizontal=2,
labels_vertical=10,
label_size=(288, 72),
Expand All @@ -66,7 +53,7 @@ class LabelInfo:
pagesize=LETTER,
),
# 4 x 2 address labels
"avery5163": LabelInfo(
"avery5163": LabelSheetInfo(
labels_horizontal=2,
labels_vertical=5,
label_size=(288, 144),
Expand All @@ -75,7 +62,7 @@ class LabelInfo:
pagesize=LETTER,
),
# 1.75 x 0.5 return address labels
"avery5167": LabelInfo(
"avery5167": LabelSheetInfo(
labels_horizontal=4,
labels_vertical=20,
label_size=(1.75 * inch, 0.5 * inch),
Expand All @@ -84,7 +71,7 @@ class LabelInfo:
pagesize=LETTER,
),
# 3.5 x 2 business cards
"avery5371": LabelInfo(
"avery5371": LabelSheetInfo(
labels_horizontal=2,
labels_vertical=5,
label_size=(252, 144),
Expand All @@ -93,7 +80,7 @@ class LabelInfo:
pagesize=LETTER,
),
# Herma 4201, 64 removable labels
"herma4201": LabelInfo(
"herma4201": LabelSheetInfo(
labels_horizontal=4,
labels_vertical=16,
label_size=(45.7 * mm, 16.9 * mm),
Expand All @@ -102,15 +89,15 @@ class LabelInfo:
pagesize=A4,
),
# HERMA No. 10003 labels (former article No. 4345)
"herma10003": LabelInfo(
"herma10003": LabelSheetInfo(
labels_horizontal=5,
labels_vertical=16,
label_size=(35.56 * mm, 16.93 * mm),
gutter_size=(2.54 * mm, 0),
margin=(11.02 * mm, 13.06 * mm),
pagesize=A4,
),
"herma4346": LabelInfo(
"herma4346": LabelSheetInfo(
labels_horizontal=4,
labels_vertical=12,
label_size=(45.72*mm, 21.167*mm),
Expand All @@ -124,12 +111,12 @@ class LabelInfo:
BUSINESS_CARDS = 5371


class AveryLabel:
class LabelSheet:
""" class for creating the pdfs """
def __init__(self, label, debug,
topDown=True, start_pos=None,
**kwargs):
data = labelInfo[label]
data = labelSheetInfo[label]
self.across = data.labels_horizontal
self.down = data.labels_vertical
self.size = data.label_size
Expand Down
41 changes: 14 additions & 27 deletions paperless_asn_qr_codes/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
# pylint: disable=global-statement,global-variable-undefined,global-variable-not-assigned,used-before-assignment
""" Main module for the paperless ASN QR code generator, fills the labels with content """
import argparse
import re

from reportlab.lib.units import mm
from reportlab_qrcode import QRCodeImage

from paperless_asn_qr_codes import avery_labels

def render(c, _, y):
""" Render the QR code and ASN number on the label """
global startASN
global digits
barcode_value = f"ASN{startASN:0{digits}d}"
startASN = startASN + 1

qr = QRCodeImage(barcode_value, size=y * 0.9)
qr.drawOn(c, 1 * mm, y * 0.05)
c.setFont("Helvetica", 2 * mm)
c.drawString(y, (y - 2 * mm) / 2, barcode_value)

from paperless_asn_qr_codes import label_sheet, label

def main():
""" Main function for the paperless ASN QR code generator """
Expand All @@ -32,7 +15,7 @@ def _start_position(arg):
raise argparse.ArgumentTypeError("invalid value")

# prepare a sorted list of all formats
available_formats = list(avery_labels.labelInfo.keys())
available_formats = list(label_sheet.labelSheetInfo.keys())
available_formats.sort()

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -90,20 +73,24 @@ def _start_position(arg):
)

args = parser.parse_args()
global startASN
global digits
startASN = int(args.start_asn)
start_asn = int(args.start_asn)
digits = int(args.digits)
label = avery_labels.AveryLabel(
# TODO: set digits to LabelInfo

# setup LabelSheet
sheet = label_sheet.LabelSheet(
args.format, args.border, topDown=args.row_wise, start_pos=args.start_position
)
label.open(args.output_file)
sheet.open(args.output_file)

# setup LabelRenderer with Label
l = label.Label(start_asn)

# If defined use parameter for number of labels
if args.num_labels:
count = args.num_labels
else:
# Otherwise number of pages*labels - offset
count = args.pages * label.across * label.down - label.position
label.render(render, count)
label.close()
count = args.pages * sheet.across * sheet.down - sheet.position
sheet.render(l.render, count)
sheet.close()
Loading