s-reports 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. s_reports-0.1.0/LICENSE +21 -0
  2. s_reports-0.1.0/PKG-INFO +295 -0
  3. s_reports-0.1.0/README.md +263 -0
  4. s_reports-0.1.0/build/s_reports.egg-info/SOURCES.txt +67 -0
  5. s_reports-0.1.0/pyproject.toml +141 -0
  6. s_reports-0.1.0/setup.cfg +5 -0
  7. s_reports-0.1.0/sr/__init__.py +13 -0
  8. s_reports-0.1.0/sr/__main__.py +16 -0
  9. s_reports-0.1.0/sr/api.py +194 -0
  10. s_reports-0.1.0/sr/barcode/__init__.py +139 -0
  11. s_reports-0.1.0/sr/barcode/aztec.py +796 -0
  12. s_reports-0.1.0/sr/barcode/dmtx.py +370 -0
  13. s_reports-0.1.0/sr/barcode/linear.py +324 -0
  14. s_reports-0.1.0/sr/barcode/qr.py +583 -0
  15. s_reports-0.1.0/sr/barcode/readable.py +116 -0
  16. s_reports-0.1.0/sr/barcode/reedsolomon.py +134 -0
  17. s_reports-0.1.0/sr/barcode/symbol.py +140 -0
  18. s_reports-0.1.0/sr/cli.py +1630 -0
  19. s_reports-0.1.0/sr/color.py +231 -0
  20. s_reports-0.1.0/sr/data.py +352 -0
  21. s_reports-0.1.0/sr/errors.py +523 -0
  22. s_reports-0.1.0/sr/expr/__init__.py +63 -0
  23. s_reports-0.1.0/sr/expr/builtins.py +1574 -0
  24. s_reports-0.1.0/sr/expr/calc.py +544 -0
  25. s_reports-0.1.0/sr/expr/compile.py +595 -0
  26. s_reports-0.1.0/sr/expr/fmt.py +550 -0
  27. s_reports-0.1.0/sr/expr/golayout.py +676 -0
  28. s_reports-0.1.0/sr/expr/literals.py +557 -0
  29. s_reports-0.1.0/sr/expr/values.py +1528 -0
  30. s_reports-0.1.0/sr/fonts/__init__.py +70 -0
  31. s_reports-0.1.0/sr/fonts/face.py +623 -0
  32. s_reports-0.1.0/sr/fonts/hostenum.py +737 -0
  33. s_reports-0.1.0/sr/fonts/resolve.py +566 -0
  34. s_reports-0.1.0/sr/fonts/text.py +338 -0
  35. s_reports-0.1.0/sr/image.py +358 -0
  36. s_reports-0.1.0/sr/kdl.py +713 -0
  37. s_reports-0.1.0/sr/layout/__init__.py +49 -0
  38. s_reports-0.1.0/sr/layout/blobs.py +166 -0
  39. s_reports-0.1.0/sr/layout/columns.py +191 -0
  40. s_reports-0.1.0/sr/layout/context.py +212 -0
  41. s_reports-0.1.0/sr/layout/defer.py +277 -0
  42. s_reports-0.1.0/sr/layout/fonttable.py +147 -0
  43. s_reports-0.1.0/sr/layout/frame.py +361 -0
  44. s_reports-0.1.0/sr/layout/images.py +251 -0
  45. s_reports-0.1.0/sr/layout/loop.py +2884 -0
  46. s_reports-0.1.0/sr/layout/measure.py +1949 -0
  47. s_reports-0.1.0/sr/layout/place.py +266 -0
  48. s_reports-0.1.0/sr/layout/subreport.py +302 -0
  49. s_reports-0.1.0/sr/layout/variables.py +183 -0
  50. s_reports-0.1.0/sr/meta.py +32 -0
  51. s_reports-0.1.0/sr/pdf/__init__.py +27 -0
  52. s_reports-0.1.0/sr/pdf/document.py +663 -0
  53. s_reports-0.1.0/sr/pdf/draw.py +569 -0
  54. s_reports-0.1.0/sr/pdf/images.py +264 -0
  55. s_reports-0.1.0/sr/pdf/objects.py +344 -0
  56. s_reports-0.1.0/sr/pdf/subset.py +457 -0
  57. s_reports-0.1.0/sr/printout/__init__.py +57 -0
  58. s_reports-0.1.0/sr/printout/inspect.py +777 -0
  59. s_reports-0.1.0/sr/printout/model.py +472 -0
  60. s_reports-0.1.0/sr/printout/read.py +746 -0
  61. s_reports-0.1.0/sr/printout/verify.py +672 -0
  62. s_reports-0.1.0/sr/printout/write.py +635 -0
  63. s_reports-0.1.0/sr/py.typed +0 -0
  64. s_reports-0.1.0/sr/settle.py +300 -0
  65. s_reports-0.1.0/sr/template/__init__.py +35 -0
  66. s_reports-0.1.0/sr/template/load.py +1602 -0
  67. s_reports-0.1.0/sr/template/model.py +1172 -0
  68. s_reports-0.1.0/sr/template/validate.py +1313 -0
  69. s_reports-0.1.0/sr/units.py +252 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aleksandr Smyshliaev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,295 @@
