Skip to content

Latest commit

ย 

History

121 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

jinja-tree

Python Badge UV Badge Mergify Badge Renovate Badge MIT Licensed

What is it?

jinja-tree is a CLI utility to process jinja (jinja2) templates recursively in a directory tree.

It is very configurable and very easy to extend with its included plugin system.

The default behavior is to recursively search for files with a given extension (.template for example) and to process the context with Jinja (Jinja2) templates engine reading the context variables from:

  • a configuration file
  • environment variables
  • dotenv files

Then, the processed content is written into another file with the same name/path but with the configured extension (.template by default) removed. The original file can also be deleted (but this is not the default behavior).

Full example about overall operation (in default mode)

Note: this is only the default behavior as you can tune this with your own plugins!

Let's imagine the following directory structure:

/foo/
/foo/README.md.template
/foo/bar/baz.py.template
/foo/bar/another.file

And execute jinja-tree /foo with the default configuration.

We get:

/foo/
/foo/README.md.template
/foo/README.md <= NEW FILE FROM README.md.template jinja2 processing
/foo/bar/baz.py.template
/foo/bar/baz.py <= NEW FILE FROM baz.py.template jinja2 processing 
/foo/bar/another.file

What's it for?

Your imagination is your limit ๐Ÿ˜… but it's very useful for maintaining DRY documentation (for example your-cli --help output automatically updated in a markdown file), configuration files with default values read in code, including common blocks in different files...

So it's a great tool for maintaining repositories in general.

Note

Another "action" plugin will be soon ๐Ÿ•’ provided to bootstrap directory trees from templates (like with the cookiecutter project).

Features

1๏ธโƒฃ Easy to extend

jinja-tree includes a plugin system. You can override the default behavior with your own plugins.

There are two extension points:

  • context plugins: to provide context variables to Jinja templates
  • file plugins: to change the way how jinja-tree finds files to process (including target files)

See this specification documentation page for more details.

2๏ธโƒฃ Very configurable

jinja-tree is very configurable. You can configure global options via CLI options or a configuration file.

Plugins are configurable via the configuration file.

See this specification documentation page for more details.

3๏ธโƒฃ Embedded extensions

jinja-tree includes some extensions to Jinja templates engine:

Usage examples

shell extension

{{ "date"|shell() }}

=> will render something like: Sun Jan 28 15:11:44 CET 2024

from_json extension

export MYENV='["foo", "bar", "baz"]'

(
    cat <<EOF
{% for item in MYENV|from_json() -%}
- {{ item }}
{% endfor %}
EOF
) | jinja-stdin

=> will render something like:

- foo
- bar
- bar

See this directory for others

4๏ธโƒฃ Full Jinja / Jinja2 support (including "includes" and "inheritance")

jinja-tree has several options for Jinja "search paths". So you can use Jinja "includes" and "inheritance" features.

Installation

pip install jinja-tree

Tip

If you want to get a better readability of jinja-tree output (colors...), you can also use pip install rich to install this optional dependency.

Usage

Main CLI

jinja-tree .

Note

The . in the previous command in the "root directory" (the directory jinja-tree will explore recursively to find files to process). You can replace it with any directory you want. By using ., you will process all files in the current directory and its subdirectories.

Main CLI options
                                                                                                                        
 Usage: jinja-tree [OPTIONS] ROOT_DIR                                                                                   
                                                                                                                        
 Process a directory tree with the Jinja / Jinja2 templating system.                                                    
                                                                                                                        
