Skip to main content

Templating Poetry Plugin

Poetry PyPI - Version PyPI - Python Version Coverage Status GitHub Workflow Status (with event)

A plugin for Poetry which lets you substitute text on build. This plugin was created to allow you to keep a single source of truth for package wide properties such as version and author.

Installation

The easiest way to install the plugin is via the self add command of Poetry.

poetry self add poetry-templating

If you used pipx to install Poetry you can add the plugin via the pipx inject command.

pipx inject poetry poetry-templating

Otherwise, if you used pip to install Poetry you can add the plugin packages via the pip install command.

pip install poetry-templating

Usage

Poetry Templating uses 'template slots' as placeholders and definitions for what they should be replaced with. Consider the following file:

__version__ = "${pyproject.tool.poetry.version}"

When evaluated, the slot will be replaced with the tool.poetry.version property from pyproject.toml, for example:

__version__ = "1.2.3"

Slots can also be used in conjunction with comments to add entire lines only present in the built package. This can be used with # templating: delete to significantly change functionality in the built package, for example:

production = false # templating: delete
# ${"production = true"}

becomes

production = true

Evaluating Templates

Poetry Templating will automatically evaluate template slots when building the package distributables with the poetry build command. You can also evaluate the project in-place by running the poetry templating evaluate command.

Constructs

Poetry Templating features a number of constructs which can be used in template slots.

Literal Construct

Denoted by quotes or double quotes, literal constructs simply replace the slot with the text within.

${"Hello World"}

becomes

Hello World

Template slots within literal constructs are also evaluated, allowing for basic string concatenation, for example:

# ${"__version__ = ${pyproject.tool.poetry.version}"}

becomes

__version__ = "1.2.3"

PyProject Construct

Allows you to refernce values from the package's pyproject.toml file.

${pyproject.tool.poetry.version}
${pyproject.tool.poetry.authors.0}

becomes

1.2.3
John Doe <john.doe@example.com

Environment Variable Construct

Allows you to reference the buildtime environment variables.

${env.HOME}

becomes

/home/example/

File Construct

Denoted with a forward slash, or ./ for relative files, the file construct gets the entire content of an arbitrary file.

${/LICENCE}

becomes

MIT License

Permission is hereby granted ...

Configuration

Poetry Templating can be configured in your pyproject.toml file under the tool.poetry-templating table.

key default description
encoding "utf-8" The text encoding to use when processing files.
include ["*.py"] A list of glob patterns for files to process.
exclude [] A list of glob patterns for files not to process, has priority over include.

Poetry Templating can also be enabled and disabled within a single file:

# templating: off
example = "${'will NOT get evaluated'}"
# templating: on
evaluated = "${'WILL get evaluated'}"

becomes

example = "${'will NOT get evaluated'}"
evaluated = "WILL get evaluated"

Metadata

Release files for poetry-templating 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for poetry-templating 1.1.2
File Size Uploaded Provenance
poetry_templating-1.1.2.tar.gz 9.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for poetry-templating 1.1.2
File Interpreter ABI Platform
poetry_templating-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.1 kB

Release files / poetry_templating-1.1.2.tar.gz

Download URL poetry_templating-1.1.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2bb794078a984e051c0ee24f3b76931ba26144b95b3c36a91667cf129da4863f
BLAKE2b-256 checksum
How to use checksums
3c0cfb0abf684a27c8ed5fbd267fba24d3640cac2a1f1957ce86e83f56614301
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / poetry_templating-1.1.2-py3-none-any.whl

Download URL poetry_templating-1.1.2-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
94a81822abd3995762243dff4f34b12624d5b2df7da3113ae334ca981364be6a
BLAKE2b-256 checksum
How to use checksums
91e9241cf7837840b1903e0fb52da16786872346ef4f4ba2994a8a0ce382eeb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.2

Security for poetry-templating 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page