enable search by IPN, exit out if not found

This commit is contained in:
2025-06-12 10:13:05 -07:00
parent e1639ff8ba
commit 0aa17bfd40
2 changed files with 26 additions and 5 deletions

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)
@@ -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)

View File

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