1
+ Metadata-Version: 2.4
2
+ Name: s-reports
3
+ Version: 0.1.0
4
+ Summary: A banded report formatter: template plus records in, PDF out.
5
+ Author: Aleksandr Smyshliaev
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/a1s/sr.py
8
+ Project-URL: Reference, https://github.com/a1s/sr
9
+ Keywords: reporting,pdf,kdl,starlark
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Topic :: Printing
14
+ Requires-Python: >=3.11
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: ckdl>=1.0
18
+ Requires-Dist: fonttools
19
+ Requires-Dist: cbor2
20
+ Requires-Dist: pillow
21
+ Requires-Dist: tzdata
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=8; extra == "dev"
24
+ Requires-Dist: pytest-xdist>=3; extra == "dev"
25
+ Requires-Dist: ruff==0.16.6; extra == "dev"
26
+ Requires-Dist: mypy==2.3.1; extra == "dev"
27
+ Provides-Extra: oracles
28
+ Requires-Dist: pypdfium2; extra == "oracles"
29
+ Requires-Dist: starlark; extra == "oracles"
30
+ Requires-Dist: qrcode; extra == "oracles"
31
+ Dynamic: license-file
32
+
33
+ # sr.py — structural reporting
34
+
35
+ A banded report formatter. Apply a template to a sequence of records
36
+ and get a paginated document: bands, groups, page and column headers
37
+ and footers, running aggregates, and page footers that know the final
38
+ page count.
39
+
40
+ A Python library with a CLI over it, counterpart to the parallel
41
+ [Go project](https://github.com/a1s/sr). Template plus JSON in, PDF out.
42
+
43
+ ## Documentation
44
+
45
+ | | |
46
+ |---|---|
47
+ | [doc/template.md](https://github.com/a1s/sr.py/blob/main/doc/template.md) | Template format: nodes, properties, geometry, ordering rules, font resolution |
48
+ | [doc/expressions.md](https://github.com/a1s/sr.py/blob/main/doc/expressions.md) | Expression language, predefined names, formatting, variable semantics |
49
+ | [doc/layout.md](https://github.com/a1s/sr.py/blob/main/doc/layout.md) | Layout and pagination |
50
+ | [doc/printout.md](https://github.com/a1s/sr.py/blob/main/doc/printout.md) | The intermediate document a renderer consumes |
51
+ | [doc/barcode.md](https://github.com/a1s/sr.py/blob/main/doc/barcode.md) | Barcode encoding: which symbol each type draws for a value |
52
+ | [doc/render.md](https://github.com/a1s/sr.py/blob/main/doc/render.md) | PDF rendering: what a renderer decides, and what it must not |
53
+ | [doc/cli.md](https://github.com/a1s/sr.py/blob/main/doc/cli.md) | The command line: subcommands, flags, streams, exit codes |
54
+ | [example/minimal/](https://github.com/a1s/sr.py/tree/main/example/minimal/) | The smallest complete report, and the printout doc/printout.md shows |
55
+ | [example/sakila/](https://github.com/a1s/sr.py/tree/main/example/sakila/) | Reference template and dataset |
56
+ | [example/invoices/](https://github.com/a1s/sr.py/tree/main/example/invoices/) | Second example: both kinds of subreport, region grouping, the remaining variable modes |
57
+ | [example/fonts/](https://github.com/a1s/sr.py/tree/main/example/fonts/) | Fonts committed so examples resolve identically everywhere |
58
+
59
+ ## How it works
60
+
61
+ Three artifacts and one language:
62
+
63
+ - A **template** is a [KDL](https://kdl.dev) v2 document describing page geometry
64
+ and a tree of bands. Bands are `title`, `summary`, `header`, `footer`, and
65
+ `detail`; groups nest around the detail band.
66
+ - **Data** is JSON or NDJSON. Field types are declared in the template, which is
67
+ what turns `"19.99"` into an exact decimal and `"2005-05-24T22:53:30Z"` into a
68
+ time value.
69
+ - A **printout** is the engine's output: pages of absolutely-positioned marks
70
+ with nothing evaluable left in them. Renderers do no layout.
71
+ - **Expressions** are [Starlark](https://github.com/bazelbuild/starlark) —
72
+ sandboxed, with no imports, filesystem, or network.
73
+
74
+ Layout is speculative: every band is measured before it is committed, then placed,
75
+ split, or deferred to the next frame. Band splitting, keep-together, orphan and
76
+ widow control, and correct deferred page counts all follow from that.
77
+
78
+ ## Command line
79
+
80
+ ```bash
81
+ python sr.py build --template sakila.kdl --data payments.jsonl --out report.pdf
82
+ ```
83
+
84
+ That is the launcher at the root of a checkout, which runs the program
85
+ without installing the package. Installed with `pip install s-reports`,
86
+ the same program is the `s-reports` command, and `python -m sr` where
87
+ the interpreter's scripts directory is not on the path.
88
+
89
+ | Flag | |
90
+ |---|---|
91
+ | `-t`, `--template` | Template file. Required. |
92
+ | `-d`, `--data` | JSON array or NDJSON file, `-` for standard input. Omit for a template with no records. |
93
+ | `-o`, `--out` | Output path, `-` for standard output. Extension selects the format: `.pdf`, `.srp.jsonl`, `.srp.cbor`. |
94
+ | `--format` | `pdf`, `jsonl` or `cbor`, when the extension does not say. |
95
+ | `--param NAME=VALUE` | Supply a report parameter. Repeatable. `VALUE` is text, parsed per the parameter's declared type. A name the template does not declare is an error, not a value that goes nowhere. |
96
+ | `--build-time` | RFC 3339. Fixes `BUILD_TIME` for reproducible output. |
97
+ | `--strict-fonts` | Resolve only explicitly named font files; fail otherwise. |
98
+ | `--strict-names` | Refuse a node or property the format does not define, instead of warning about it. |
99
+ | `--accept NAME` | Take one such name without a warning. Repeatable. |
100
+ | `--allow-overflow` | Downgrade oversized-band errors to warnings recorded in the printout. |
101
+ | `--uncompressed` | Leave PDF streams uncompressed, to read the file in an editor. |
102
+ | `-v`, `--verbose` | Report host font diagnostics. |
103
+
104
+ Other subcommands:
105
+
106
+ ```bash
107
+ python sr.py validate sakila.kdl # check a template without data
108
+ python sr.py inspect report.srp.jsonl # dump a printout as readable text
109
+ python sr.py render report.srp.jsonl -o report.pdf
110
+ ```
111
+
112
+ `build` writing a printout and `render` reading one back produce the same PDF as
113
+ `build` writing PDF directly, which is what makes printouts worth archiving.
114
+
115
+ The document goes to standard output and everything about the run to
116
+ standard error, so `-o -` pipes. Exit 0 is success, 1 a run that failed,
117
+ 2 a mistake in the command line; warnings never change it, because they
118
+ travel in the printout header where archiving keeps them. A one-letter flag
119
+ takes one dash and a long one two, so `-t` and `--template`, but not `--t`
120
+ or `-template`. A value is attached with `=` or separate, a flag may follow
121
+ the positional arguments, and a switch also takes `=true` or `=false`.
122
+ Full reference: [doc/cli.md](https://github.com/a1s/sr.py/blob/main/doc/cli.md).
123
+
124
+ ## Library
125
+
126
+ ```python
127
+ from pathlib import Path
128
+
129
+ from sr.api import Options, build
130
+ from sr.printout.write import write_jsonl
131
+
132
+ result = build(
133
+ Path("sakila.kdl"),
134
+ Path("payments.jsonl"),
135
+ Options(params={"period_start": "2005-06-01"}, strict_fonts=True),
136
+ )
137
+ out = Path("sakila.srp.jsonl")
138
+ with out.open("w", encoding="utf-8", newline="") as handle:
139
+ write_jsonl(result.printout, handle, out.parent)
140
+ ```
141
+
142
+ `build` returns the printout as an object, which is the primary artifact:
143
+ serializing it is a separate step, and the directory it is written to is what
144
+ its font and image paths are made relative to. Every flag on the command line
145
+ is a field of `Options`, because the command line
146
+ [decides nothing](https://github.com/a1s/sr.py/blob/main/doc/cli.md).
147
+
148
+ Rendering is a separate step too, and takes the printout in memory
149
+ or one read back from a file. Both give the same bytes:
150
+
151
+ ```python
152
+ from sr.pdf import render
153
+ from sr.printout.read import read
154
+
155
+ Path("sakila.pdf").write_bytes(render(result.printout))
156
+ Path("again.pdf").write_bytes(render(read(out)))
157
+ ```
158
+
159
+ ## Running the examples
160
+
161
+ ```bash
162
+ python sr.py build -t example/minimal/minimal.kdl -d example/minimal/films.jsonl -o minimal.srp.jsonl
163
+ ```
164
+
165
+ [minimal.kdl](https://github.com/a1s/sr.py/blob/main/example/minimal/minimal.kdl)
166
+ is the smallest report there is: one font, a header with a rule,
167
+ and one field per record. Its printout is the example at the end of
168
+ [doc/printout.md](https://github.com/a1s/sr.py/blob/main/doc/printout.md#example),
169
+ which the test suite builds and compares byte for byte, so that example is a
170
+ document rather than an illustration of one.
171
+
172
+ ```bash
173
+ python sr.py build -t example/sakila/sakila.kdl -d example/sakila/payments.jsonl -o sakila.pdf
174
+ ```
175
+
176
+ Narrowed to one month, using the template's `date` parameters:
177
+
178
+ ```bash
179
+ python sr.py build -t example/sakila/sakila.kdl -d example/sakila/payments.jsonl -o june.pdf --param period_start=2005-06-01 --param period_end=2005-07-01
180
+ ```
181
+
182
+ The second example, which uses both kinds of subreport:
183
+
184
+ ```bash
185
+ python sr.py build -t example/invoices/invoices.kdl -d example/invoices/invoices.jsonl -o invoices.pdf
186
+ ```
187
+
188
+ [sakila.kdl](https://github.com/a1s/sr.py/blob/main/example/sakila/sakila.kdl) —
189
+ a payment list grouped by customer in two columns: every band type,
190
+ a group with its own title and summary, column header and footer,
191
+ stretch fields, a floating element, six barcode types, an embedded image
192
+ and a referenced one, both kinds of cross-reference, conditional outline entries,
193
+ a deferred page count, justified text, and typed parameters.
194
+
195
+ [invoices.kdl](https://github.com/a1s/sr.py/blob/main/example/invoices/invoices.kdl) —
196
+ invoices by region with line items: an inline `embedded` subreport
197
+ with an `arg` and its own `records`, a `template=` subreport in
198
+ [region_sheet.kdl](https://github.com/a1s/sr.py/blob/main/example/invoices/region_sheet.kdl)
199
+ that paginates itself onto landscape pages of its own, a group using `keeptogether`
200
+ with `minrows` and `mintailrows`, a `summary` with `swapfooter`, an image with
201
+ `embed=#false`, a compressed `data` blob, `iter="item"` against `iter="detail"`,
202
+ and the `calc` modes sakila leaves out.
203
+
204
+ Between them the two templates use every node in the format but `accept`,
205
+ and all twelve `calc` modes. They do not exhaust every property.
206
+ What they leave out, so that reading them as a reference does not mislead:
207
+
208
+ | | |
209
+ |---|---|
210
+ | Nodes | `accept` |
211
+ | Properties | `layout width` / `height` / `landscape`, `font typeface` / `data` / `bold` / `italic`, `line backslant`, `image type`, `barcode data`, `data expr`, `maxheight`, `format` as a date-parse layout on either `parameter` or `member`, `subreport ownpageno`, `columns balance` |
212
+ | Spellings | the `x` / `y` aliases for `left` / `top` — both examples use `left` and `top` throughout |
213
+ | Enumerations | barcode types `Code93`, `DataMatrix`, `QR-M`; `image scale="cut"` and `"grow"`; `dash="dash"` |
214
+ | Scopes | `iter="report"` / `"page"` / `"column"`; `reset="detail"` / `"item"` |
215
+
216
+ Both reference the [committed fonts](https://github.com/a1s/sr.py/tree/main/example/fonts/)
217
+ by path, so they build identically on any machine and work under `--strict-fonts`.
218
+ Swap `file=` for `typeface=` to use whatever the machine has instead.
219
+
220
+ ## Reproducible output
221
+
222
+ The same template over the same data produces byte-identical printouts when
223
+ `--build-time` is fixed and `--strict-fonts` is set -- **on any machine**,
224
+ not just across runs on one. Rendering adds no timestamp of its own.
225
+ Without the first, the run timestamp differs; without the second,
226
+ output depends on which fonts are installed.
227
+
228
+ Nothing machine-specific survives in a strict printout. Strict mode admits only
229
+ fonts the template named by path, and every path the template named — fonts and
230
+ `embed=#false` images alike — is written relative to the printout rather than
231
+ absolute. So a printout and the files it points at move as one tree. The header
232
+ records which font file each typeface resolved to and by which step, so a difference
233
+ is diagnosable from the artifact.
234
+
235
+ ## Development
236
+
237
+ ```bash
238
+ make install # a venv, the dependencies, and the test and lint tools
239
+ make check # ruff, mypy, pytest
240
+ ```
241
+
242
+ Everything generated goes under `build/`: the pytest, ruff and mypy caches,
243
+ setuptools' metadata, and compiled bytecode. The first three come from
244
+ `pyproject.toml` and the fourth from `PYTHONPYCACHEPREFIX`, which the
245
+ `GNUmakefile` exports — a `pytest` typed straight into a shell still writes
246
+ `__pycache__` beside the sources unless that variable is exported there too.
247
+
248
+ Tests are `pytest`. `tests/unit/` covers a module at a time,
249
+ `tests/golden/` holds this engine to documents that are printed in the
250
+ specification, and `tests/differential/` compares this engine against the
251
+ Go one, byte for byte, over the examples and over a corpus of probes.
252
+ It compares the renderers too, by drawing each page of both PDFs
253
+ with pdfium, from the `oracles` extra, and comparing the pixels;
254
+ a PDF is also held to its structure by the tests' own reader,
255
+ and by `qpdf --check` where qpdf is on the path.
256
+ And it compares the command line: what `validate` reports on every
257
+ template in the corpus, what `inspect` dumps of every printout,
258
+ and the exit code, the output, and the summary line of a table
259
+ of command lines, each asking one question of doc/cli.md.
260
+
261
+ The engine was built a milestone at a time, and every command and
262
+ output format is here now. Every node the format has is built: it paginates
263
+ reports of `field`, `line`, `rectangle`, `image`, `barcode`, and `xref`
264
+ elements and outline entries, with groups, columns, band splitting,
265
+ `eject` nodes, keep-together rules, balanced columns, deferred values,
266
+ and subreports of both kinds, inline and with pages of their own,
267
+ and writes them as NDJSON, CBOR, or PDF. Each printout is held to its
268
+ own [invariants](https://github.com/a1s/sr.py/blob/main/doc/printout.md#invariants)
269
+ before it is written, and `s-reports inspect` holds
270
+ any printout it reads to them. Every barcode type is encoded here,
271
+ from the standards, rather than by a library, and an image
272
+ is interpreted only as far as the header that states its size.
273
+ The PDF writer is written here too: the object table and the cross
274
+ reference, the drawing, the outline and the links. `fontTools` subsets
275
+ the faces and Pillow decodes the images the file does not carry
276
+ as they stand. Each rendered page is drawn by pdfium and compared,
277
+ pixel by pixel, with the same printout rendered by the reference.
278
+ The command line and its two reports are held to the reference's too.
279
+ The cases the differential corpus cannot yet compare are listed,
280
+ with what each waits for, in
281
+ [tests/differential/pending.toml](https://github.com/a1s/sr.py/blob/main/tests/differential/pending.toml).
282
+
283
+ The Go implementation is the **oracle**: a reference binary whose *outputs*
284
+ settle questions the specification leaves open. Its source is not read --
285
+ this is a second implementation from the specification, not a port.
286
+ The harness builds it from a tree beside this repository, and skips
287
+ with a reason where there is no Go toolchain to build it with.
288
+ See [tests/differential/README.md](https://github.com/a1s/sr.py/blob/main/tests/differential/README.md).
289
+
290
+ Where the two engines disagree and `doc/` does not say who is right,
291
+ the answer is a sentence in `doc/`, not a workaround in either engine.
292
+
293
+ ## License
294
+
295
+ MIT. See [LICENSE](https://github.com/a1s/sr.py/blob/main/LICENSE).
@@ -0,0 +1,263 @@
1
+ # sr.py — structural reporting
2
+
3
+ A banded report formatter. Apply a template to a sequence of records
4
+ and get a paginated document: bands, groups, page and column headers
5
+ and footers, running aggregates, and page footers that know the final
6
+ page count.
7
+
8
+ A Python library with a CLI over it, counterpart to the parallel
9
+ [Go project](https://github.com/a1s/sr). Template plus JSON in, PDF out.
10
+
11
+ ## Documentation
12
+
13
+ | | |
14
+ |---|---|
15
+ | [doc/template.md](https://github.com/a1s/sr.py/blob/main/doc/template.md) | Template format: nodes, properties, geometry, ordering rules, font resolution |
16
+ | [doc/expressions.md](https://github.com/a1s/sr.py/blob/main/doc/expressions.md) | Expression language, predefined names, formatting, variable semantics |
17
+ | [doc/layout.md](https://github.com/a1s/sr.py/blob/main/doc/layout.md) | Layout and pagination |
18
+ | [doc/printout.md](https://github.com/a1s/sr.py/blob/main/doc/printout.md) | The intermediate document a renderer consumes |
19
+ | [doc/barcode.md](https://github.com/a1s/sr.py/blob/main/doc/barcode.md) | Barcode encoding: which symbol each type draws for a value |
20
+ | [doc/render.md](https://github.com/a1s/sr.py/blob/main/doc/render.md) | PDF rendering: what a renderer decides, and what it must not |
21
+ | [doc/cli.md](https://github.com/a1s/sr.py/blob/main/doc/cli.md) | The command line: subcommands, flags, streams, exit codes |
22
+ | [example/minimal/](https://github.com/a1s/sr.py/tree/main/example/minimal/) | The smallest complete report, and the printout doc/printout.md shows |
23
+ | [example/sakila/](https://github.com/a1s/sr.py/tree/main/example/sakila/) | Reference template and dataset |
24
+ | [example/invoices/](https://github.com/a1s/sr.py/tree/main/example/invoices/) | Second example: both kinds of subreport, region grouping, the remaining variable modes |
25
+ | [example/fonts/](https://github.com/a1s/sr.py/tree/main/example/fonts/) | Fonts committed so examples resolve identically everywhere |
26
+
27
+ ## How it works
28
+
29
+ Three artifacts and one language:
30
+
31
+ - A **template** is a [KDL](https://kdl.dev) v2 document describing page geometry
32
+ and a tree of bands. Bands are `title`, `summary`, `header`, `footer`, and
33
+ `detail`; groups nest around the detail band.
34
+ - **Data** is JSON or NDJSON. Field types are declared in the template, which is
35
+ what turns `"19.99"` into an exact decimal and `"2005-05-24T22:53:30Z"` into a
36
+ time value.
37
+ - A **printout** is the engine's output: pages of absolutely-positioned marks
38
+ with nothing evaluable left in them. Renderers do no layout.
39
+ - **Expressions** are [Starlark](https://github.com/bazelbuild/starlark) —
40
+ sandboxed, with no imports, filesystem, or network.
41
+
42
+ Layout is speculative: every band is measured before it is committed, then placed,
43
+ split, or deferred to the next frame. Band splitting, keep-together, orphan and
44
+ widow control, and correct deferred page counts all follow from that.
45
+
46
+ ## Command line
47
+
48
+ ```bash
49
+ python sr.py build --template sakila.kdl --data payments.jsonl --out report.pdf
50
+ ```
51
+
52
+ That is the launcher at the root of a checkout, which runs the program
53
+ without installing the package. Installed with `pip install s-reports`,
54
+ the same program is the `s-reports` command, and `python -m sr` where
55
+ the interpreter's scripts directory is not on the path.
56
+
57
+ | Flag | |
58
+ |---|---|
59
+ | `-t`, `--template` | Template file. Required. |
60
+ | `-d`, `--data` | JSON array or NDJSON file, `-` for standard input. Omit for a template with no records. |
61
+ | `-o`, `--out` | Output path, `-` for standard output. Extension selects the format: `.pdf`, `.srp.jsonl`, `.srp.cbor`. |
62
+ | `--format` | `pdf`, `jsonl` or `cbor`, when the extension does not say. |
63
+ | `--param NAME=VALUE` | Supply a report parameter. Repeatable. `VALUE` is text, parsed per the parameter's declared type. A name the template does not declare is an error, not a value that goes nowhere. |
64
+ | `--build-time` | RFC 3339. Fixes `BUILD_TIME` for reproducible output. |
65
+ | `--strict-fonts` | Resolve only explicitly named font files; fail otherwise. |
66
+ | `--strict-names` | Refuse a node or property the format does not define, instead of warning about it. |
67
+ | `--accept NAME` | Take one such name without a warning. Repeatable. |
68
+ | `--allow-overflow` | Downgrade oversized-band errors to warnings recorded in the printout. |
69
+ | `--uncompressed` | Leave PDF streams uncompressed, to read the file in an editor. |
70
+ | `-v`, `--verbose` | Report host font diagnostics. |
71
+
72
+ Other subcommands:
73
+
74
+ ```bash
75
+ python sr.py validate sakila.kdl # check a template without data
76
+ python sr.py inspect report.srp.jsonl # dump a printout as readable text
77
+ python sr.py render report.srp.jsonl -o report.pdf
78
+ ```
79
+
80
+ `build` writing a printout and `render` reading one back produce the same PDF as
81
+ `build` writing PDF directly, which is what makes printouts worth archiving.
82
+
83
+ The document goes to standard output and everything about the run to
84
+ standard error, so `-o -` pipes. Exit 0 is success, 1 a run that failed,
85
+ 2 a mistake in the command line; warnings never change it, because they
86
+ travel in the printout header where archiving keeps them. A one-letter flag
87
+ takes one dash and a long one two, so `-t` and `--template`, but not `--t`
88
+ or `-template`. A value is attached with `=` or separate, a flag may follow
89
+ the positional arguments, and a switch also takes `=true` or `=false`.
90
+ Full reference: [doc/cli.md](https://github.com/a1s/sr.py/blob/main/doc/cli.md).
91
+
92
+ ## Library
93
+
94
+ ```python
95
+ from pathlib import Path
96
+
97
+ from sr.api import Options, build
98
+ from sr.printout.write import write_jsonl
99
+
100
+ result = build(
101
+ Path("sakila.kdl"),
102
+ Path("payments.jsonl"),
103
+ Options(params={"period_start": "2005-06-01"}, strict_fonts=True),
104
+ )
105
+ out = Path("sakila.srp.jsonl")
106
+ with out.open("w", encoding="utf-8", newline="") as handle:
107
+ write_jsonl(result.printout, handle, out.parent)
108
+ ```
109
+
110
+ `build` returns the printout as an object, which is the primary artifact:
111
+ serializing it is a separate step, and the directory it is written to is what
112
+ its font and image paths are made relative to. Every flag on the command line
113
+ is a field of `Options`, because the command line
114
+ [decides nothing](https://github.com/a1s/sr.py/blob/main/doc/cli.md).
115
+
116
+ Rendering is a separate step too, and takes the printout in memory
117
+ or one read back from a file. Both give the same bytes:
118
+
119
+ ```python
120
+ from sr.pdf import render
121
+ from sr.printout.read import read
122
+
123
+ Path("sakila.pdf").write_bytes(render(result.printout))
124
+ Path("again.pdf").write_bytes(render(read(out)))
125
+ ```
126
+
127
+ ## Running the examples
128
+
129
+ ```bash
130
+ python sr.py build -t example/minimal/minimal.kdl -d example/minimal/films.jsonl -o minimal.srp.jsonl
131
+ ```
132
+
133
+ [minimal.kdl](https://github.com/a1s/sr.py/blob/main/example/minimal/minimal.kdl)
134
+ is the smallest report there is: one font, a header with a rule,
135
+ and one field per record. Its printout is the example at the end of
136
+ [doc/printout.md](https://github.com/a1s/sr.py/blob/main/doc/printout.md#example),
137
+ which the test suite builds and compares byte for byte, so that example is a
138
+ document rather than an illustration of one.
139
+
140
+ ```bash
141
+ python sr.py build -t example/sakila/sakila.kdl -d example/sakila/payments.jsonl -o sakila.pdf
142
+ ```
143
+
144
+ Narrowed to one month, using the template's `date` parameters:
145
+
146
+ ```bash
147
+ python sr.py build -t example/sakila/sakila.kdl -d example/sakila/payments.jsonl -o june.pdf --param period_start=2005-06-01 --param period_end=2005-07-01
148
+ ```
149
+
150
+ The second example, which uses both kinds of subreport:
151
+
152
+ ```bash
153
+ python sr.py build -t example/invoices/invoices.kdl -d example/invoices/invoices.jsonl -o invoices.pdf
154
+ ```
155
+
156
+ [sakila.kdl](https://github.com/a1s/sr.py/blob/main/example/sakila/sakila.kdl) —
157
+ a payment list grouped by customer in two columns: every band type,
158
+ a group with its own title and summary, column header and footer,
159
+ stretch fields, a floating element, six barcode types, an embedded image
160
+ and a referenced one, both kinds of cross-reference, conditional outline entries,
161
+ a deferred page count, justified text, and typed parameters.
162
+
163
+ [invoices.kdl](https://github.com/a1s/sr.py/blob/main/example/invoices/invoices.kdl) —
164
+ invoices by region with line items: an inline `embedded` subreport
165
+ with an `arg` and its own `records`, a `template=` subreport in
166
+ [region_sheet.kdl](https://github.com/a1s/sr.py/blob/main/example/invoices/region_sheet.kdl)
167
+ that paginates itself onto landscape pages of its own, a group using `keeptogether`
168
+ with `minrows` and `mintailrows`, a `summary` with `swapfooter`, an image with
169
+ `embed=#false`, a compressed `data` blob, `iter="item"` against `iter="detail"`,
170
+ and the `calc` modes sakila leaves out.
171
+
172
+ Between them the two templates use every node in the format but `accept`,
173
+ and all twelve `calc` modes. They do not exhaust every property.
174
+ What they leave out, so that reading them as a reference does not mislead:
175
+
176
+ | | |
177
+ |---|---|
178
+ | Nodes | `accept` |
179
+ | Properties | `layout width` / `height` / `landscape`, `font typeface` / `data` / `bold` / `italic`, `line backslant`, `image type`, `barcode data`, `data expr`, `maxheight`, `format` as a date-parse layout on either `parameter` or `member`, `subreport ownpageno`, `columns balance` |
180
+ | Spellings | the `x` / `y` aliases for `left` / `top` — both examples use `left` and `top` throughout |
181
+ | Enumerations | barcode types `Code93`, `DataMatrix`, `QR-M`; `image scale="cut"` and `"grow"`; `dash="dash"` |
182
+ | Scopes | `iter="report"` / `"page"` / `"column"`; `reset="detail"` / `"item"` |
183
+
184
+ Both reference the [committed fonts](https://github.com/a1s/sr.py/tree/main/example/fonts/)
185
+ by path, so they build identically on any machine and work under `--strict-fonts`.
186
+ Swap `file=` for `typeface=` to use whatever the machine has instead.
187
+
188
+ ## Reproducible output
189
+
190
+ The same template over the same data produces byte-identical printouts when
191
+ `--build-time` is fixed and `--strict-fonts` is set -- **on any machine**,
192
+ not just across runs on one. Rendering adds no timestamp of its own.
193
+ Without the first, the run timestamp differs; without the second,
194
+ output depends on which fonts are installed.
195
+
196
+ Nothing machine-specific survives in a strict printout. Strict mode admits only
197
+ fonts the template named by path, and every path the template named — fonts and
198
+ `embed=#false` images alike — is written relative to the printout rather than
199
+ absolute. So a printout and the files it points at move as one tree. The header
200
+ records which font file each typeface resolved to and by which step, so a difference
201
+ is diagnosable from the artifact.
202
+
203
+ ## Development
204
+
205
+ ```bash
206
+ make install # a venv, the dependencies, and the test and lint tools
207
+ make check # ruff, mypy, pytest
208
+ ```
209
+
210
+ Everything generated goes under `build/`: the pytest, ruff and mypy caches,
211
+ setuptools' metadata, and compiled bytecode. The first three come from
212
+ `pyproject.toml` and the fourth from `PYTHONPYCACHEPREFIX`, which the
213
+ `GNUmakefile` exports — a `pytest` typed straight into a shell still writes
214
+ `__pycache__` beside the sources unless that variable is exported there too.
215
+
216
+ Tests are `pytest`. `tests/unit/` covers a module at a time,
217
+ `tests/golden/` holds this engine to documents that are printed in the
218
+ specification, and `tests/differential/` compares this engine against the
219
+ Go one, byte for byte, over the examples and over a corpus of probes.
220
+ It compares the renderers too, by drawing each page of both PDFs
221
+ with pdfium, from the `oracles` extra, and comparing the pixels;
222
+ a PDF is also held to its structure by the tests' own reader,
223
+ and by `qpdf --check` where qpdf is on the path.
224
+ And it compares the command line: what `validate` reports on every
225
+ template in the corpus, what `inspect` dumps of every printout,
226
+ and the exit code, the output, and the summary line of a table
227
+ of command lines, each asking one question of doc/cli.md.
228
+
229
+ The engine was built a milestone at a time, and every command and
230
+ output format is here now. Every node the format has is built: it paginates
231
+ reports of `field`, `line`, `rectangle`, `image`, `barcode`, and `xref`
232
+ elements and outline entries, with groups, columns, band splitting,
233
+ `eject` nodes, keep-together rules, balanced columns, deferred values,
234
+ and subreports of both kinds, inline and with pages of their own,
235
+ and writes them as NDJSON, CBOR, or PDF. Each printout is held to its
236
+ own [invariants](https://github.com/a1s/sr.py/blob/main/doc/printout.md#invariants)
237
+ before it is written, and `s-reports inspect` holds
238
+ any printout it reads to them. Every barcode type is encoded here,
239
+ from the standards, rather than by a library, and an image
240
+ is interpreted only as far as the header that states its size.
241
+ The PDF writer is written here too: the object table and the cross
242
+ reference, the drawing, the outline and the links. `fontTools` subsets
243
+ the faces and Pillow decodes the images the file does not carry
244
+ as they stand. Each rendered page is drawn by pdfium and compared,
245
+ pixel by pixel, with the same printout rendered by the reference.
246
+ The command line and its two reports are held to the reference's too.
247
+ The cases the differential corpus cannot yet compare are listed,
248
+ with what each waits for, in
249
+ [tests/differential/pending.toml](https://github.com/a1s/sr.py/blob/main/tests/differential/pending.toml).
250
+
251
+ The Go implementation is the **oracle**: a reference binary whose *outputs*
252
+ settle questions the specification leaves open. Its source is not read --
253
+ this is a second implementation from the specification, not a port.
254
+ The harness builds it from a tree beside this repository, and skips
255
+ with a reason where there is no Go toolchain to build it with.
256
+ See [tests/differential/README.md](https://github.com/a1s/sr.py/blob/main/tests/differential/README.md).
257
+
258
+ Where the two engines disagree and `doc/` does not say who is right,
259
+ the answer is a sentence in `doc/`, not a workaround in either engine.
260
+
261
+ ## License
262
+
263
+ MIT. See [LICENSE](https://github.com/a1s/sr.py/blob/main/LICENSE).
@@ -0,0 +1,67 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ setup.cfg
5
+ sr/__init__.py
6
+ sr/__main__.py
7
+ sr/api.py
8
+ sr/cli.py
9
+ sr/color.py
10
+ sr/data.py
11
+ sr/errors.py
12
+ sr/image.py
13
+ sr/kdl.py
14
+ sr/meta.py
15
+ sr/py.typed
16
+ sr/settle.py
17
+ sr/units.py
18
+ sr/barcode/__init__.py
19
+ sr/barcode/aztec.py
20
+ sr/barcode/dmtx.py
21
+ sr/barcode/linear.py
22
+ sr/barcode/qr.py
23
+ sr/barcode/readable.py
24
+ sr/barcode/reedsolomon.py
25
+ sr/barcode/symbol.py
26
+ sr/expr/__init__.py
27
+ sr/expr/builtins.py
28
+ sr/expr/calc.py
29
+ sr/expr/compile.py
30
+ sr/expr/fmt.py
31
+ sr/expr/golayout.py
32
+ sr/expr/literals.py
33
+ sr/expr/values.py
34
+ sr/fonts/__init__.py
35
+ sr/fonts/face.py
36
+ sr/fonts/hostenum.py
37
+ sr/fonts/resolve.py
38
+ sr/fonts/text.py
39
+ sr/layout/__init__.py
40
+ sr/layout/blobs.py
41
+ sr/layout/columns.py
42
+ sr/layout/context.py
43
+ sr/layout/defer.py
44
+ sr/layout/fonttable.py
45
+ sr/layout/frame.py
46
+ sr/layout/images.py
47
+ sr/layout/loop.py
48
+ sr/layout/measure.py
49
+ sr/layout/place.py
50
+ sr/layout/subreport.py
51
+ sr/layout/variables.py
52
+ sr/pdf/__init__.py
53
+ sr/pdf/document.py
54
+ sr/pdf/draw.py
55
+ sr/pdf/images.py
56
+ sr/pdf/objects.py
57
+ sr/pdf/subset.py
58
+ sr/printout/__init__.py
59
+ sr/printout/inspect.py
60
+ sr/printout/model.py
61
+ sr/printout/read.py
62
+ sr/printout/verify.py
63
+ sr/printout/write.py
64
+ sr/template/__init__.py
65
+ sr/template/load.py
66
+ sr/template/model.py
67
+ sr/template/validate.py