Yaml reader and jinja2

I would like to be able to read the jinja YAML configuration file in python, for example, using the PyYAML library, but I get errors:

{% set name = "abawaca" %} {% set version = "1.00" %} package: name: {{ name }} version: {{ version }} source: fn: {{ name }}-{{ version }}.tar.gz url: https://github.com/CK7/abawaca/archive/v{{ version }}.tar.gz sha256: 57465bb291c3a9af93605ffb11d704324079036205e5ac279601c9e98c467529 build: number: 0 requirements: build: - gcc # [not osx] - llvm # [osx] 
+1
python jinja2 pyyaml
source share
1 answer

Your input is invalid YAML, as you can easily check for example. here First you must expand the {% %} constructors and then process YAML, or you must make your file in a valid YAML.

This is partly due to the choice of jinja2, for which the macro sequences {% ... %} begin with the character ( { ), which has special meaning in YAML.

If you need to change the YAML and write it again, you can define your own delimiters and select them so that they do not have much meaning in the YAML.

{% %} you should add a YAML comment block, since at the top level you have a mapping and should only have key-value pairs. One way to achieve this is to redefine the beginning as #% %# (you don't have to change the end, but I prefer symmetry).

Then, after the upgrade, run the correct YAML through a small script that processes the file and replaces the delimiters with those that jinja2 understands or sets up the environment to change the actual definitions used by jinja2.

fixed data.yaml :

 #% set name = "abawaca" %# #% set version = "1.00" %# package: name: <{ name }> version: 42 source: fn: <{ name }>-<{ version }>.tar.gz url: https://github.com/CK7/abawaca/archive/v<{ version }>.tar.gz sha256: 57465bb291c3a9af93605ffb11d704324079036205e5ac279601c9e98c467529 build: number: 0 requirements: build: - gcc # [not osx] - llvm # [osx] 

This can be handled:

 import jinja2 from ruamel import yaml yaml_file = 'data.yaml' tmp_file = 'tmp.yaml' data = yaml.round_trip_load(open(yaml_file)) data['package']['version'] = '<{ version }>' with open(tmp_file, 'w') as fp: yaml.round_trip_dump(data, fp) environment = jinja2.Environment( loader=jinja2.FileSystemLoader(searchpath='.'), trim_blocks=True, block_start_string='#%', block_end_string='%#', variable_start_string='<{', variable_end_string='}>') print(environment.get_template(tmp_file).render()) 

:

 package: name: abawaca version: 1.00 source: fn: abawaca-1.00.tar.gz url: https://github.com/CK7/abawaca/archive/v1.00.tar.gz sha256: 57465bb291c3a9af93605ffb11d704324079036205e5ac279601c9e98c467529 build: number: 0 requirements: build: - gcc # [not osx] - llvm # [osx] 

Note that you must use `ruamel.yaml (disclaimer: I am the author of this package), you cannot do this with ease with PyYAML, as this discards comments when reading the YAML file. Since all jinja2 in the comments appears at the beginning of the file, you can get around this with this specific example, but overall it will not.

+1
source share

All Articles