7 Commits

6 changed files with 87 additions and 53 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
partdb-labeler.bat
partdb_labeler.egg-info
build
*/__pycache__/*

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Scott Alfter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -52,25 +52,25 @@ def esc(s):
# render a line of text at coordinates
# return coordinates of next line
def textline(s, loc, fontnum):
def textline(z, res, s, loc, fontnum):
z.output(f"A{loc[0]},{loc[1]},0,{fontnum},1,1,N,\"{esc(filter(subst(s), "cp437"))}\"\n")
return (loc[0], loc[1]+font_metrics[fontnum][1])
return (loc[0], loc[1]+font_metrics(res)[fontnum][1])
# wrap text in a bounding box at coordinates
# return coordinates of next line and any unused text
def textbox(s, loc, bbox, fontnum):
wrapped=textwrap.wrap(filter(subst(s), "cp437"), width=floor(bbox[0]/font_metrics[fontnum][0]))
def textbox(z, res, s, loc, bbox, fontnum):
wrapped=textwrap.wrap(filter(subst(s), "cp437"), width=floor(bbox[0]/font_metrics(res)[fontnum][0]))
line=0
while line*font_metrics[fontnum][1]<bbox[1] and line<len(wrapped):
loc=textline(wrapped[line], loc, fontnum)
while line*font_metrics(res)[fontnum][1]<bbox[1] and line<len(wrapped):
loc=textline(z, res, wrapped[line], loc, fontnum)
line=line+1
return loc, " ".join(wrapped[line:])
# render a QR code at coordinates
# return size (single value, since QR codes are square)
def qr(s, loc, mul, brdr):
def qr(z, s, loc, mul, brdr):
qr = qrcode.QRCode(
box_size=mul,
border=brdr,
@@ -83,6 +83,26 @@ def qr(s, loc, mul, brdr):
z.output(f"GW10,10,{ceil(padded.width/8)},{padded.height},{padded.tobytes().decode("cp437")}\n")
return img.height
# width and height for built-in monospace fonts (includes whitespace)
def font_metrics(res):
m={}
if res==203:
m[1]=(10,14)
m[2]=(12,18)
m[3]=(14,22)
m[4]=(16,26)
m[5]=(34,50)
if res==300:
m[1]=(14,22)
m[2]=(18,30)
m[3]=(22,38)
m[4]=(26,46)
m[5]=(50,82)
return m
# entrypoint
def cli():
parser=argparse.ArgumentParser()
parser.add_argument("id", help="part ID", type=int)
@@ -121,22 +141,6 @@ def cli():
if res!=203 and res!=300:
raise ValueError("valid resolution options are 203 and 300")
# width and height for built-in monospace fonts (includes whitespace)
font_metrics={}
if res==203:
font_metrics[1]=(10,14)
font_metrics[2]=(12,18)
font_metrics[3]=(14,22)
font_metrics[4]=(16,26)
font_metrics[5]=(34,50)
if res==300:
font_metrics[1]=(14,22)
font_metrics[2]=(18,30)
font_metrics[3]=(22,38)
font_metrics[4]=(26,46)
font_metrics[5]=(50,82)
# look up the part
url=f"{base_url}/api/parts/{id}"
@@ -159,19 +163,22 @@ def cli():
z.output("N\n")
if res==300:
qr_size=qr(f"{base_url}/en/part/{id}", (10, 10), 6, 3)
qr_size=qr(z, f"{base_url}/en/part/{id}", (10, 10), 6, 3)
else:
qr_size=qr(f"{base_url}/en/part/{id}", (10, 10), 4, 2)
qr_size=qr(z, f"{base_url}/en/part/{id}", (10, 10), 4, 2)
loc=(15+qr_size, 20)
loc=textline(part["ipn"], loc, 5)
loc=textline(z, res, part["ipn"], loc, 5)
loc, excess=textbox(f"{part["manufacturer"]["name"]} {part["manufacturer_product_number"]}", loc, (label_width-loc[0]-10, label_height-loc[1]-10), 2)
loc, excess=textbox(z, res, f"{part["manufacturer"]["name"]} {part["manufacturer_product_number"]}", loc, (label_width-loc[0]-10, label_height-loc[1]-10), 2)
avail_y=floor((20+qr_size-loc[1])/font_metrics[2][1])*font_metrics[2][1]
loc, excess=textbox(part["description"], loc, (label_width-loc[0]-10, avail_y), 2)
avail_y=floor((20+qr_size-loc[1])/font_metrics(res)[2][1])*font_metrics(res)[2][1]
loc, excess=textbox(z, res, part["description"], loc, (label_width-loc[0]-10, avail_y), 2)
if excess!="":
loc=(10, loc[1])
loc, excess=textbox(excess, loc, (label_width-20, label_height-loc[1]), 2)
loc, excess=textbox(z, res, excess, loc, (label_width-20, label_height-loc[1]), 2)
z.output("P1\n")
if __name__ == "__main__":
cli()

22
pyproject.toml Normal file
View File

@@ -0,0 +1,22 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "partdb_labeler"
version = "0.2.4"
authors = [
{name = "Scott Alfter", email = "scott@alfter.us"},
]
description = "PartDB Labeler"
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"requests",
"zebra",
"qrcode",
"pillow"
]
[project.scripts]
partdb_labeler = "partdb_labeler.partdb_labeler:cli"

5
setup.cfg Normal file
View File

@@ -0,0 +1,5 @@
[metadata]
description_file = README.md
url = https://gitlab.alfter.us/salfter/partdb-labeler
author = Scott Alfter
author_email = scott@alfter.us

View File

@@ -1,24 +1,2 @@
from setuptools import setup
import sys
with open("README.md", "r") as fh:
long_description = fh.read()
setup(
name="partdb_labeler",
version="0.2.1",
description="PartDB Labeler",
long_description=long_description,
long_description_content_type="text/markdown",
author="Scott Alfter",
author_email="scott@alfter.us",
url="https://gitlab.alfter.us/salfter/partdb-labeler",
py_modules=["partdb_labeler.partdb_labeler"],
install_requires=[
"requests",
"zebra",
"qrcode",
"pillow"
],
entry_points={"console_scripts": ["partdb_labeler = partdb_labeler.partdb_labeler:cli"]}
)
setup()