โ•ญโ”€ Arguments โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ *    root_dir      PATH  root directory [required]                                                                   โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โ•ญโ”€ Options โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ --config-file                                                               TEXT  config file path (default: first   โ”‚
โ”‚                                                                                   '.jinja-tree.toml' file found up   โ”‚
โ”‚                                                                                   from current working dir), can     โ”‚
โ”‚                                                                                   also be see with                   โ”‚
โ”‚                                                                                   JINJA_TREE_CONFIG_FILE env var     โ”‚
โ”‚                                                                                   [env var: JINJA_TREE_CONFIG_FILE]  โ”‚
โ”‚ --log-level                                                                 TEXT  log level (DEBUG, INFO, WARNING or โ”‚
โ”‚                                                                                   ERROR)                             โ”‚
โ”‚                                                                                   [default: INFO]                    โ”‚
โ”‚ --verbose                             --no-verbose                                increase verbosity of the DEBUG    โ”‚
โ”‚                                                                                   log level (note: this forces       โ”‚
โ”‚                                                                                   log-level = DEBUG)                 โ”‚
โ”‚                                                                                   [default: no-verbose]              โ”‚
โ”‚ --extra-search-path                                                         PATH  Search path to jinja (can be used  โ”‚
โ”‚                                                                                   multiple times)                    โ”‚
โ”‚ --add-cwd-to-search-path              --no-add-cwd-to-search-path                 add current working directory      โ”‚
โ”‚                                                                                   (CWD) to jinja search path         โ”‚
โ”‚ --add-root-dir-to-search-path         --no-add-root-dir-to-search-path            add root directory to jinja search โ”‚
โ”‚                                                                                   path                               โ”‚
โ”‚ --jinja-extension                                                           TEXT  jinja extension to load            โ”‚
โ”‚ --context-plugin                                                            TEXT  context plugins (full python class โ”‚
โ”‚                                                                                   path, can be used multiple times)  โ”‚
โ”‚ --action-plugin                                                             TEXT  action plugin (full python class   โ”‚
โ”‚                                                                                   path, can be used multiple times)  โ”‚
โ”‚ --strict-undefined                    --no-strict-undefined                       if set, raise an error if a        โ”‚
โ”‚                                                                                   variable does not exist in context โ”‚
โ”‚ --blank-run                           --no-blank-run                              if set, execute a blank run        โ”‚
โ”‚                                                                                   (without modifying or deleting     โ”‚
โ”‚                                                                                   anything)                          โ”‚
โ”‚                                                                                   [default: no-blank-run]            โ”‚
โ”‚ --disable-embedded-jinja-extensioโ€ฆ    --no-disable-embedded-jinja-extenโ€ฆ          disable embedded jinja extensions  โ”‚
โ”‚ --help                                                                            Show this message and exit.        โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ


Bonus CLI (if you want to process only one file but with the same behavior)

cat /path/to/your/file/to/process | jinja-stdin >/path/to/your/processed/file

or (if you want to process only a string):

$ export FOO=bar
$ echo "Hello {{FOO}}" | jinja-stdin
Hello bar
Bonus CLI options
                                                                                                                        
 Usage: jinja-stdin [OPTIONS]                                                                                           
                                                                                                                        
 Process the standard input with Jinja templating system and return the result on the standard output.                  
                                                                                                                        
โ•ญโ”€ Options โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ --config-file                                                               TEXT  config file path (default: first   โ”‚
โ”‚                                                                                   '.jinja-tree.toml' file found up   โ”‚
โ”‚                                                                                   from current working dir), can     โ”‚
โ”‚                                                                                   also be see with                   โ”‚
โ”‚                                                                                   JINJA_TREE_CONFIG_FILE env var     โ”‚
โ”‚                                                                                   [env var: JINJA_TREE_CONFIG_FILE]  โ”‚
โ”‚ --log-level                                                                 TEXT  log level (DEBUG, INFO, WARNING or โ”‚
โ”‚                                                                                   ERROR)                             โ”‚
โ”‚                                                                                   [default: INFO]                    โ”‚
โ”‚ --verbose                             --no-verbose                                increase verbosity of the DEBUG    โ”‚
โ”‚                                                                                   log level (note: this forces       โ”‚
โ”‚                                                                                   log-level = DEBUG)                 โ”‚
โ”‚                                                                                   [default: no-verbose]              โ”‚
โ”‚ --extra-search-path                                                         PATH  Search path to jinja (can be used  โ”‚
โ”‚                                                                                   multiple times)                    โ”‚
โ”‚ --add-cwd-to-search-path              --no-add-cwd-to-search-path                 add current working directory      โ”‚
โ”‚                                                                                   (CWD) to jinja search path         โ”‚
โ”‚ --jinja-extension                                                           TEXT  jinja extension to load            โ”‚
โ”‚ --context-plugin                                                            TEXT  context plugins (full python class โ”‚
โ”‚                                                                                   path, can be used multiple times)  โ”‚
โ”‚ --strict-undefined                    --no-strict-undefined                       if set, raise an error if a        โ”‚
โ”‚                                                                                   variable does not exist in context โ”‚
โ”‚ --disable-embedded-jinja-extensioโ€ฆ    --no-disable-embedded-jinja-extenโ€ฆ          disable embedded jinja extensions  โ”‚
โ”‚ --help                                                                            Show this message and exit.        โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ


About

CLI to process jinja (jinja2) templates in a directory tree

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages