A templating language for embedding python in source files
This is a templating solution for embedded python expressions and source in C files. This can be a clean way of injecting some compile-time logic into a program - where the python source is inline with your source - making it pretty easy to get a full understanding of whats going on.
Given a source file named file.c.tmpl
const char * number = "${{10 + 63}}"
The templater is run using: python template.py file.c.tmpl
The output file will be file.c
const char * number = "73"
All expressions within ${{...}}
are evaluated as a python expression.
All expressions within !{{...}}
are executed. Variables from an executed block can be referenced in subsequent expressions.
For example:
!{{
import subprocess
def Cmd(command):
return subprocess.check_output(command).decode('ascii').strip()
REV = Cmd("git rev-parse --short HEAD")
}}
const char * revision = "${{REV}}"
Compiles to:
const char * revision = "f6335a7"
By default the output path will be the input path minus the .tmpl
file extention. This can also be passed explicitly:
python template.py file.c.tmpl output/file.c