isis-report-lib 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.
- isis_report_lib-0.1.0/.github/dependabot.yml +10 -0
- isis_report_lib-0.1.0/.github/workflows/ci.yml +75 -0
- isis_report_lib-0.1.0/.gitignore +16 -0
- isis_report_lib-0.1.0/AGENTS.md +67 -0
- isis_report_lib-0.1.0/LICENSE +21 -0
- isis_report_lib-0.1.0/PKG-INFO +139 -0
- isis_report_lib-0.1.0/README.md +116 -0
- isis_report_lib-0.1.0/examples/generate_example.py +63 -0
- isis_report_lib-0.1.0/examples/pedido.toon +41 -0
- isis_report_lib-0.1.0/pyproject.toml +51 -0
- isis_report_lib-0.1.0/src/isis_report_lib/__init__.py +40 -0
- isis_report_lib-0.1.0/src/isis_report_lib/api.py +106 -0
- isis_report_lib-0.1.0/src/isis_report_lib/formatting.py +28 -0
- isis_report_lib-0.1.0/src/isis_report_lib/layout.py +128 -0
- isis_report_lib-0.1.0/src/isis_report_lib/models.py +22 -0
- isis_report_lib-0.1.0/src/isis_report_lib/pdf.py +160 -0
- isis_report_lib-0.1.0/src/isis_report_lib/rendering.py +49 -0
- isis_report_lib-0.1.0/src/isis_report_lib/service.py +92 -0
- isis_report_lib-0.1.0/src/isis_report_lib/templates/relatorio_base.jinja +213 -0
- isis_report_lib-0.1.0/src/isis_report_lib/templates/relatorio_tabular.jinja +11 -0
- isis_report_lib-0.1.0/src/isis_report_lib/templates/relatorio_tabular_sections.jinja +68 -0
- isis_report_lib-0.1.0/src/isis_report_lib/toon.py +174 -0
- isis_report_lib-0.1.0/tests/__init__.py +0 -0
- isis_report_lib-0.1.0/tests/test_formatting.py +31 -0
- isis_report_lib-0.1.0/tests/test_layout.py +85 -0
- isis_report_lib-0.1.0/tests/test_pdf_e2e.py +70 -0
- isis_report_lib-0.1.0/tests/test_service.py +71 -0
- isis_report_lib-0.1.0/tests/test_toon.py +40 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
concurrency:
|
|
12
|
+
group: ci-${{ github.ref }}
|
|
13
|
+
cancel-in-progress: true
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
lint-unit:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
strategy:
|
|
19
|
+
matrix:
|
|
20
|
+
python-version: ["3.11", "3.12", "3.13", "3.14", "3.15"]
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v7
|
|
23
|
+
with:
|
|
24
|
+
persist-credentials: false
|
|
25
|
+
|
|
26
|
+
- uses: actions/setup-python@v7
|
|
27
|
+
with:
|
|
28
|
+
python-version: ${{ matrix.python-version }}
|
|
29
|
+
cache: pip
|
|
30
|
+
cache-dependency-path: pyproject.toml
|
|
31
|
+
|
|
32
|
+
- name: Install
|
|
33
|
+
run: pip install -e ".[dev]"
|
|
34
|
+
|
|
35
|
+
- name: Lint
|
|
36
|
+
run: python -m ruff check src tests
|
|
37
|
+
|
|
38
|
+
- name: Format
|
|
39
|
+
run: python -m ruff format --check src tests
|
|
40
|
+
|
|
41
|
+
- name: Unit tests (no browser)
|
|
42
|
+
run: python -m pytest tests -q -m "not browser"
|
|
43
|
+
|
|
44
|
+
e2e:
|
|
45
|
+
runs-on: ubuntu-24.04
|
|
46
|
+
timeout-minutes: 20
|
|
47
|
+
env:
|
|
48
|
+
DEBIAN_FRONTEND: noninteractive
|
|
49
|
+
steps:
|
|
50
|
+
- uses: actions/checkout@v7
|
|
51
|
+
with:
|
|
52
|
+
persist-credentials: false
|
|
53
|
+
|
|
54
|
+
- uses: actions/setup-python@v7
|
|
55
|
+
with:
|
|
56
|
+
python-version: "3.14"
|
|
57
|
+
cache: pip
|
|
58
|
+
cache-dependency-path: pyproject.toml
|
|
59
|
+
|
|
60
|
+
- name: Install
|
|
61
|
+
run: pip install -e ".[dev]"
|
|
62
|
+
|
|
63
|
+
- name: Cache Playwright browsers
|
|
64
|
+
uses: actions/cache@v4
|
|
65
|
+
with:
|
|
66
|
+
path: ~/.cache/ms-playwright
|
|
67
|
+
key: playwright-${{ runner.os }}-${{ hashFiles('pyproject.toml') }}
|
|
68
|
+
restore-keys: |
|
|
69
|
+
playwright-${{ runner.os }}-
|
|
70
|
+
|
|
71
|
+
- name: Install Chromium
|
|
72
|
+
run: python -m playwright install --with-deps chromium
|
|
73
|
+
|
|
74
|
+
- name: Full tests (incl. browser)
|
|
75
|
+
run: timeout 600 python -m pytest tests -q
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Guia de Agentes de IA — isis-report-lib
|
|
2
|
+
|
|
3
|
+
Biblioteca Python (`src/isis_report_lib`) para geração de PDFs tabulares com layouts
|
|
4
|
+
hierárquicos. Extraída do POC `isis_report` (microserviço Robyn); aqui **não há
|
|
5
|
+
servidor** — só a lib + testes + exemplos.
|
|
6
|
+
|
|
7
|
+
## Regras de Ouro
|
|
8
|
+
1. **API dupla:** toda funcionalidade pública nova precisa de versão `async def a...`
|
|
9
|
+
e wrapper sync em `src/isis_report_lib/api.py` (via `_run`, que lida com loop ativo).
|
|
10
|
+
2. **Sem Robyn:** nada de `robyn`, rotas ou `JSONResponse` neste repo. O pipeline vive em
|
|
11
|
+
`service.py` (`TabularReportService.execute`: Obter → Tratar → HTML → PDF).
|
|
12
|
+
3. **Templates embutidos:** ficam em `src/isis_report_lib/templates/` e são carregados via
|
|
13
|
+
`rendering.get_jinja_env()` (importlib.resources). Nunca use caminho relativo a `cwd`.
|
|
14
|
+
|
|
15
|
+
## Estrutura
|
|
16
|
+
```
|
|
17
|
+
src/isis_report_lib/
|
|
18
|
+
├── __init__.py # exports públicos (atualizar __all__ ao expor algo novo)
|
|
19
|
+
├── api.py # generate_pdf/agenerate_pdf, ..._from_toon, render_html/...
|
|
20
|
+
├── models.py # MetaInfo, ReportPayload (pydantic v2)
|
|
21
|
+
├── layout.py # convert_layout, convert_data, infer_layout, is_template_format
|
|
22
|
+
├── toon.py # ToonParser, parse_toon
|
|
23
|
+
├── formatting.py # format_value (máscaras int/float/date/str)
|
|
24
|
+
├── rendering.py # arender_report_html / render_report_html
|
|
25
|
+
├── pdf.py # pool Playwright (ensure_browser, get_pdf_from_html, close_browser)
|
|
26
|
+
├── service.py # BaseReportService + TabularReportService
|
|
27
|
+
└── templates/ # relatorio_base/tabular*.jinja
|
|
28
|
+
tests/ # test_formatting/layout/toon/service (mocks), test_pdf_e2e (browser)
|
|
29
|
+
examples/ # generate_example.py + pedido.toon
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Detalhes que já morderam (não reintroduzir)
|
|
33
|
+
- **Dados crus sem `layout`:** o template itera `data.fields`, então registros crus precisam
|
|
34
|
+
ser embrulhados em `{"fields": ...}` (ver `_tratar_dados` e `arender_html`). O POC tinha
|
|
35
|
+
esse bug (HTML vazio); a lib corrige — manter.
|
|
36
|
+
- **Pool Playwright × event loops:** `_browser` é singleton mas preso a um loop. `pdf.py`
|
|
37
|
+
rastreia `_owner_loop` e recria o browser em loop novo/fechado (suítes criam um loop por
|
|
38
|
+
teste). Não volte a checar só `if _browser is not None`.
|
|
39
|
+
- **`_run` com loop ativo:** usa `ThreadPoolExecutor + asyncio.run` para não travar
|
|
40
|
+
(Jupyter). Não simplificar para `asyncio.run` direto.
|
|
41
|
+
|
|
42
|
+
## Workflow
|
|
43
|
+
```bash
|
|
44
|
+
pip install -e ".[dev]"
|
|
45
|
+
playwright install chromium # só p/ testes e2e / geração real de PDF
|
|
46
|
+
python -m pytest tests -q -m "not browser" # sem browser
|
|
47
|
+
python -m pytest tests -q # completo (precisa do chromium)
|
|
48
|
+
python -m ruff check src tests
|
|
49
|
+
python -m ruff format --check src tests
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Testes
|
|
53
|
+
- Unitários com mocks para `rendering.arender_report_html` e `pdf.get_pdf_from_html`
|
|
54
|
+
(ver `test_service.py`); nunca exigir browser fora de `test_pdf_e2e.py`.
|
|
55
|
+
- `test_pdf_e2e.py` é marcado `browser` e pula sozinho sem Chromium
|
|
56
|
+
(`_browser_available`). Registrar marks novas em `pyproject.toml`.
|
|
57
|
+
- Ao mudar `layout.py`/`toon.py`/`formatting.py`, portar/adicionar casos nos testes
|
|
58
|
+
correspondentes.
|
|
59
|
+
|
|
60
|
+
## Convenções
|
|
61
|
+
- Código e docstrings: classes em Inglês; pipeline do serviço pode usar PT
|
|
62
|
+
(`_obter_dados`, `_tratar_dados`, `_gerar_html`, `_gerar_pdf`) por paridade com o POC.
|
|
63
|
+
- Máscaras: `str` · `int` (`1.234`) · `float` pt-BR (`1.234,50`) · `date` (pass-through).
|
|
64
|
+
- Templates: estendem `relatorio_base.jinja`; formatação via `fmt(valor, mascara)` do Jinja.
|
|
65
|
+
|
|
66
|
+
## Commits
|
|
67
|
+
Conventional Commits: `feat:`, `fix:`, `refactor:`, `test:`, `docs:`, `chore:`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Gil Cleber Barboza
|
|
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,139 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: isis-report-lib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Biblioteca Python para geração de relatórios PDF tabulares com layouts hierárquicos (Jinja2 + Playwright). Extraída do POC isis_report.
|
|
5
|
+
Project-URL: Homepage, https://github.com/thepaneb/isis_report
|
|
6
|
+
Author: Gil Cleber Barboza
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: jinja2,pdf,playwright,report,toon
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: jinja2>=3.1
|
|
15
|
+
Requires-Dist: playwright>=1.40
|
|
16
|
+
Requires-Dist: pydantic>=2.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest-timeout>=2; extra == 'dev'
|
|
20
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
21
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# isis-report-lib
|
|
25
|
+
|
|
26
|
+
Biblioteca Python para geração de relatórios **PDF tabulares** com layouts hierárquicos customizáveis — extraída do POC [isis_report](https://github.com/thepaneb/isis_report) (microserviço Robyn), sem o servidor.
|
|
27
|
+
|
|
28
|
+
- Entrada: `dict`/`ReportPayload` (`meta` + `layout` opcional + `data`) ou texto **`.toon`**
|
|
29
|
+
- Renderização: Jinja2 (templates embutidos no pacote)
|
|
30
|
+
- PDF: Playwright (Chromium)
|
|
31
|
+
- API: síncrona (`generate_pdf`) + assíncrona (`agenerate_pdf`)
|
|
32
|
+
|
|
33
|
+
## Instalação
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install isis-report-lib
|
|
37
|
+
playwright install chromium # necessário p/ gerar PDF
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Dev:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install -e ".[dev]"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Uso rápido
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from isis_report_lib import generate_pdf, generate_pdf_from_toon, render_html
|
|
50
|
+
|
|
51
|
+
payload = {
|
|
52
|
+
"meta": {"title": "Pedido de Vendas", "sub_title": "2024", "mode": "exact"},
|
|
53
|
+
"layout": {
|
|
54
|
+
"empresa": {"label": "Empresa", "format_mask": "str", "alignment": "left"},
|
|
55
|
+
"pedidos": {
|
|
56
|
+
"pedido": {"label": "Código", "format_mask": "int", "alignment": "right"},
|
|
57
|
+
"valor_total": {"label": "Valor Total", "format_mask": "float", "alignment": "right"},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
"data": [{"empresa": "CQ CONSULTORIA", "pedidos": [{"pedido": 750, "valor_total": 4000}]}],
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
pdf_bytes = generate_pdf(payload)
|
|
64
|
+
open("relatorio.pdf", "wb").write(pdf_bytes)
|
|
65
|
+
|
|
66
|
+
# Sem Playwright: só o HTML intermediário
|
|
67
|
+
html = render_html(payload)
|
|
68
|
+
|
|
69
|
+
# A partir de .toon
|
|
70
|
+
toon = open("examples/pedido.toon", encoding="utf-8").read()
|
|
71
|
+
open("relatorio_toon.pdf", "wb").write(generate_pdf_from_toon(toon))
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Async:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from isis_report_lib import agenerate_pdf, agenerate_pdf_from_toon
|
|
78
|
+
|
|
79
|
+
pdf = await agenerate_pdf(payload)
|
|
80
|
+
pdf = await agenerate_pdf_from_toon(toon_text)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Opções do PDF (repassadas ao `page.pdf` do Playwright):
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
generate_pdf(payload, pdf_options={"format": "A4", "margin": {"top": ".2in", "bottom": ".5in"}})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Exemplo completo em `examples/`.
|
|
90
|
+
|
|
91
|
+
## Payload
|
|
92
|
+
|
|
93
|
+
| Campo | Tipo | Obrigatório | Descrição |
|
|
94
|
+
| --- | --- | --- | --- |
|
|
95
|
+
| `meta` | object | sim | `title`, `sub_title`, `mode` (`"exact"`/`"gray"`), `company` |
|
|
96
|
+
| `layout` | object | não | definição hierárquica; omitir = inferido dos dados |
|
|
97
|
+
| `data` | array | sim | registros crus ou formato template (`{"fields": ..., "groups": ...}`) |
|
|
98
|
+
|
|
99
|
+
Máscaras: `str` · `int` (milhar `1.234`) · `float` (pt-BR `1.234,50`) · `date`.
|
|
100
|
+
|
|
101
|
+
## Sintaxe `.toon`
|
|
102
|
+
|
|
103
|
+
```toon
|
|
104
|
+
meta:
|
|
105
|
+
title: Pedido de Vendas
|
|
106
|
+
mode: exact
|
|
107
|
+
layout:
|
|
108
|
+
produto{Produto,str,left}
|
|
109
|
+
valor{Valor,float,right}
|
|
110
|
+
dados[1]:
|
|
111
|
+
- produto: Caneta
|
|
112
|
+
valor: 5.50
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`dados[n]` é mapeado para `data`. Ver `examples/pedido.toon`.
|
|
116
|
+
|
|
117
|
+
## API
|
|
118
|
+
|
|
119
|
+
| Função | Descrição |
|
|
120
|
+
| --- | --- |
|
|
121
|
+
| `generate_pdf(payload, pdf_options?) -> bytes` | PDF sync a partir de dict/`ReportPayload` |
|
|
122
|
+
| `agenerate_pdf(...)` | idem, async |
|
|
123
|
+
| `generate_pdf_from_toon(text, ...)` / `agenerate_pdf_from_toon` | PDF a partir de `.toon` |
|
|
124
|
+
| `render_html(payload)` / `arender_html` / `render_html_from_toon` | HTML intermediário (sem browser) |
|
|
125
|
+
| `ToonParser().parse(text)` / `parse_toon(text)` | parse `.toon` -> dict |
|
|
126
|
+
| `TabularReportService(pdf_options?)` | pipeline `execute(payload)` (Obter→Tratar→HTML→PDF) |
|
|
127
|
+
|
|
128
|
+
## Testes
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pytest -q
|
|
132
|
+
ruff check src tests
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Testes de PDF real exigem `playwright install chromium` e são pulados (`-m "not browser"`) se o browser não estiver disponível.
|
|
136
|
+
|
|
137
|
+
## Licença
|
|
138
|
+
|
|
139
|
+
MIT — ver `LICENSE`.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# isis-report-lib
|
|
2
|
+
|
|
3
|
+
Biblioteca Python para geração de relatórios **PDF tabulares** com layouts hierárquicos customizáveis — extraída do POC [isis_report](https://github.com/thepaneb/isis_report) (microserviço Robyn), sem o servidor.
|
|
4
|
+
|
|
5
|
+
- Entrada: `dict`/`ReportPayload` (`meta` + `layout` opcional + `data`) ou texto **`.toon`**
|
|
6
|
+
- Renderização: Jinja2 (templates embutidos no pacote)
|
|
7
|
+
- PDF: Playwright (Chromium)
|
|
8
|
+
- API: síncrona (`generate_pdf`) + assíncrona (`agenerate_pdf`)
|
|
9
|
+
|
|
10
|
+
## Instalação
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install isis-report-lib
|
|
14
|
+
playwright install chromium # necessário p/ gerar PDF
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Dev:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install -e ".[dev]"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Uso rápido
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from isis_report_lib import generate_pdf, generate_pdf_from_toon, render_html
|
|
27
|
+
|
|
28
|
+
payload = {
|
|
29
|
+
"meta": {"title": "Pedido de Vendas", "sub_title": "2024", "mode": "exact"},
|
|
30
|
+
"layout": {
|
|
31
|
+
"empresa": {"label": "Empresa", "format_mask": "str", "alignment": "left"},
|
|
32
|
+
"pedidos": {
|
|
33
|
+
"pedido": {"label": "Código", "format_mask": "int", "alignment": "right"},
|
|
34
|
+
"valor_total": {"label": "Valor Total", "format_mask": "float", "alignment": "right"},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
"data": [{"empresa": "CQ CONSULTORIA", "pedidos": [{"pedido": 750, "valor_total": 4000}]}],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
pdf_bytes = generate_pdf(payload)
|
|
41
|
+
open("relatorio.pdf", "wb").write(pdf_bytes)
|
|
42
|
+
|
|
43
|
+
# Sem Playwright: só o HTML intermediário
|
|
44
|
+
html = render_html(payload)
|
|
45
|
+
|
|
46
|
+
# A partir de .toon
|
|
47
|
+
toon = open("examples/pedido.toon", encoding="utf-8").read()
|
|
48
|
+
open("relatorio_toon.pdf", "wb").write(generate_pdf_from_toon(toon))
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Async:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from isis_report_lib import agenerate_pdf, agenerate_pdf_from_toon
|
|
55
|
+
|
|
56
|
+
pdf = await agenerate_pdf(payload)
|
|
57
|
+
pdf = await agenerate_pdf_from_toon(toon_text)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Opções do PDF (repassadas ao `page.pdf` do Playwright):
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
generate_pdf(payload, pdf_options={"format": "A4", "margin": {"top": ".2in", "bottom": ".5in"}})
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Exemplo completo em `examples/`.
|
|
67
|
+
|
|
68
|
+
## Payload
|
|
69
|
+
|
|
70
|
+
| Campo | Tipo | Obrigatório | Descrição |
|
|
71
|
+
| --- | --- | --- | --- |
|
|
72
|
+
| `meta` | object | sim | `title`, `sub_title`, `mode` (`"exact"`/`"gray"`), `company` |
|
|
73
|
+
| `layout` | object | não | definição hierárquica; omitir = inferido dos dados |
|
|
74
|
+
| `data` | array | sim | registros crus ou formato template (`{"fields": ..., "groups": ...}`) |
|
|
75
|
+
|
|
76
|
+
Máscaras: `str` · `int` (milhar `1.234`) · `float` (pt-BR `1.234,50`) · `date`.
|
|
77
|
+
|
|
78
|
+
## Sintaxe `.toon`
|
|
79
|
+
|
|
80
|
+
```toon
|
|
81
|
+
meta:
|
|
82
|
+
title: Pedido de Vendas
|
|
83
|
+
mode: exact
|
|
84
|
+
layout:
|
|
85
|
+
produto{Produto,str,left}
|
|
86
|
+
valor{Valor,float,right}
|
|
87
|
+
dados[1]:
|
|
88
|
+
- produto: Caneta
|
|
89
|
+
valor: 5.50
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`dados[n]` é mapeado para `data`. Ver `examples/pedido.toon`.
|
|
93
|
+
|
|
94
|
+
## API
|
|
95
|
+
|
|
96
|
+
| Função | Descrição |
|
|
97
|
+
| --- | --- |
|
|
98
|
+
| `generate_pdf(payload, pdf_options?) -> bytes` | PDF sync a partir de dict/`ReportPayload` |
|
|
99
|
+
| `agenerate_pdf(...)` | idem, async |
|
|
100
|
+
| `generate_pdf_from_toon(text, ...)` / `agenerate_pdf_from_toon` | PDF a partir de `.toon` |
|
|
101
|
+
| `render_html(payload)` / `arender_html` / `render_html_from_toon` | HTML intermediário (sem browser) |
|
|
102
|
+
| `ToonParser().parse(text)` / `parse_toon(text)` | parse `.toon` -> dict |
|
|
103
|
+
| `TabularReportService(pdf_options?)` | pipeline `execute(payload)` (Obter→Tratar→HTML→PDF) |
|
|
104
|
+
|
|
105
|
+
## Testes
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pytest -q
|
|
109
|
+
ruff check src tests
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Testes de PDF real exigem `playwright install chromium` e são pulados (`-m "not browser"`) se o browser não estiver disponível.
|
|
113
|
+
|
|
114
|
+
## Licença
|
|
115
|
+
|
|
116
|
+
MIT — ver `LICENSE`.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Exemplo: gera relatorio.pdf a partir do payload dict + pedido.toon."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from isis_report_lib import generate_pdf, generate_pdf_from_toon
|
|
6
|
+
|
|
7
|
+
HERE = Path(__file__).parent
|
|
8
|
+
|
|
9
|
+
payload = {
|
|
10
|
+
"meta": {
|
|
11
|
+
"title": "Pedido de Vendas",
|
|
12
|
+
"sub_title": "De 01/01/2024 até 31/12/2024",
|
|
13
|
+
"mode": "exact",
|
|
14
|
+
},
|
|
15
|
+
"layout": {
|
|
16
|
+
"empresa": {"label": "Empresa", "format_mask": "str", "alignment": "left"},
|
|
17
|
+
"data_entrega": {"label": "Data Entrega", "format_mask": "date", "alignment": "center"},
|
|
18
|
+
"pedidos": {
|
|
19
|
+
"pedido": {"label": "Código", "format_mask": "int", "alignment": "right"},
|
|
20
|
+
"data_pedido": {"label": "Data Pedido", "format_mask": "date", "alignment": "center"},
|
|
21
|
+
"nome_produto": {"label": "Produto", "format_mask": "str", "alignment": "left"},
|
|
22
|
+
"total_tarifa": {"label": "Total Tarifa", "format_mask": "float", "alignment": "right"},
|
|
23
|
+
"valor_total": {"label": "Valor Total", "format_mask": "float", "alignment": "right"},
|
|
24
|
+
"cargas": {
|
|
25
|
+
"qtde": {"label": "Quantidade", "format_mask": "int", "alignment": "right"},
|
|
26
|
+
"valor_unitario": {"label": "Valor Unitário", "format_mask": "float", "alignment": "right"},
|
|
27
|
+
"valor_total": {"label": "Valor Total", "format_mask": "float", "alignment": "right"},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
"data": [
|
|
32
|
+
{
|
|
33
|
+
"empresa": "CQ CONSULTORIA ADMINITRATIVA",
|
|
34
|
+
"data_entrega": "01/12/2024",
|
|
35
|
+
"pedidos": [
|
|
36
|
+
{
|
|
37
|
+
"pedido": 750,
|
|
38
|
+
"data_pedido": "01/12/2024",
|
|
39
|
+
"nome_produto": "CQ REFEIÇÃO",
|
|
40
|
+
"total_tarifa": 9.01,
|
|
41
|
+
"valor_total": 4000,
|
|
42
|
+
"cargas": [
|
|
43
|
+
{"qtde": 1, "valor_unitario": 1500, "valor_total": 1500},
|
|
44
|
+
{"qtde": 1, "valor_unitario": 1000, "valor_total": 1000},
|
|
45
|
+
{"qtde": 1, "valor_unitario": 1500, "valor_total": 1500},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def main() -> None:
|
|
55
|
+
(HERE / "relatorio.pdf").write_bytes(generate_pdf(payload))
|
|
56
|
+
print("OK:", HERE / "relatorio.pdf")
|
|
57
|
+
toon_text = (HERE / "pedido.toon").read_text(encoding="utf-8")
|
|
58
|
+
(HERE / "relatorio_toon.pdf").write_bytes(generate_pdf_from_toon(toon_text))
|
|
59
|
+
print("OK:", HERE / "relatorio_toon.pdf")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
main()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
meta:
|
|
2
|
+
company: Minha Empresa
|
|
3
|
+
title: Pedido de Vendas
|
|
4
|
+
sub_title: De 01/01/2024 até 31/12/2024
|
|
5
|
+
type: pdf
|
|
6
|
+
mode: exact
|
|
7
|
+
layout:
|
|
8
|
+
empresa{Empresa,str,left}
|
|
9
|
+
data_entrega{Data Entrega,date,center}
|
|
10
|
+
pedidos:
|
|
11
|
+
pedido{Código,int,right}
|
|
12
|
+
data_pedido{Data Pedido,date,center}
|
|
13
|
+
nome_produto{Produto,str,left}
|
|
14
|
+
total_tarifa{Total Tarifa,float,right}
|
|
15
|
+
valor_total{Valor Total,float,right}
|
|
16
|
+
cargas:
|
|
17
|
+
qtde{Quantidade,int,right}
|
|
18
|
+
valor_unitario{Valor Unitário,float,right}
|
|
19
|
+
valor_total{Valor Total,float,right}
|
|
20
|
+
dados[1]:
|
|
21
|
+
- empresa: CQ CONSULTORIA ADMINITRATIVA
|
|
22
|
+
data_entrega: 01/12/2024
|
|
23
|
+
pedidos[2]:
|
|
24
|
+
- pedido: 750
|
|
25
|
+
data_pedido: 01/12/2024
|
|
26
|
+
nome_produto: CQ REFEIÇÃO
|
|
27
|
+
total_tarifa: 9.01
|
|
28
|
+
valor_total: 4000
|
|
29
|
+
cargas[3]{qtde,valor_unitario,valor_total}:
|
|
30
|
+
1,1500,1500
|
|
31
|
+
1,1000,1000
|
|
32
|
+
1,1500,1500
|
|
33
|
+
- pedido: 8750
|
|
34
|
+
data_pedido: 01/12/2024
|
|
35
|
+
nome_produto: CQ REFEIÇÃO
|
|
36
|
+
total_tarifa: 9.01
|
|
37
|
+
valor_total: 4000
|
|
38
|
+
cargas[3]{qtde,valor_unitario,valor_total}:
|
|
39
|
+
1,1500,1500
|
|
40
|
+
1,1000,1000
|
|
41
|
+
1,1500,1500
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "isis-report-lib"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Biblioteca Python para geração de relatórios PDF tabulares com layouts hierárquicos (Jinja2 + Playwright). Extraída do POC isis_report."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = { text = "MIT" }
|
|
8
|
+
authors = [{ name = "Gil Cleber Barboza" }]
|
|
9
|
+
keywords = ["pdf", "report", "jinja2", "playwright", "toon"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"jinja2>=3.1",
|
|
17
|
+
"playwright>=1.40",
|
|
18
|
+
"pydantic>=2.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
dev = ["pytest>=8", "pytest-asyncio>=0.23", "pytest-timeout>=2", "ruff>=0.4"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/thepaneb/isis_report"
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["hatchling"]
|
|
29
|
+
build-backend = "hatchling.build"
|
|
30
|
+
|
|
31
|
+
# Layout src/ auto-detectado pelo hatchling (não declarar `packages` manualmente:
|
|
32
|
+
# isso gerava wheel vazio). Templates .jinja viajam junto por estarem no pacote.
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
asyncio_mode = "auto"
|
|
36
|
+
# Um único loop para todos os testes async: o pool do Playwright é preso ao loop,
|
|
37
|
+
# então trocar de loop entre testes causa close cross-loop (trava no Linux).
|
|
38
|
+
asyncio_default_test_loop_scope = "session"
|
|
39
|
+
markers = ["browser: requer Chromium do Playwright instalado"]
|
|
40
|
+
# Diagnóstico de hangs no CI: despeja stacks aos 90s e falha o teste aos 300s.
|
|
41
|
+
faulthandler_timeout = 90
|
|
42
|
+
timeout = 300
|
|
43
|
+
|
|
44
|
+
[tool.ruff]
|
|
45
|
+
line-length = 140
|
|
46
|
+
target-version = "py311"
|
|
47
|
+
src = ["src"]
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint]
|
|
50
|
+
select = ["E", "F", "I", "B", "UP"]
|
|
51
|
+
ignore = ["E501"]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""isis_report_lib — gere PDFs tabulares a partir de dict ou texto `.toon`."""
|
|
2
|
+
|
|
3
|
+
from isis_report_lib.api import (
|
|
4
|
+
agenerate_pdf,
|
|
5
|
+
agenerate_pdf_from_toon,
|
|
6
|
+
arender_html,
|
|
7
|
+
generate_pdf,
|
|
8
|
+
generate_pdf_from_toon,
|
|
9
|
+
render_html,
|
|
10
|
+
render_html_from_toon,
|
|
11
|
+
)
|
|
12
|
+
from isis_report_lib.formatting import format_value
|
|
13
|
+
from isis_report_lib.layout import convert_data, convert_layout, infer_layout, is_template_format
|
|
14
|
+
from isis_report_lib.models import MetaInfo, ReportPayload
|
|
15
|
+
from isis_report_lib.service import BaseReportService, TabularReportService
|
|
16
|
+
from isis_report_lib.toon import ToonParser, parse_toon
|
|
17
|
+
|
|
18
|
+
__version__ = "0.1.0"
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"MetaInfo",
|
|
22
|
+
"ReportPayload",
|
|
23
|
+
"ToonParser",
|
|
24
|
+
"parse_toon",
|
|
25
|
+
"format_value",
|
|
26
|
+
"convert_layout",
|
|
27
|
+
"convert_data",
|
|
28
|
+
"infer_layout",
|
|
29
|
+
"is_template_format",
|
|
30
|
+
"BaseReportService",
|
|
31
|
+
"TabularReportService",
|
|
32
|
+
"agenerate_pdf",
|
|
33
|
+
"generate_pdf",
|
|
34
|
+
"agenerate_pdf_from_toon",
|
|
35
|
+
"generate_pdf_from_toon",
|
|
36
|
+
"arender_html",
|
|
37
|
+
"render_html",
|
|
38
|
+
"render_html_from_toon",
|
|
39
|
+
"__version__",
|
|
40
|
+
]
|