Skip to content

Commit 9e2e7b4

Browse files
committed
Adjust file format and update documentation
1 parent 49bc615 commit 9e2e7b4

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,29 @@ UI does, this mode allows multiple blocks of each type. `--batch` reads from
187187
stdin. Each line must begin with a command, a colon, and then details. The
188188
commands are:
189189

190-
* `TEXTSTART`: Starts a new text block. Any previous text lines will be printed
190+
* `LABELLE-LABEL-SPEC-VERSION:1` must be the first line.
191+
* `TEXT`: Starts a new text block. Any previous text lines will be printed
191192
first.
192-
* `TEXTADD`: Adds an additional line to the current block of text. Used to
193-
create multiline text blocks.
194-
* `QRSTART`: Creates a QR code block
195-
* `QRADD`: Adds additional lines to the text that will be encoded to form the QR
196-
code (rarely used)
193+
* `NEWLINE`: Adds an additional line to the current block of text or QR. Used to
194+
create multiline blocks.
195+
* `QR`: Creates a QR code block
197196

198197
For instance, this input:
199198

200199
```text
201-
TEXTSTART:FD12
202-
TEXTADD:2013
203-
QRSTART:1234
204-
TEXTSTART:BIG
205-
TEXTSTART:LINE1
206-
TEXTADD:LINE2
207-
QRSTART:12345
200+
LABELLE-LABEL-SPEC-VERSION:1
201+
TEXT:FD12
202+
NEWLINE:2013
203+
QR:1234
204+
TEXT:BIG
205+
TEXT:LINE1
206+
NEWLINE:LINE2
207+
QR:12345
208208
```
209209

210210
Creates a label with:
211211

212-
* A 2-line text block (first like `FD12`, second line `2013`)
212+
* A 2-line text block (first line `FD12`, second line `2013`)
213213
* Then, a QR code for 1234
214214
* Then, a line-line text block with the text `BIG`
215215
* Then, another 2-line text block

src/labelle/cli/cli.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -505,45 +505,48 @@ def render_text(lines):
505505
render_engines.append(PictureRenderEngine(picture))
506506

507507
if batch:
508-
text_accumulator: List[str] = []
509-
qr_accumulator: List[str] = []
510-
511-
def flush_text():
512-
nonlocal text_accumulator
513-
if len(text_accumulator) > 0:
514-
render_text(text_accumulator)
515-
text_accumulator = []
516-
517-
def flush_qr():
518-
nonlocal qr_accumulator
519-
if len(qr_accumulator) > 0:
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":
520517
render_engines.append(
521-
QrRenderEngine(qr_callback("\n".join(qr_accumulator)))
518+
QrRenderEngine(qr_callback("\n".join(accumulator)))
522519
)
523-
qr_accumulator = []
524520

525-
def flush_both():
526-
flush_text()
527-
flush_qr()
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()
528533

529534
for line in sys.stdin:
530535
line = line.rstrip("\r\n")
531536
parts = line.split(":", 1)
532-
if parts[0] == "TEXTSTART":
533-
flush_both()
534-
text_accumulator.append(parts[1])
535-
elif parts[0] == "TEXTADD":
536-
flush_qr()
537-
text_accumulator.append(parts[1])
538-
elif parts[0] == "QRSTART":
539-
flush_both()
540-
qr_accumulator.append(parts[1])
541-
elif parts[0] == "QRADD":
542-
flush_text()
543-
qr_accumulator.append(parts[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])
544547
else:
545548
print("WARNING: invalid command", line)
546-
flush_both()
549+
flush_all()
547550

548551
if fixed_length is None:
549552
min_label_mm_len = min_length

0 commit comments

Comments
 (0)