peltak.core.templates

Script template reference

Using jinja filters

Let’s suppose you have a scripts that runs pytest over your project and you want to pass the verbosity flag down to the pytest command. The verbosity option can be accessed with {{ opts.verbose }} but it is an int so we need to somehow convert it to the appropriate flag -v, or -vv etc. Thankfully peltak already implements a filter to do just that, it’s called count_flag and will convert a given number N to a flag that has the given letter appear N times. Here’s a quick example.

scripts:
    test:
        about: Run tests with pytest
        command: |
            pytest {{ opts.verbose | count_flag('v') }} {{ conf.src_dir }}

This will result in the following command being invoked:

peltak run test
# will result in: pytest src

peltak run test -v
# will result in: pytest src -v

peltak run test -vv
# will result in: pytest src -vv

Template context

Peltak will inject the following context into the template.

Name

Description

conf

The entire configuration object. This is the dictionary
representation of pelconf.yaml and makes it easy to acess
any global configuration values from within a script.

opts

Script command line options. This will contains all command
line options the script was called with.

script

The script configuration as read from pelconf.yaml.
This will only contain the configuration for the currently
running script, not the entire scripts: section.

ctx

Current runtime context. This is a value store that exists
only when peltak is running and is recreated on every run.
This is a way to share runtime information between commands
(things like verbosity or pretend).

proj_path

A helper function. Given any project relative path (relative
to pelconf.yaml) it will convert it to an absolute path.

On top of all the values in the context, you can also use /reference/script_filters.

Dev Reference

class peltak.core.templates.Engine(*args, **kw)[source]

Template engine wrapper.

Engine is a singleton, which means there is always a single instance in existence during the app run. You can access this instance by calling the class initializer:

>>> from peltak.core.templates import Engine
>>>
>>> engine = Engine()

Only on first call will the class be actually constructed, all following calls will return the same instance:

>>> from peltak.core.templates import Engine
>>>
>>> Engine() is Engine()
True
render(template_str, template_ctx=None)[source]

Render a script template using the given context.

Examples

>>> from peltak.core.templates import Engine
>>>
>>> Engine().render("{{ msg | upper }}", {'msg': 'hello'})
'HELLO'
Return type

str

render_file(template_file, template_ctx=None)[source]

Render a template file from src/peltak/templates directory.

All built-in peltak templates should go to the src/peltak/templates directory.

Parameters
  • template_file (str) – The name of the template file to use. This file needs to reside in the templates directory and will be automatically loaded.

  • template_ctx (dict[str, Any]) – The template context to use. This is a dictionary of values that will be injected into the template during rendering.

Returns

A rendered template.

Return type

str

Examples

 >>> from peltak.core.templates import Engine >>> >>> output = Engine().render_file(“peltak-py.yaml”, {‘src_dir’: ‘src’}) >>> print(output) # peltak configuration file # Visit https://novopl.github.io/peltak for more information pelconf_version: ‘1’ <BLANKLINE> # You can add custom project commands or 3rd party packages here. plugins:

  • peltak.cli.git

  • peltak.cli.version

  • peltak_changelog

  • peltak_gitflow

  • peltak_todos

<BLANKLINE> cfg:

build_dir: .build python_paths: [‘src’] scripts_dir: scripts

<BLANKLINE>
clean:
include:
  • __pycache__

  • *.py[cod]’

  • *.swp’

  • *.mypy_cache’

  • *.pytest_cache’

  • *.build’

exclude:
  • ‘.venv’

<BLANKLINE>
changelog:

tag_format: ‘{tag}:’ tags:

  • tag: feature header: Features

  • tag: fix header: Fixes

  • tag: change header: Changes

  • tag: dev header: Dev tasks