|
6 | 6 | # this notice are preserved.
|
7 | 7 | # === END LICENSE STATEMENT ===
|
8 | 8 | import logging
|
| 9 | +import sys |
9 | 10 | from pathlib import Path
|
10 | 11 | from typing import List, NoReturn, Optional
|
11 | 12 |
|
@@ -200,6 +201,10 @@ def default(
|
200 | 201 | "--qr", callback=qr_callback, help="QR code", rich_help_panel="Elements"
|
201 | 202 | ),
|
202 | 203 | ] = None,
|
| 204 | + batch: Annotated[ |
| 205 | + bool, |
| 206 | + typer.Option(help="Read batch commands from stdin", rich_help_panel="Elements"), |
| 207 | + ] = False, |
203 | 208 | barcode_content: Annotated[
|
204 | 209 | Optional[str],
|
205 | 210 | typer.Option("--barcode", help="Barcode", rich_help_panel="Elements"),
|
@@ -482,20 +487,67 @@ def default(
|
482 | 487 | BarcodeRenderEngine(content=barcode_content, barcode_type=barcode_type)
|
483 | 488 | )
|
484 | 489 |
|
485 |
| - if text: |
| 490 | + def render_text(lines): |
486 | 491 | render_engines.append(
|
487 | 492 | TextRenderEngine(
|
488 |
| - text_lines=text, |
| 493 | + text_lines=lines, |
489 | 494 | font_file_name=font_path,
|
490 | 495 | frame_width_px=frame_width_px,
|
491 | 496 | font_size_ratio=int(font_scale) / 100.0,
|
492 | 497 | align=align,
|
493 | 498 | )
|
494 | 499 | )
|
495 | 500 |
|
| 501 | + if text: |
| 502 | + render_text(text) |
| 503 | + |
496 | 504 | if picture:
|
497 | 505 | render_engines.append(PictureRenderEngine(picture))
|
498 | 506 |
|
| 507 | + if batch: |
| 508 | + accumulator: List[str] = [] |
| 509 | + accumulator_type: str = "empty" |
| 510 | + |
| 511 | + def flush_all(): |
| 512 | + nonlocal accumulator |
| 513 | + nonlocal accumulator_type |
| 514 | + if accumulator_type == "text": |
| 515 | + render_text(accumulator) |
| 516 | + elif accumulator_type == "qr": |
| 517 | + render_engines.append( |
| 518 | + QrRenderEngine(qr_callback("\n".join(accumulator))) |
| 519 | + ) |
| 520 | + |
| 521 | + accumulator = [] |
| 522 | + accumulator_type = "empty" |
| 523 | + |
| 524 | + # Verify version |
| 525 | + line = sys.stdin.readline().strip() |
| 526 | + parts = line.split(":", 1) |
| 527 | + if not (parts[0] == "LABELLE-LABEL-SPEC-VERSION" and parts[1] == "1"): |
| 528 | + err_console = Console(stderr=True) |
| 529 | + err_console.print( |
| 530 | + "Error: Batch doesn't begin with LABELLE-LABEL-SPEC-VERSION:1" |
| 531 | + ) |
| 532 | + raise typer.Exit() |
| 533 | + |
| 534 | + for line in sys.stdin: |
| 535 | + line = line.rstrip("\r\n") |
| 536 | + parts = line.split(":", 1) |
| 537 | + if parts[0] == "TEXT": |
| 538 | + flush_all() |
| 539 | + accumulator_type = "text" |
| 540 | + accumulator.append(parts[1]) |
| 541 | + elif parts[0] == "QR": |
| 542 | + flush_all() |
| 543 | + accumulator_type = "qr" |
| 544 | + accumulator.append(parts[1]) |
| 545 | + elif parts[0] == "NEWLINE": |
| 546 | + accumulator.append(parts[1]) |
| 547 | + else: |
| 548 | + print("WARNING: invalid command", line) |
| 549 | + flush_all() |
| 550 | + |
499 | 551 | if fixed_length is None:
|
500 | 552 | min_label_mm_len = min_length
|
501 | 553 | max_label_mm_len = max_length
|
|
0 commit comments