Skip to main content

Application specific YAML tag of Jinja2 template

Project description

jinjyaml

GitHub tag Python Package PyPI Documentation Status Quality Gate Status Coverage

Application specific tag of Jinja2 template in PyYAML.

It may be useful if you only want to render special tag nodes in the document, instead of whole YAML string as a template.

Usage

Example 1

  1. Add Jinja2 template constructor for tag "!j2"

    import yaml
    import jinjyaml as jy
    
    ctor = jy.Constructor()
    yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
    
  2. create YAML file 1.yml, with such contents:

    array: !j2 |
      {% for i in range(n) %}
      - sub{{i}}: {{loop.index}}
      {% endfor %}
    
  3. load and render the YAML file

    with open("1.yml") as fp:
        data = yaml.load(fp, Loader=yaml.SafeLoader)
        # or for the short:
        # data = yaml.safe_load(fp)
    
    jy.extract(data, context={"n": 3}, inplace=True)
    
    print(data)
    

We'll get:

{"array": [{"sub0": 1}, {"sub1": 2}, {"sub2": 3}]}

Example 2

We have such YAML files:

  • sub-1.yml:

    "1.1": one
    "1.2": two
    
  • sub-2.yml:

    "2.1":
      "2.1.1": three
      "2.1.2": four
    
  • main.yml:

    foo: !j2 |
    
      {% filter indent %}
      {% include "sub-1.yml" %}
      {% endfilter %}
    
      {% filter indent %}
      {% include "sub-2.yml" %}
      {% endfilter %}
    

execute python code:

from pprint import pprint

import jinja2
import jinjyaml as jy
import yaml

env = jinja2.Environment(loader=jinja2.FileSystemLoader("."))

ctor = jy.Constructor()
yaml.add_constructor("!j2", ctor, yaml.SafeLoader)

with open("main.yml") as fp:
    doc = yaml.safe_load(fp)

obj = jy.extract(doc, env)
pprint(obj)

We'll get:

{"foo": {"1.1": "one",
         "1.2": "two",
         "2.1": {"2.1.1": "three", "2.1.2": "four"}}}

NOTE:

Since Jinja2's include and indent do not work very nice with indention languages like Python or YAML, it's not advised to use the feature in a complex case.

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

jinjyaml-0.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

jinjyaml-0.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file jinjyaml-0.4.tar.gz.

File metadata

  • Download URL: jinjyaml-0.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for jinjyaml-0.4.tar.gz
Algorithm Hash digest
SHA256 2d72eefe36b8005a6a2b11d9a268bbcff18daf67120590db11062c13f40668be
MD5 d30872edd9d85ab03186419bf93addd8
BLAKE2b-256 2f279a486de3702acb4c349604c14d8baa0c5604ccda32a0b57d92a28978869b

See more details on using hashes here.

File details

Details for the file jinjyaml-0.4-py3-none-any.whl.

File metadata

  • Download URL: jinjyaml-0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for jinjyaml-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 816a5fea4ecb913e26dbd879374df4d701a8b55cec9aeb2037d20bbb07c9ed45
MD5 948c66cad7a892abcecdb96c6922395c
BLAKE2b-256 a96d0142d79dbc715bb9f425eaacbf9167927f6597e40dcd0d16b4aef933a864

See more details on using hashes here.

Supported by

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