Skip to main content

Go-template

Based on https://github.com/harsh-98/go-template

!!! Do not work in Linux Alpine

Overview

Python bindings for go text/template with sprig and helmfile functions

Quickstart

Build from sources

go_template tested by Python 3.8+.

# install go
make lib

Example

Content of sample.tmpl

{{.Count}} items are made of {{.Material}}
  1. Render template from dict values
>>> import go_template
>>> from pathlib import Path
>>> values = {"Count": 12, "Material": "Wool"}
>>> go_template.render(Path('tests/sample.tmpl'), values)
b'12 items are made of Wool'

Content of values.yml

Count: 12
Material: Wool
  1. Render template from values.yml file
>>> import go_template
>>> from pathlib import Path
>>> go_template.render_from_values_file(Path('tests/sample.tmpl'), Path('tests/values.yml'))
b'12 items are made of Wool'

NOTE: Paths provided to render_template should either be absolute path or relative to directory where it is ran.

Build shared library

For building a fresh shared object of text/template, you must have golang^1.5 installed.

make lib

This will create template.so in the bind folder.

Motivation

Currently, there is no python package which exposes golang text/template functionality to python. And I am in the process of learning about interoperability between different languages. So, I started working on this as a learning project.

Explanation

Golang library cannot be directly used in python. Firstly, we have to compile it as shared object or archive for interoperability with C. And then create python bindings for this C object.

CPython is the original Python implementation and provides cpython API for creating python wrapper, but the wrapping code is in C. There is a library gopy which exactly uses this approach. But it works only on go1.5 and for python2.

If we want to write the wrapping code in python, there are Cython and ctypes. Ctypes allow directly importing the C library and calling functions through its interface. This project uses ctypes for calling go functions.

When a golang library is compiled as shared object, cgo handles exposing functions and data type conversion. Using ctypes, we can only modify simple data type, string, int, float, bool. I tried converting python class to golang struct, but it failed.

So, I created a golang wrapper over text/template library, which takes simple datatypes. And handles complex operation in this layer. Then a python wrapper over this layer using ctypes.

It is far from complete and doesn't use the best approach. Currently, it has only one function which takes path of template and value file. And depending on the third argument, either writes to stdout if empty or to file if given its path.

License

This project is licensed under MIT License.

Metadata

Release files for py-go-template 1.0.0

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

Built distributions (wheels)

Table of built distributions (wheels) for py-go-template 1.0.0
File Interpreter ABI Platform Actions
py_go_template-1.0.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for py_go_template-1.0.0-py3-none-win_amd64.whl
py_go_template-1.0.0-py3-none-manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for py_go_template-1.0.0-py3-none-manylinux2014_x86_64.whl
py_go_template-1.0.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for py_go_template-1.0.0-py3-none-macosx_11_0_arm64.whl

Total release size: 7.1 MB

Release files / py_go_template-1.0.0-py3-none-win_amd64.whl

Download URL py_go_template-1.0.0-py3-none-win_amd64.whl
Size 2.4 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
4991aba8d26e6ec4cddec2a99dd5e1a37c114a480934f7dd66b5f7134c61e2cb
BLAKE2b-256 checksum
How to use checksums
c95d7e83ec3b808c8faa824f96852817815c5877e8ce7f8fd600db5b13f0c6cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / py_go_template-1.0.0-py3-none-manylinux2014_x86_64.whl

Download URL py_go_template-1.0.0-py3-none-manylinux2014_x86_64.whl
Size 2.4 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
c681d3b94118276438ea2473aa8ac174a5b74d8db42c632fad92b17e0a4244ac
BLAKE2b-256 checksum
How to use checksums
a4b62d985ba1480ebae3fa81009c37a41b8a925d2f558676da7c2d8d768246e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / py_go_template-1.0.0-py3-none-macosx_11_0_arm64.whl

Download URL py_go_template-1.0.0-py3-none-macosx_11_0_arm64.whl
Size 2.3 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
43f44dc79e7480d4c144b23c77fc6fd9ba451913a570334f9a46147a04fd6334
BLAKE2b-256 checksum
How to use checksums
f2335d3486f2ace7fa8a411ce6416dc4e40f279fb00d0feb9885f633be4061b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for py-go-template 1.0.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

3 release files No provenance

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