GithubHelp home page GithubHelp logo

myback / go-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from harsh-98/go-template

0.0 0.0 0.0 1.93 MB

Python bindings for go template

License: MIT License

Python 9.07% Go 87.11% Makefile 3.82%

go-template's Introduction

Go-template

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

go_template tested in Python 3.8.

!!! Do not work in Linux Alpine

Overview

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

Quickstart

Build from sources

# install go
make lib

Get build lib

git clone https://github.com/myback/go-template.git
cd go-template
mkdir src/go_template/bind

# Download pre-build lib from https://github.com/myback/go-template/releases/

tar xf py_go_template_bind_linux_amd64.tgz -C src/go_template/bind

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.

go-template's People

Contributors

harsh-98 avatar myback avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.