Skip to main content

Command-line utility that validates jinja2 syntax according to Arista's AVD style guide.

Project description

GitHub license PyPI version fury.io PyPI pyversions PyPI status Maintenance

Jinja2-Linter

AVD Ecosystem - Jinja2 Linter

Project Goals

Build a Jinja2 linter that will provide the following capabilities:

  • Validate syntax according to AVD style guide.
  • Capability to run as part of a CI pipeline to enforce j2lint rules.
  • Develop an extension that works with VSCode and potentially other IDEs i.e PyCharm.

Syntax and code style issues

Code Short Description Description
S0 jinja-syntax-error Jinja2 syntax should be correct
S1 single-space-decorator A single space should be added between Jinja2 curly brackets and a variable's name
S2 operator-enclosed-by-spaces When variables are used in combination with an operator, the operator shall be enclosed by space
S3 jinja-statements-indentation Nested jinja code block should follow next rules:
- All J2 statements must be enclosed by 1 space
- All J2 statements must be indented by 4 more spaces within jinja delimiter
- To close a control, end tag must have same indentation level
S4 jinja-statements-single-space Jinja statement should have at least a single space after '{%' and a single space before '%}'
S5 jinja-statements-no-tabs Indentation should not use tabulation but 4 spaces
S6 jinja-statements-delimiter Jinja statements should not have {%- or {%+ or -%} as delimiters
S7 single-statement-per-line Jinja statements should be on separate lines
V1 jinja-variable-lower-case All variables should use lower case
V2 jinja-variable-format If variable is multi-words, underscore _ should be used as a separator

Getting Started

Requirements

Minimum Python version: 3.9

Install with pip

To get started, you can use Python pip to install j2lint:

Install the latest stable version:

pip3 install j2lint

Install the latest development version:

pip3 install git+https://github.com/aristanetworks/j2lint.git

Running the linter

j2lint <path-to-directory-of-templates>

Running the linter on a specific file

j2lint <path-to-directory-of-templates>/template.j2

Listing linting rules

j2lint --list

Running the linter with verbose linter error output

j2lint <path-to-directory-of-templates> --verbose

Running the linter with custom file extensions

j2lint <path-to-directory-of-templates> --extensions j2,html,yml

Running the linter with logs enabled. Logs saved in jinja2-linter.log in the current directory

j2lint <path-to-directory-of-templates> --log

To enable debug logs, use both options:

j2lint <path-to-directory-of-templates> --log --debug

Running the linter with JSON format for linter error output

j2lint <path-to-directory-of-templates> --json

Ignoring rules

  1. The --ignore option can have one or more of these values: syntax-error, single-space-decorator, filter-enclosed-by-spaces, jinja-statement-single-space, jinja-statements-indentation, no-tabs, single-statement-per-line, jinja-delimiter, jinja-variable-lower-case, jinja-variable-format.

  2. If multiple rules are to be ignored, use the --ignore option along with rule descriptions separated by space.

    j2lint <path-to-directory-of-templates> --ignore <rule_description1> <rule_desc>
    

Note This runs the custom linting rules in addition to the default linting rules. When using the -i/--ignore or -w/--warn options, the arguments MUST either:

  • Be entered at the end of the CLI as in the example above
  • Be entered as the last options before the <path-to-directory-of-templates> with -- separator. e.g.
    j2lint --ignore <rule_description1> <rule_desc> -- <path-to-directory-of-templates>
    
  1. If one or more linting rules are to be ignored only for a specific jinja template file, add a Jinja comment at the top of the file. The rule can be disabled using the short description of the rule or the id of the rule.

    {# j2lint: disable=S6}
    
    # OR
    {# j2lint: disable=jinja-delimiter #}
    
  2. Disabling multiple rules

    {# j2lint: disable=jinja-delimiter j2lint: disable=S1 #}
    

Adding custom rules

  1. Create a new rules directory under j2lint folder.

  2. Add custom rule classes which are similar to classes in j2lint/rules directory: The file name of rules should be in snake_case and the class name should be the PascalCase version of the file name. For example:

    • File name: jinja_operator_has_spaces_rule.py
    • Class name: JinjaOperatorHasSpacesRule
  3. Run the jinja2 linter using --rules-dir option

    j2lint <path-to-directory-of-templates> --rules-dir <custom-rules-directory>
    

Note This runs the custom linting rules in addition to the default linting rules.

Running jinja2 linter help command

j2lint --help

Running jinja2 linter on STDIN template. This option can be used with VS Code.

j2lint --stdin

Using j2lint as a pre-commit-hook

  1. Add j2lint pre-commit hook inside your repository in .pre-commit-config.yaml.

    - repo: https://github.com/aristanetworks/j2lint.git
        rev: <release_tag/sha>
        hooks:
        - id: j2lint
    
  2. Run pre-commit -> pre-commit run --all-files

Note When using -i/--ignore or -w/--warn argument in pre-commit, use the following syntax

- repo: https://github.com/aristanetworks/j2lint.git
    rev: <release_tag/sha>
    hooks:
    - id: j2lint
    # Using -- to separate the end of ignore from the positional arguments
    # passed to j2lint
      args: [--ignore, S3, jinja-statements-single-space, --]

Acknowledgments

This project is based on salt-lint and jinjalint

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

j2lint-1.2.0.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

j2lint-1.2.0-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file j2lint-1.2.0.tar.gz.

File metadata

  • Download URL: j2lint-1.2.0.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for j2lint-1.2.0.tar.gz
Algorithm Hash digest
SHA256 3bb9d2a3e3eb1647fe6c469bfbd2c2a9a8597378cda8590ed9eb7df7f3334639
MD5 feb3a8e7ec7199c10863a6379c7fd52a
BLAKE2b-256 41b8c3a194c6ec14d51104ac9070564b42c0335ba131d7d27e3e137d227c2a1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for j2lint-1.2.0.tar.gz:

Publisher: release.yml on aristanetworks/j2lint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file j2lint-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: j2lint-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for j2lint-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cda7fab7af742f25dcb4925168de785721dcf66c7add2063d8e3643241e19e4e
MD5 64b7fd12f39586490f6ec0fd7982825f
BLAKE2b-256 057cf24df7f5fca454036ed4d87847c81390793d120c3c89629cef8d79ecc3fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for j2lint-1.2.0-py3-none-any.whl:

Publisher: release.yml on aristanetworks/j2lint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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