Python API reference#
The public API is importable from the top-level package:
from ymprint import (
Ymprint, default_app,
RenderEvent, RenderCallbackError,
BlockRegistry, BlockExistsError, parse_block_key,
register_loader, register_serializer, register_decoder, RenderRun,
)
For a walkthrough, see Custom applications.
Ymprint#
Ymprint(
name: str = "ym",
*,
config_path: str | Path | None = None,
builtin_blocks: bool = True,
starter_document: str = STARTER_DOCUMENT,
)
Parameter |
Meaning |
|---|---|
|
The application’s name, used as its CLI name. |
|
The application’s config file or directory (house style). Layered above YMPrint’s defaults and below the user’s config. |
|
Start from a copy of the built-in blocks ( |
|
The document |
Member |
Description |
|---|---|
|
This application’s |
|
Adds a block. |
|
Decorator form of |
|
Registers |
|
Registers |
|
Registers |
|
Renders a path or dict to a PDF, once; returns the |
|
Renders a document as its |
|
The renders |
|
Runs live mode; blocks until Ctrl+C and returns an exit code. |
|
The config layers a render would use, lowest priority first. |
|
The project root for a document in |
|
Returns a new Typer app with |
render() arguments#
Argument |
Meaning |
|---|---|
|
A YAML document path, or the document as a |
|
Output PDF. Defaults to the source path with |
|
An explicit config file or directory (or a sequence of them, lowest priority first). The highest-priority config layer. |
|
Where relative paths in the document resolve. Defaults to the source’s directory, or the current directory for a dict. |
|
Search |
|
Passed through to the hooks on the |
|
|
|
Variables layered over the document’s |
|
The parameter set this render is for: |
render_all() and RenderRun#
render_all renders a document as its _render
entry in the project config says. With a for_each, the document is read once and
rendered once per parameter set; a failing set doesn’t stop the others. Without one, it
renders once (to the entry’s output, destination, or next to the source) and errors
propagate as from render().
|
Meaning |
|---|---|
|
The document. |
|
Its |
|
The renders attempted: |
|
The |
|
|
|
Every file read, by |
|
The key of each result, in order. |
|
The document’s exports: its |
|
The export files written to the |
|
Why exports were not written, when they weren’t. |
|
|
default_app#
The Ymprint instance behind the ym command. ymprint.report_reader.load_report(src, dest, config, vars=None) renders with it, using exactly the config given (no discovery),
and returns the RenderEvent.
RenderEvent#
A dataclass passed to every hook. Its fields are app, source, destination,
base_dir, config_paths, trigger, mode, vars, params and project_root, plus the
initial property. After the content is rendered, inputs holds the files the document
read (its _data sources, _loadjson files, _img images and
PDF backgrounds), and exports holds its _export values
(or None). pdf is the PDF written. An exception raised by a render carries its event as
render_event, with inputs holding the files read before it failed.
See The RenderEvent.
RenderCallbackError#
Raised by render() when a hook raises anything other than a YmprintAuthoringError.
Attribute |
Meaning |
|---|---|
|
|
|
The hook that raised. |
|
The original exception. |
BlockRegistry#
A mapping of block codes to converter functions.
Member |
Description |
|---|---|
|
Adds a block. Raises |
|
Decorator form of |
|
The converter for |
|
The registered codes. |
|
An independent copy. |
|
Membership and size. |
A converter has the signature convert(block_key, block_value, context) -> list[Flowable].
Reserved codes, which are handled by YMPrint itself: _ul, _ol, _textstyle, _slide,
_frame, _vars, _style, _doc, _tablestyle, _data, _export.
parse_block_key(key)#
Splits a document key into (code, id), or returns None if it isn’t block-shaped:
parse_block_key("_beam_b1") # ("_beam", "b1")
parse_block_key("_beam") # ("_beam", None)
parse_block_key("_beamx") # ("_beamx", None) — a different code, not `_beam`
parse_block_key("Heading") # None
Plugins#
register_loader(name, loader, *, extensions=(), override=False)#
Registers loader(path) -> data for _data entries that give
loader: <name>. extensions (for example [".toml"]) makes it the default for files with
those suffixes. The built-in loaders are yaml and json.
register_serializer(type, serializer)#
Registers serializer(value) -> JSON-native data for exported values of type (and its
subclasses). Later registrations take precedence. Built in: JSON-native types, paths, sets,
and real numbers (including numpy’s). Any other object is an error, even one with
__float__: a units quantity’s float() can be a scaled display value, which would be
written as a wrong number.
register_decoder(tag, decoder)#
Registers decoder(mapping) -> value for JSON mappings tagged {"$type": tag, ...}
(ymprint.exports.TYPE_KEY). A serializer that returns such a mapping, together with a
decoder for its tag, makes a type round-trip: a document or script reading the data with
_data gets the value back, not a dict. JSON read with the
built-in json loader is decoded, and a mapping with an unregistered tag stays a plain dict.
Installed plugins#
A package extends YMPrint when it is installed by naming a setup function in the
ymprint.plugins entry-point group. YMPrint calls each setup function once, with no
arguments, before it first needs a loader or serializer; the function registers what the
package provides.
# the plugin's pyproject.toml
[project.entry-points."ymprint.plugins"]
units = "my_package.ymprint_plugin:setup"
# my_package/ymprint_plugin.py
from ymprint import register_loader
def setup():
register_loader("yaml+units", load_yaml_with_quantities)
ymprint.project#
For scripts that are steps of a project build.
Function |
Meaning |
|---|---|
|
A fresh copy of data source |
|
The file data source |
|
Writes |
ymprint.build.Build#
Build(app, start=None, *, force=False, dry_run=False, report=None) is the engine behind
ym build. run(targets=()) returns a BuildSummary whose results are StepResults
(step, status, reasons, seconds, detail, error, failures) in the order the
steps finished; report(result) is called as each finishes.