4 Commits

5 changed files with 40 additions and 7 deletions

3
.gitignore vendored
View File

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

View File

@@ -22,6 +22,11 @@ So far, it's been tested with two Zebra printers: an LP2844 and a GK420t. The L
a CUPS print queue feeding it through a network print server. The GK420t was driven by a Windows 11 system, connected to the
printer via USB.
Installation
------------
This package is also available through [PyPI](https://pypi.org/project/partdb-labeler/) and [AUR](https://aur.archlinux.org/packages/python-partdb-labeler).
Usage
-----

View File

@@ -6,6 +6,7 @@ from math import ceil, floor
from PIL import Image
import textwrap
import argparse
import sys
# make substitutions for characters not in CP437
@@ -105,7 +106,8 @@ def font_metrics(res):
def cli():
parser=argparse.ArgumentParser()
parser.add_argument("id", help="part ID", type=int)
parser.add_argument("id", help="part ID (or IPN)")
parser.add_argument("-i", action="store_true", help="search by IPN instead of part ID")
parser.add_argument("-x", help="label width, in inches (default: 2\")", type=float)
parser.add_argument("-y", help="label height, in inches (default: 1\")", type=float)
parser.add_argument("-g", help="label gap, in inches (default: 0.118\")", type=float)
@@ -113,7 +115,7 @@ def cli():
parser.add_argument("-p", help="PartDB base URL")
parser.add_argument("-k", help="PartDB API key")
parser.add_argument("-r", help="printer resolution (default: 203 dpi)", type=int)
parser.add_argument('-v', action='version', version='%(prog)s 0.1.2')
parser.add_argument('-v', action='version', version='%(prog)s 0.3.1')
args=parser.parse_args()
id=args.id
if args.x==None:
@@ -143,12 +145,25 @@ def cli():
# look up the part
url=f"{base_url}/api/parts/{id}"
if args.i==True:
url=f"{base_url}/api/parts/?ipn={id}"
else:
url=f"{base_url}/api/parts/{id}"
headers={}
headers["Accept"]="application/json"
if api_key!=None:
headers["Authorization"]=f"Bearer {api_key}"
part=requests.get(url, headers=headers).json()
if args.i==True: # search by IPN
try:
part=part[0]
except IndexError:
sys.exit("part not found")
try:
if part["status"]==404: # catch a search that returns nothing
sys.exit("part not found")
except KeyError:
pass
# render a label for it
@@ -168,9 +183,15 @@ def cli():
qr_size=qr(z, f"{base_url}/en/part/{id}", (10, 10), 4, 2)
loc=(15+qr_size, 20)
loc=textline(z, res, part["ipn"], loc, 5)
try:
loc=textline(z, res, part["ipn"], loc, 5)
except KeyError:
loc=textline(z, res, "", loc, 5)
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)
try:
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)
except KeyError:
loc, excess=textbox(z, res, "", loc, (label_width-loc[0]-10, label_height-loc[1]-10), 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)

6
publish.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
# publish to PyPI
rm -r dist || true
python setup.py sdist bdist_wheel
twine upload dist/*

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "partdb_labeler"
version = "0.2.4"
version = "0.3.1"
authors = [
{name = "Scott Alfter", email = "scott@alfter.us"},
]