Skip to main content

A jinja2-based template engine for FORTRAN projects

Project description

jin2for - Generate FORTRAN source files from jinja2 templates

pipeline status coverage report coverage report coverage report

Why

The FORTRAN programing language has not a built-in templating engine like C++ does. In concequence, it is not unusual to end-up needing to write code like this one

function sum_i2(a,b) result (c)
  implicit none
  integer(2), intent(in) :: a
  integer(2), intent(in) :: b
  integer(2) :: c
  c = a + b
end function

function sum_i4(a,b) result (c)
  implicit none
  integer(4), intent(in) :: a
  integer(4), intent(in) :: b
  integer(4) :: c
  c = a + b
end function

function sum_i8(a,b) result (c)
  implicit none
  integer(8), intent(in) :: a
  integer(8), intent(in) :: b
  integer(8) :: c
  c = a + b
end function

The standard approach to circumvent code repetition is to use pre-processor directives. However, the functionality of the preprocessor is quite limited and this approach often leads to criptic code that is difficult to read.

The goal of jin2for is to facilitate the usage of jinja2 as a more flexible preprocessor in FORTRAN projects. jinja2 is a mature, powerful, and flexible templeting engine that allows to write templates like this one:

{% for ip in [2, 4, 8] %}
function sum_i{{ip}}(a,b) result (c)
  implicit none
  integer({{ip}}), intent(in) :: a
  integer({{ip}}), intent(in) :: b
  integer({{ip}}) :: c
  c = a + b
end function
{% endfor %}

If you store this templated code in a file, say foo.t90, and compile it with jin2for

$ jin2for foo.t90

you will obtain a file foo.f90 that contains piece of FORTRAN code shown at the beginning of this README file. Internally, jin2for uses the jinja2 engine to render the templates. So, any valid jinja2 template can be processed in this way.

jin2for is environment aware

jin2for predefines a number of useful python objects that can be used as template parameters.

For instance, if we want to generate the previous sum function for all the integer types available in the installed gfortran compiler, we can use the predefined integer_types object. Create a file sum_ints.t90 containing this template:

{% for t in integer_types %}
function sum_{{t.alias}}(a,b) result (c)
  implicit none
  {{t.decl}}, intent(in) :: a
  {{t.decl}}, intent(in) :: b
  {{t.decl}} :: c
  c = a + b
end function
{% endfor %}

and compile it with

$ jin2for --generate-for gfortran sum_ints.t90

jin2for will find out for you which are the supported integer kinds of the installed gfortran compiler and render the template only for those kinds.

Installation

jin2for is a command line application written in Python 3 and distributed via pypi.org.

It can be installed using pip:

$ pip install jin2for

If you obtain any installation error, make sure that you are using Python 3, e.g.,

$ python3 -m pip install jin2for

The latest development version can be installed manually from soruce:

$ git clone https://gitlab.com/fverdugo/jin2for
$ cd jin2for
$ python3 setup.py test # optional
$ python3 setup.py install

Usage

Basic usage

The basic command line interface (CLI) ressembles to the one of the GNU and Intel compilers. For instance,

$ jin2for file1.t90 file2.t90

will "compile" (i.e., render) the jinja2 template files file1.t90 and file2.t90 into the FORTRAN source files file1.f90 and file2.f90. By default, jin2for will replace the extension of the input files to .f90 to generate the output file names.

The files file1.t90 and file2.t90 are allowed to include or import some other jinja2 templates containing, e.g., macros or definitions. If these included template files are located in a folder, namely folder/for/included/templates, different from the current working directory, jin2for has to be informed with the -I flag:

$ jin2for -I folder/for/included/templates file1.t90 file2.t90

More advanced usage

See documentation

Documentation

For the template syntax, see the official jinja2 documentation.

For the full CLI reference:

$ jin2for -h

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

jin2for-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

jin2for-0.1.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file jin2for-0.1.0.tar.gz.

File metadata

  • Download URL: jin2for-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for jin2for-0.1.0.tar.gz
Algorithm Hash digest
SHA256 481814882980452ac09d8ac41d2271ad2b3369b222faddb09a47e8b1ffea201e
MD5 5435b12ae82ebf21d69f0237b3d25028
BLAKE2b-256 f4c84859fbfc32cac39a14cab6ab1fb6e76b3e11ce3ce7958d30eead9b8adbc1

See more details on using hashes here.

File details

Details for the file jin2for-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jin2for-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for jin2for-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97eeed2bcf8ffa68c33a124457705139207bf981691b43f8732b1cd8a804fed9
MD5 9c81732e596d16a15c804ffca6ac513e
BLAKE2b-256 7c3ef2dedcec0e5e64d2d1889b0b69af259050d184a7e1595f04d36e0314fc2e

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