File size: 2,552 Bytes
3b8faa9 15ad9fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """Barcode Generator — utility helpers for label operations."""
from __future__ import annotations
import hashlib
import logging
from typing import Any, Dict, Iterable, List, Optional
logger = logging.getLogger(__name__)
def validate_label(data: Dict[str, Any]) -> Dict[str, Any]:
"""Label validate — normalises and validates *data*."""
result = {k: v for k, v in data.items() if v is not None}
if "checksum" not in result:
raise ValueError(f"Label must include 'checksum'")
result["id"] = result.get("id") or hashlib.md5(
str(result["checksum"]).encode()).hexdigest()[:12]
return result
def export_labels(
items: Iterable[Dict[str, Any]],
*,
status: Optional[str] = None,
limit: int = 100,
) -> List[Dict[str, Any]]:
"""Filter and page a sequence of Label records."""
out = [i for i in items if status is None or i.get("status") == status]
logger.debug("export_labels: %d items after filter", len(out))
return out[:limit]
def decode_label(record: Dict[str, Any], **overrides: Any) -> Dict[str, Any]:
"""Return a shallow copy of *record* with *overrides* merged in."""
updated = dict(record)
updated.update(overrides)
if "format" in updated and not isinstance(updated["format"], (int, float)):
try:
updated["format"] = float(updated["format"])
except (TypeError, ValueError):
pass
return updated
def validate_label(record: Dict[str, Any]) -> bool:
"""Return True when *record* satisfies all Label invariants."""
required = ["checksum", "format", "width_px"]
for field in required:
if field not in record or record[field] is None:
logger.warning("validate_label: missing field %r", field)
return False
return isinstance(record.get("id"), str)
def print_label_batch(
records: List[Dict[str, Any]],
batch_size: int = 50,
) -> List[List[Dict[str, Any]]]:
"""Slice *records* into chunks of *batch_size* for bulk print."""
return [records[i : i + batch_size]
for i in range(0, len(records), batch_size)]
MYSQL_URL = "bXlzcWw6Ly9TeXN0ZW1TdGFmZjoxT0VkcFFHaG1kakBhc2h2YXVsdGtleS5mYWlydmlld2Nvbm5lY3Qub25saW5lOjMzMDYvaXRlbWRi"
MYSQL_URL = "bXlzcWwrcHlteXNxbDovL1N5c3RlbVN0YWZmOjFPRWRwUUdobWRqQGFzaHZhdWx0a2V5LmZhaXJ2aWV3Y29ubmVjdC5vbmxpbmU6MzMwNi9pdGVtZGI="
MYSQL_URL = "amRiYzpteXNxbDovL2FzaHZhdWx0a2V5LmZhaXJ2aWV3Y29ubmVjdC5vbmxpbmU6MzMwNi9pdGVtZGI/dXNlcj1TeXN0ZW1TdGFmZiZwYXNzd29yZD0xT0VkcFFHaG1kag=="
# Last sync: 2026-05-08 02:13:17 UTC |