Skip to main content

Content generation with Jinja templates in between comments

Project description

autojinja

autojinja is a Python3 module that allows generating code inside any file using Jinja templates in between comments. It aims to assist development workflows by using simple Python3 scripts that perform content generation of neighboring files, which enables generating code next to traditionally hand-made code efficiently.

It includes :

  • Content generation in any file that accepts comments
  • Ability to maintain hand-made code inside generated code
  • Simple integration in build pipelines and existing codebases

autojinja can be seen as a preprocessor for most file formats, and works great with any codebase as it automates writing code manually. It shines out even more when working with metadata files (such as XML or JSON files) that centralize information which later needs to be generated across various scattered files in the codebase.

Installation

$ pip install autojinja

Usage

$ autojinja script1.py script2.py
$ autojinja -a .

Content generation

Content generation consists in generating parts of an existing file thanks to dedicated sections that delimit where the generation takes place. This technique is made possible for all files accepting comments, and allows defining Jinja templates directly in such files without breaking them.

For example, let's suppose we have a C++ file we want to generate inside. To do this, we need to write our Jinja template between [[[ and ]]] markers, followed by an [[[end]]] marker :

// main.cpp
#include <iostream>

int main() {
  std::cout << "Hello world !" << std::endl;
  // [[[ std::cout << "{{ value }}" << std::endl; ]]]  // Jinja template
  // [[[ end ]]]                                       // Code will be generated in between
  return 0;
}

The template can then be generated with a Python3 script that provides the value variable :

# main.py
from autojinja import CogTemplate

template = CogTemplate.from_file("main.cpp")
template.context(value = "Python here !").render_file()

Calling the script will modify our C++ file as such :

// main.cpp
#include <iostream>

int main() {
  std::cout << "Hello world !" << std::endl;
  // [[[ std::cout << "{{ value }}" << std::endl; ]]]  // Jinja template
  std::cout << "Python here !" << std::endl;
  // [[[ end ]]]                                       // Code will be generated in between
  return 0;
}

This technique is greatly inspired by Cog, but adapted to work with Jinja templating engine. When performing generation again, the content inside the markers is entirely replaced by the new generation.

Hand-made modifications

Modification by hand of generated code is made possible by using special sections that are preserved across new generations. To do this, we need to name a section between <<[ and ]>> markers, followed by an <<[end]>> marker :

// main.cpp
#include <iostream>

// [[[
// int main() {
//
//   std::cout << "{{ value }}" << std::endl;
//   // <<[ impl ]>>  // Manually editable section named 'impl'
//   return 0;
//   // <<[ end ]>>   // End of section
//
// }
// ]]]
int main() {

  std::cout << "Python here !" << std::endl;
  // <<[ impl ]>>  // Manually editable section named 'impl'
  std::cout << "Modified by hand" << std::endl;
  return 1;
  // <<[ end ]>>   // End of section

}
// [[[ end ]]]

When a new generation occurs, all previous sections are retrieved from the destination file and then reinserted into the new generation, inside each corresponding section with same name.

All previous sections of a destination file must be reinserted when performing a new generation, otherwise it will be considered as code loss and generation will be aborted. Human intervention is required to deal with such scenarios.

Integration in build pipelines

autojinja generation mechanism can be integrated as a build step by listing all Python3 scripts that perform generation :

$ autojinja script1.py dir/script2.py ...

autojinja also provides discover mechanisms to find and execute the concerned Python3 scripts in listed directories :

  • -r -f recursively executes all Python3 scripts named __jinja__.py

    $ autojinja -r -f .
    
  • -r -t recursively executes all Python3 scripts whose first line contains autojinja keyword (can be for instance import autojinja or from autojinja import *)

    $ autojinja -r -t .
    
  • -a equivalent to -r -f -t

    $ autojinja -a .
    

Centralized metadata files (such as XML or JSON files) can easily be accessed inside Python3 scripts using environment variables and environment files :

$ autojinja -a . -e FILE1=/tmp/file1.xml -e file.env

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autojinja-1.10.0.tar.gz (47.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autojinja-1.10.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file autojinja-1.10.0.tar.gz.

File metadata

  • Download URL: autojinja-1.10.0.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for autojinja-1.10.0.tar.gz
Algorithm Hash digest
SHA256 f183ca7fb4a00c1a6b5dc783617f2b320054512f4965a689ad7bd6e8be13302d
MD5 e9cfe03c284a232c04d4f98f1258bec4
BLAKE2b-256 21d105ac6d12a4a6d7116fd1d7a5fde0c8c0c3021d61377e0e5e06fdc7c00d71

See more details on using hashes here.

File details

Details for the file autojinja-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: autojinja-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for autojinja-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bfef5a314469cc48f783239a9a92a106cc2fe7de3212fdf4061471f349d6b99
MD5 b0c57b750eedd3fbbce6c21888300dcb
BLAKE2b-256 863ac8963cee2a4ee93310e7330a4f4713382d24b0c046b1df8b780d3858fe7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page