"""Local QR generation for printable inventory labels. No asset writes.""" from urllib.parse import urlsplit, urlunsplit import qrcode from qrcode.image.svg import SvgPathImage from fastapi import HTTPException LABEL_SIZES = {'62x40': (62, 40), '62x29': (62, 29), '89x36': (89, 36)} def hardware_label_url(base_url: str, hardware_id: int) -> str: try: parts = urlsplit(base_url.strip()) except ValueError as error: raise HTTPException(400, "Hub-adressen er ugyldig") from error if (parts.scheme not in {'http', 'https'} or not parts.hostname or parts.username or parts.password or parts.query or parts.fragment): raise HTTPException(400, 'Brug en http- eller https-adresse uden login, søgeparametre eller fragment.') return urlunsplit((parts.scheme, parts.netloc, parts.path.rstrip('/') + f'/hardware/{hardware_id}', '', '')) def hardware_label_qr(url: str) -> str: qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_M, border=4) qr.add_data(url) qr.make(fit=True) return qr.make_image(image_factory=SvgPathImage).to_string().decode('utf-8')