postext 1.78.0__tar.gz → 1.80.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.
- {postext-1.78.0 → postext-1.80.0}/PKG-INFO +23 -6
- {postext-1.78.0 → postext-1.80.0}/README.md +22 -5
- {postext-1.78.0 → postext-1.80.0}/src/postext/__init__.py +16 -2
- postext-1.80.0/src/postext/_args.py +221 -0
- postext-1.80.0/src/postext/_client.py +328 -0
- postext-1.80.0/src/postext/_design_idml.py +122 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/_report.py +49 -0
- postext-1.80.0/src/postext/_runner.py +175 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/_version.py +1 -1
- postext-1.80.0/tests/test_args.py +224 -0
- {postext-1.78.0 → postext-1.80.0}/tests/test_commands.py +32 -2
- postext-1.78.0/src/postext/_client.py +0 -629
- postext-1.78.0/tests/test_args.py +0 -89
- {postext-1.78.0 → postext-1.80.0}/.gitignore +0 -0
- {postext-1.78.0 → postext-1.80.0}/LICENSE +0 -0
- {postext-1.78.0 → postext-1.80.0}/pyproject.toml +0 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/__main__.py +0 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/_binary.py +0 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/_errors.py +0 -0
- {postext-1.78.0 → postext-1.80.0}/src/postext/py.typed +0 -0
- {postext-1.78.0 → postext-1.80.0}/tests/conftest.py +0 -0
- {postext-1.78.0 → postext-1.80.0}/tests/test_binary.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: postext
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.80.0
|
|
4
4
|
Summary: Typeset books from Python with Postext: PDF, HTML, EPUB, page images and Word from a .postext book or Markdown, through the self-contained postext command line.
|
|
5
5
|
Project-URL: Homepage, https://postext.dev/
|
|
6
6
|
Project-URL: Documentation, https://postext.dev/en/docs/command-line#python
|
|
@@ -28,8 +28,8 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
# postext for Python
|
|
29
29
|
|
|
30
30
|
Typeset books from Python with [Postext](https://postext.dev/): a print-ready
|
|
31
|
-
PDF, HTML pages, an EPUB, page images
|
|
32
|
-
book, a book folder or loose Markdown files.
|
|
31
|
+
PDF, HTML pages, an EPUB, page images, a Word document or an InDesign package
|
|
32
|
+
from a `.postext` book, a book folder or loose Markdown files.
|
|
33
33
|
|
|
34
34
|
```bash
|
|
35
35
|
pip install postext
|
|
@@ -84,10 +84,13 @@ Every command of the command line is a function. Each runs the command with
|
|
|
84
84
|
| `images(book, out="pages/", pages=…, pattern=…)` | `postext images` | `Report` |
|
|
85
85
|
| `docx(book, out=…, variant=…)` | `postext docx` | `Report` |
|
|
86
86
|
| `import_docx(docx, book=None, out=…, dry_run=…)` | `postext import-docx` | `ImportDocxReport` |
|
|
87
|
+
| `idml(book, out=…, images=…, lines=…, spreads=…, fonts=…)` | `postext idml` | `Report` |
|
|
88
|
+
| `import_idml(idml, out=…, layout=…, links=…, document=…)` | `postext import-idml` | `ImportIdmlReport` |
|
|
87
89
|
| `pack(book, out=…)` / `unpack(bundle, out=…)` | `postext pack` / `unpack` | `Report` / `UnpackReport` |
|
|
90
|
+
| `design(book, out=…, parts=…, design_name=…)` | `postext design` | `DesignReport` |
|
|
88
91
|
| `info(book, pages=True)` | `postext info` | `InfoReport` |
|
|
89
92
|
| `check(book, strict=True)` | `postext check` | `CheckReport` |
|
|
90
|
-
| `build(book, pdf=…, html=…, epub=…, images=…, docx=…)` | `postext build` | `Report` |
|
|
93
|
+
| `build(book, pdf=…, html=…, epub=…, images=…, docx=…, idml=…)` | `postext build` | `Report` |
|
|
91
94
|
|
|
92
95
|
`book` is a path (a `.postext` file, a book folder, a Markdown file), a list
|
|
93
96
|
of Markdown files (the chapters, in order), or the bytes of a `.postext`
|
|
@@ -105,9 +108,19 @@ postext.pdf(
|
|
|
105
108
|
resources="img/", fonts="fonts/", # --resources, --fonts
|
|
106
109
|
font_dirs=["~/Fonts"], # --font-dir
|
|
107
110
|
offline=True, # --offline: never download fonts
|
|
111
|
+
design="novel.postext", # --design: a design file (a path or bytes), or another book
|
|
112
|
+
design_parts=["page", "text"], # --design-parts page,text
|
|
113
|
+
composition="auto", # --composition: the composition rules of the book's language (or "es", "en-GB"…)
|
|
108
114
|
)
|
|
109
115
|
```
|
|
110
116
|
|
|
117
|
+
`design(book, out="novel.postext", parts=["page", "text", "headings"])`
|
|
118
|
+
takes a book's design into a design file, and `design=` puts it on any
|
|
119
|
+
other book. In `idml`, `fonts=True` is the switch that writes the font
|
|
120
|
+
files beside the package, not the book option. `import_idml` reads a bare
|
|
121
|
+
`.idml` or a zipped InDesign package, as a path or as bytes (then give
|
|
122
|
+
`out`, and `links` for the linked pictures).
|
|
123
|
+
|
|
111
124
|
### Reports and warnings
|
|
112
125
|
|
|
113
126
|
A `Report` has `ok`, `command`, `version`, `exit_code`, `pages`, `book`
|
|
@@ -115,13 +128,17 @@ A `Report` has `ok`, `command`, `version`, `exit_code`, `pages`, `book`
|
|
|
115
128
|
pages), `warnings` (`Diagnostic`: kind, message, severity, at, page, data),
|
|
116
129
|
`timings` and `raw`, the whole JSON object. `InfoReport` adds `chapters`,
|
|
117
130
|
`resources` and `fonts`; `CheckReport` adds `passed`, `error_count`,
|
|
118
|
-
`warning_count`, `contrast`, `reflow` and `preflight
|
|
131
|
+
`warning_count`, `contrast`, `reflow` and `preflight`; `DesignReport` adds
|
|
132
|
+
`name` and `parts`; `ImportIdmlReport` adds `layout` (`pages` or `flow`),
|
|
133
|
+
`reason`, `chapters`, `quality` (the import report), `fonts`,
|
|
134
|
+
`missing_links` and `package`.
|
|
119
135
|
|
|
120
136
|
### Bytes out
|
|
121
137
|
|
|
122
138
|
`out="-"` (or `postext.STDOUT`) writes to stdout and returns the bytes in
|
|
123
139
|
`report.content`; `pdf_bytes`, `html_bytes`, `epub_bytes`, `image_bytes`,
|
|
124
|
-
`docx_bytes` and `
|
|
140
|
+
`docx_bytes`, `idml_bytes`, `pack_bytes` and `design_bytes` return them
|
|
141
|
+
directly (`idml` with linked pictures or `fonts=True` needs a file). A book given as bytes
|
|
125
142
|
with no `out` comes back as bytes.
|
|
126
143
|
|
|
127
144
|
### Errors
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# postext for Python
|
|
2
2
|
|
|
3
3
|
Typeset books from Python with [Postext](https://postext.dev/): a print-ready
|
|
4
|
-
PDF, HTML pages, an EPUB, page images
|
|
5
|
-
book, a book folder or loose Markdown files.
|
|
4
|
+
PDF, HTML pages, an EPUB, page images, a Word document or an InDesign package
|
|
5
|
+
from a `.postext` book, a book folder or loose Markdown files.
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
pip install postext
|
|
@@ -57,10 +57,13 @@ Every command of the command line is a function. Each runs the command with
|
|
|
57
57
|
| `images(book, out="pages/", pages=…, pattern=…)` | `postext images` | `Report` |
|
|
58
58
|
| `docx(book, out=…, variant=…)` | `postext docx` | `Report` |
|
|
59
59
|
| `import_docx(docx, book=None, out=…, dry_run=…)` | `postext import-docx` | `ImportDocxReport` |
|
|
60
|
+
| `idml(book, out=…, images=…, lines=…, spreads=…, fonts=…)` | `postext idml` | `Report` |
|
|
61
|
+
| `import_idml(idml, out=…, layout=…, links=…, document=…)` | `postext import-idml` | `ImportIdmlReport` |
|
|
60
62
|
| `pack(book, out=…)` / `unpack(bundle, out=…)` | `postext pack` / `unpack` | `Report` / `UnpackReport` |
|
|
63
|
+
| `design(book, out=…, parts=…, design_name=…)` | `postext design` | `DesignReport` |
|
|
61
64
|
| `info(book, pages=True)` | `postext info` | `InfoReport` |
|
|
62
65
|
| `check(book, strict=True)` | `postext check` | `CheckReport` |
|
|
63
|
-
| `build(book, pdf=…, html=…, epub=…, images=…, docx=…)` | `postext build` | `Report` |
|
|
66
|
+
| `build(book, pdf=…, html=…, epub=…, images=…, docx=…, idml=…)` | `postext build` | `Report` |
|
|
64
67
|
|
|
65
68
|
`book` is a path (a `.postext` file, a book folder, a Markdown file), a list
|
|
66
69
|
of Markdown files (the chapters, in order), or the bytes of a `.postext`
|
|
@@ -78,9 +81,19 @@ postext.pdf(
|
|
|
78
81
|
resources="img/", fonts="fonts/", # --resources, --fonts
|
|
79
82
|
font_dirs=["~/Fonts"], # --font-dir
|
|
80
83
|
offline=True, # --offline: never download fonts
|
|
84
|
+
design="novel.postext", # --design: a design file (a path or bytes), or another book
|
|
85
|
+
design_parts=["page", "text"], # --design-parts page,text
|
|
86
|
+
composition="auto", # --composition: the composition rules of the book's language (or "es", "en-GB"…)
|
|
81
87
|
)
|
|
82
88
|
```
|
|
83
89
|
|
|
90
|
+
`design(book, out="novel.postext", parts=["page", "text", "headings"])`
|
|
91
|
+
takes a book's design into a design file, and `design=` puts it on any
|
|
92
|
+
other book. In `idml`, `fonts=True` is the switch that writes the font
|
|
93
|
+
files beside the package, not the book option. `import_idml` reads a bare
|
|
94
|
+
`.idml` or a zipped InDesign package, as a path or as bytes (then give
|
|
95
|
+
`out`, and `links` for the linked pictures).
|
|
96
|
+
|
|
84
97
|
### Reports and warnings
|
|
85
98
|
|
|
86
99
|
A `Report` has `ok`, `command`, `version`, `exit_code`, `pages`, `book`
|
|
@@ -88,13 +101,17 @@ A `Report` has `ok`, `command`, `version`, `exit_code`, `pages`, `book`
|
|
|
88
101
|
pages), `warnings` (`Diagnostic`: kind, message, severity, at, page, data),
|
|
89
102
|
`timings` and `raw`, the whole JSON object. `InfoReport` adds `chapters`,
|
|
90
103
|
`resources` and `fonts`; `CheckReport` adds `passed`, `error_count`,
|
|
91
|
-
`warning_count`, `contrast`, `reflow` and `preflight
|
|
104
|
+
`warning_count`, `contrast`, `reflow` and `preflight`; `DesignReport` adds
|
|
105
|
+
`name` and `parts`; `ImportIdmlReport` adds `layout` (`pages` or `flow`),
|
|
106
|
+
`reason`, `chapters`, `quality` (the import report), `fonts`,
|
|
107
|
+
`missing_links` and `package`.
|
|
92
108
|
|
|
93
109
|
### Bytes out
|
|
94
110
|
|
|
95
111
|
`out="-"` (or `postext.STDOUT`) writes to stdout and returns the bytes in
|
|
96
112
|
`report.content`; `pdf_bytes`, `html_bytes`, `epub_bytes`, `image_bytes`,
|
|
97
|
-
`docx_bytes` and `
|
|
113
|
+
`docx_bytes`, `idml_bytes`, `pack_bytes` and `design_bytes` return them
|
|
114
|
+
directly (`idml` with linked pictures or `fonts=True` needs a file). A book given as bytes
|
|
98
115
|
with no `out` comes back as bytes.
|
|
99
116
|
|
|
100
117
|
### Errors
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""Postext from Python.
|
|
2
2
|
|
|
3
3
|
Typesets books with the self-contained ``postext`` command line: PDF, HTML,
|
|
4
|
-
EPUB, page images and
|
|
5
|
-
Markdown. The executable of this system is downloaded from the GitHub
|
|
4
|
+
EPUB, page images, Word and InDesign (IDML) from a ``.postext`` book, a book
|
|
5
|
+
folder or loose Markdown. The executable of this system is downloaded from the GitHub
|
|
6
6
|
Release of this package's version on first use, checked against its SHA256
|
|
7
7
|
and cached; ``POSTEXT_BIN`` points at one already installed.
|
|
8
8
|
|
|
@@ -27,10 +27,12 @@ from ._report import (
|
|
|
27
27
|
BookSummary,
|
|
28
28
|
ChapterInfo,
|
|
29
29
|
CheckReport,
|
|
30
|
+
DesignReport,
|
|
30
31
|
Diagnostic,
|
|
31
32
|
FontInfo,
|
|
32
33
|
ImageReport,
|
|
33
34
|
ImportDocxReport,
|
|
35
|
+
ImportIdmlReport,
|
|
34
36
|
InfoReport,
|
|
35
37
|
Output,
|
|
36
38
|
PageRef,
|
|
@@ -47,8 +49,11 @@ epub = _default.epub
|
|
|
47
49
|
image = _default.image
|
|
48
50
|
images = _default.images
|
|
49
51
|
docx = _default.docx
|
|
52
|
+
idml = _default.idml
|
|
50
53
|
import_docx = _default.import_docx
|
|
54
|
+
import_idml = _default.import_idml
|
|
51
55
|
pack = _default.pack
|
|
56
|
+
design = _default.design
|
|
52
57
|
unpack = _default.unpack
|
|
53
58
|
info = _default.info
|
|
54
59
|
check = _default.check
|
|
@@ -59,7 +64,9 @@ html_bytes = _default.html_bytes
|
|
|
59
64
|
epub_bytes = _default.epub_bytes
|
|
60
65
|
image_bytes = _default.image_bytes
|
|
61
66
|
docx_bytes = _default.docx_bytes
|
|
67
|
+
idml_bytes = _default.idml_bytes
|
|
62
68
|
pack_bytes = _default.pack_bytes
|
|
69
|
+
design_bytes = _default.design_bytes
|
|
63
70
|
|
|
64
71
|
run = _default.run
|
|
65
72
|
|
|
@@ -82,8 +89,11 @@ __all__ = [
|
|
|
82
89
|
"image",
|
|
83
90
|
"images",
|
|
84
91
|
"docx",
|
|
92
|
+
"idml",
|
|
85
93
|
"import_docx",
|
|
94
|
+
"import_idml",
|
|
86
95
|
"pack",
|
|
96
|
+
"design",
|
|
87
97
|
"unpack",
|
|
88
98
|
"info",
|
|
89
99
|
"check",
|
|
@@ -93,7 +103,9 @@ __all__ = [
|
|
|
93
103
|
"epub_bytes",
|
|
94
104
|
"image_bytes",
|
|
95
105
|
"docx_bytes",
|
|
106
|
+
"idml_bytes",
|
|
96
107
|
"pack_bytes",
|
|
108
|
+
"design_bytes",
|
|
97
109
|
"run",
|
|
98
110
|
# executable
|
|
99
111
|
"binary",
|
|
@@ -108,7 +120,9 @@ __all__ = [
|
|
|
108
120
|
"CheckReport",
|
|
109
121
|
"ImageReport",
|
|
110
122
|
"ImportDocxReport",
|
|
123
|
+
"ImportIdmlReport",
|
|
111
124
|
"UnpackReport",
|
|
125
|
+
"DesignReport",
|
|
112
126
|
"Output",
|
|
113
127
|
"Diagnostic",
|
|
114
128
|
"BookSummary",
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""The arguments of the command line, built from Python values, and the
|
|
2
|
+
reading of what the executable prints back."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import contextlib
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import tempfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import (
|
|
12
|
+
TYPE_CHECKING,
|
|
13
|
+
Any,
|
|
14
|
+
Dict,
|
|
15
|
+
Iterable,
|
|
16
|
+
List,
|
|
17
|
+
Mapping,
|
|
18
|
+
Optional,
|
|
19
|
+
Sequence,
|
|
20
|
+
Tuple,
|
|
21
|
+
Union,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from typing_extensions import TypedDict
|
|
26
|
+
|
|
27
|
+
from ._report import Report
|
|
28
|
+
|
|
29
|
+
PathLike = Union[str, "os.PathLike[str]"]
|
|
30
|
+
#: A book: a ``.postext`` file, a book folder, a Markdown file, or the
|
|
31
|
+
#: bytes of a ``.postext`` bundle.
|
|
32
|
+
BookInput = Union[PathLike, bytes]
|
|
33
|
+
Books = Union[BookInput, Sequence[BookInput]]
|
|
34
|
+
Chapters = Union[int, str, range, Sequence[Union[int, str, range]]]
|
|
35
|
+
Settings = Union[Mapping[str, Any], Sequence[str]]
|
|
36
|
+
|
|
37
|
+
#: ``out="-"`` (or ``STDOUT``): the command writes to stdout and the bytes
|
|
38
|
+
#: come back in ``Report.content``.
|
|
39
|
+
STDOUT = "-"
|
|
40
|
+
|
|
41
|
+
if TYPE_CHECKING:
|
|
42
|
+
|
|
43
|
+
class IdmlOptions(TypedDict, total=False):
|
|
44
|
+
"""The book options but ``fonts``, which ``idml`` takes as a switch of its own."""
|
|
45
|
+
|
|
46
|
+
locale: str
|
|
47
|
+
"""Language to read a multilingual book in (``es``, ``en``, ``pt-BR``…)."""
|
|
48
|
+
chapters: Chapters
|
|
49
|
+
"""Chapters to lay out, from 1: ``3``, ``"1,3-5"``, ``[1, 3]``, ``range(1, 4)``."""
|
|
50
|
+
set: Settings
|
|
51
|
+
"""Settings to change: ``{"page.dpi": 300, "bodyText.fontSize.value": 11}``."""
|
|
52
|
+
design: BookInput
|
|
53
|
+
"""A design file (``.postext`` from ``design`` or the Sandbox), or another book, whose design the book is set in."""
|
|
54
|
+
design_parts: Union[str, Sequence[str]]
|
|
55
|
+
"""With ``design``: only these parts, ``"page,text,headings"`` or ``["page", "text"]``."""
|
|
56
|
+
composition: str
|
|
57
|
+
"""A language's composition rules, applied after ``design``: ``"es"``, ``"en-GB"``…, or ``"auto"`` for the book's language."""
|
|
58
|
+
config: Union[PathLike, Mapping[str, Any]]
|
|
59
|
+
"""Configuration JSON (a file, or a dict) for loose Markdown, or merged over a book's."""
|
|
60
|
+
resources: PathLike
|
|
61
|
+
"""Pictures folder for loose Markdown."""
|
|
62
|
+
name: str
|
|
63
|
+
"""Book title for loose Markdown."""
|
|
64
|
+
font_dirs: Union[PathLike, Sequence[PathLike]]
|
|
65
|
+
"""Extra folders to look for fonts the book does not carry."""
|
|
66
|
+
offline: bool
|
|
67
|
+
"""Never download fonts from Google Fonts."""
|
|
68
|
+
cache_dir: PathLike
|
|
69
|
+
"""Where downloaded fonts are kept."""
|
|
70
|
+
|
|
71
|
+
class BookOptions(IdmlOptions, total=False):
|
|
72
|
+
"""Options of every command that reads a book."""
|
|
73
|
+
|
|
74
|
+
fonts: PathLike
|
|
75
|
+
"""Font files bundled with loose Markdown."""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
BOOK_KEYS = (
|
|
79
|
+
"locale",
|
|
80
|
+
"chapters",
|
|
81
|
+
"set",
|
|
82
|
+
"design",
|
|
83
|
+
"design_parts",
|
|
84
|
+
"composition",
|
|
85
|
+
"config",
|
|
86
|
+
"resources",
|
|
87
|
+
"fonts",
|
|
88
|
+
"name",
|
|
89
|
+
"font_dirs",
|
|
90
|
+
"offline",
|
|
91
|
+
"cache_dir",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def chapters_arg(value: Chapters) -> str:
|
|
96
|
+
def one(v: Union[int, str, range]) -> str:
|
|
97
|
+
if isinstance(v, range):
|
|
98
|
+
if v.step != 1 or len(v) == 0:
|
|
99
|
+
raise ValueError(f"chapters: {v!r} is not a run of chapters")
|
|
100
|
+
return f"{v.start}-{v.stop - 1}" if len(v) > 1 else str(v.start)
|
|
101
|
+
return str(v)
|
|
102
|
+
|
|
103
|
+
if isinstance(value, (int, str, range)):
|
|
104
|
+
return one(value)
|
|
105
|
+
return ",".join(one(v) for v in value)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def parts_arg(value: Union[str, Sequence[str]]) -> str:
|
|
109
|
+
"""Design parts: ``"page,text"`` as given, a list joined with commas."""
|
|
110
|
+
return value if isinstance(value, str) else ",".join(value)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def setting_arg(value: Any) -> str:
|
|
114
|
+
"""A ``--set`` value: the command line reads it as JSON when it parses
|
|
115
|
+
(numbers, booleans, lists, objects), else as text."""
|
|
116
|
+
if isinstance(value, str):
|
|
117
|
+
return value
|
|
118
|
+
return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def settings_args(value: Settings) -> List[str]:
|
|
122
|
+
if isinstance(value, Mapping):
|
|
123
|
+
return [f"{k}={setting_arg(v)}" for k, v in value.items()]
|
|
124
|
+
if isinstance(value, str):
|
|
125
|
+
return [value]
|
|
126
|
+
return [str(v) for v in value]
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def flag(name: str, value: Optional[bool]) -> List[str]:
|
|
130
|
+
if value is None:
|
|
131
|
+
return []
|
|
132
|
+
return [f"--{name}" if value else f"--no-{name}"]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def opt(name: str, value: Any) -> List[str]:
|
|
136
|
+
if value is None:
|
|
137
|
+
return []
|
|
138
|
+
if isinstance(value, bool):
|
|
139
|
+
return flag(name, value)
|
|
140
|
+
return [f"--{name}", os.fspath(value) if isinstance(value, os.PathLike) else str(value)]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def many(value: Union[PathLike, Sequence[PathLike], None]) -> List[str]:
|
|
144
|
+
if value is None:
|
|
145
|
+
return []
|
|
146
|
+
if isinstance(value, (str, os.PathLike)):
|
|
147
|
+
return [os.fspath(value)]
|
|
148
|
+
return [os.fspath(v) for v in value]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def split_stdout(stdout: bytes, want_content: bool) -> Tuple[Optional[Dict[str, Any]], Optional[bytes]]:
|
|
152
|
+
"""The JSON report and, with ``-o -``, the bytes before it. The report is
|
|
153
|
+
the last thing on stdout and opens with ``{\\n "ok": ``; no nested key
|
|
154
|
+
sits at that indentation, so its last occurrence is the report's start."""
|
|
155
|
+
start = stdout.rfind(b'{\n "ok": ') if want_content else 0
|
|
156
|
+
if want_content and start == -1:
|
|
157
|
+
return None, stdout
|
|
158
|
+
text = stdout[start:].decode("utf-8", "replace").strip()
|
|
159
|
+
if not text:
|
|
160
|
+
return None, stdout[:start] if want_content else None
|
|
161
|
+
try:
|
|
162
|
+
report = json.loads(text)
|
|
163
|
+
except ValueError:
|
|
164
|
+
return None, stdout if want_content else None
|
|
165
|
+
return (report if isinstance(report, dict) else None), (stdout[:start] if want_content else None)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def stderr_error(stderr: str) -> str:
|
|
169
|
+
lines = [ln for ln in stderr.splitlines() if ln.strip()]
|
|
170
|
+
errors = [ln[6:] if ln.startswith("error ") else ln for ln in lines if ln.startswith("error")]
|
|
171
|
+
return "; ".join(errors) or (lines[-1] if lines else "postext failed")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def content(report: "Report") -> bytes:
|
|
175
|
+
return report.content or b""
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class Temp(contextlib.AbstractContextManager): # type: ignore[type-arg]
|
|
179
|
+
"""A temporary folder for the books given as bytes and the settings
|
|
180
|
+
given as dicts, removed after the run."""
|
|
181
|
+
|
|
182
|
+
def __init__(self) -> None:
|
|
183
|
+
self._dir: "Optional[tempfile.TemporaryDirectory[str]]" = None
|
|
184
|
+
self._count = 0
|
|
185
|
+
self.used = False
|
|
186
|
+
|
|
187
|
+
def write(self, name: str, data: bytes) -> str:
|
|
188
|
+
if self._dir is None:
|
|
189
|
+
self._dir = tempfile.TemporaryDirectory(prefix="postext-")
|
|
190
|
+
self._count += 1
|
|
191
|
+
path = Path(self._dir.name) / f"{self._count}-{name}"
|
|
192
|
+
path.write_bytes(data)
|
|
193
|
+
return str(path)
|
|
194
|
+
|
|
195
|
+
def file(self, value: Union[PathLike, bytes], name: str) -> str:
|
|
196
|
+
"""A path as given, or bytes written to a file called ``name``
|
|
197
|
+
(which does not count as a book given as bytes)."""
|
|
198
|
+
if isinstance(value, (bytes, bytearray)):
|
|
199
|
+
return self.write(name, bytes(value))
|
|
200
|
+
return os.fspath(value)
|
|
201
|
+
|
|
202
|
+
def inputs(self, books: Books, suffix: str) -> List[str]:
|
|
203
|
+
items: Iterable[BookInput]
|
|
204
|
+
if isinstance(books, (str, bytes, bytearray, os.PathLike)):
|
|
205
|
+
items = [books]
|
|
206
|
+
else:
|
|
207
|
+
items = books
|
|
208
|
+
out: List[str] = []
|
|
209
|
+
for item in items:
|
|
210
|
+
if isinstance(item, (bytes, bytearray)):
|
|
211
|
+
self.used = True
|
|
212
|
+
out.append(self.write(f"book{suffix}", bytes(item)))
|
|
213
|
+
else:
|
|
214
|
+
out.append(os.fspath(item))
|
|
215
|
+
if not out:
|
|
216
|
+
raise ValueError("no book given")
|
|
217
|
+
return out
|
|
218
|
+
|
|
219
|
+
def __exit__(self, *exc: Any) -> None:
|
|
220
|
+
if self._dir is not None:
|
|
221
|
+
self._dir.cleanup()
|