Skip to main content

Application specific YAML tag of Jinja2 template

Project description

jinjyaml

GitHub tag Test Python Package Documentation Status Quality Gate Status PyPI

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

>>> import yaml
>>> import jinjyaml as jy
>>>
>>> ctor = jy.Constructor()
>>> yaml.add_constructor('!j2', ctor, yaml.FullLoader)
>>>
>>> s = '''
... array:
...   !j2 |
...     {% for i in range(n) %}
...     - sub{{i}}: {{loop.index}}
...     {% endfor %}
... '''
>>>
>>> obj = yaml.full_load(s)
>>>
>>> data = jy.extract(obj, context={'n': 3})
>>> print(data)
{'array': [{'sub0': 1}, {'sub1': 2}, {'sub2': 3}]}

Example 2

We have such YAML files:

  • child-1.yml:

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

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

    children: !j2 |
    
      {% include "child-1.yml" %}
      {% include "child-2.yml" %}
    

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.FullLoader)

with open('main.yml') as fp:
    doc = yaml.full_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'}}}

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.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

jinjyaml-0.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jinjyaml-0.3.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for jinjyaml-0.3.tar.gz
Algorithm Hash digest
SHA256 540c245252dcfa9d4950aa3126e91e4079a7a1fbf71dd3ead75b03b74f9eaa30
MD5 390334f14f93b6026e9925fb18819da0
BLAKE2b-256 2119cddfc8444ce728feb32f02646360027c5639bb2d607ab6ccf66eb1d6dcda

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jinjyaml-0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for jinjyaml-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ec81e0640509a5d081bf2a4ee8ec489fce9b4664f87955cc56a190e4720b6e64
MD5 cc51f7e02713d20e0f2e4acc57622be6
BLAKE2b-256 08fd101bca4a6ebff31c024656d836f5a9d9eda11a4c08c2e872c011e798ec65

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