slicer_ams_pick.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. """The slicer's AMS pick a virtual printer's queue job keeps for its archive (#2700).
  2. ``PrintQueueItem.slicer_ams_mapping`` holds it until the job's print creates
  3. the archive, as ``{"mapping": [global tray ids], "printer_id": id}``. The
  4. printer is the one the slicer resolved the trays against: the job can be moved
  5. to another printer before it prints, and tray numbers mean nothing there, so it
  6. is never taken from the job's current printer.
  7. """
  8. import json
  9. def pack(mapping_json: str | None, printer_id: int | None) -> str | None:
  10. """The column value for the slicer's ``mapping_json`` resolved on ``printer_id``."""
  11. if not mapping_json or printer_id is None:
  12. return None
  13. return json.dumps({"mapping": json.loads(mapping_json), "printer_id": printer_id})
  14. def unpack(raw: str | None) -> tuple[list[int], int] | None:
  15. """``(mapping, printer_id)`` from the column, or None when unset or malformed."""
  16. if not raw:
  17. return None
  18. try:
  19. value = json.loads(raw)
  20. except (TypeError, ValueError):
  21. return None
  22. if not isinstance(value, dict):
  23. return None
  24. mapping, printer_id = value.get("mapping"), value.get("printer_id")
  25. if not isinstance(mapping, list) or not mapping or not isinstance(printer_id, int):
  26. return None
  27. return mapping, printer_id