yaml和jinja2阅读器 [英] yaml and jinja2 reader

查看:84
本文介绍了yaml和jinja2阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够像使用PyYAML库一样在python中读取YAML jinja配置文件,但是我收到错误:

I would like to be able to read in python a YAML jinja configuration file like using the PyYAML library but I'm receiving 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]

推荐答案

您的输入内容不是有效的YAML,因为您可以轻松地进行检查,例如此处 您应该首先扩展{% %}构造,然后处理YAML,或者应该将文件制成有效的YAML.

Your input is not valid YAML, as you can easily check, e.g. here You should first expand the {% %} constructs, and then process the YAML, or you should make your file into valid YAML.

这是选择jinja2的部分结果,对于jinja2,宏序列{% ... %}以在YAML中具有特殊含义的字符({)开头.

This is a partly consequence of choosing jinja2 for which the macro sequences {% ... %} start with a character ({) that has special meaning in YAML.

如果需要更改YAML,然后再次将其写出,则可以定义自己的定界符并选择它们,以使它们在YAML中没有特殊含义.

If you need to change the YAML, and write it out again, you can define your own delimiters and choose them so that don't have special meaning in YAML.

您应该放入YAML注释块中的{% %},就像在顶层具有映射并且应该只包含键值对一样.实现此目标的一种方法是将开始重新定义为#% %#(您不必更改结束,但我更喜欢对称性.)

The {% %} you should put in a YAML comment block as at the top-level you have a mapping and should only have key-value pairs. One way to achieve that is by redefining the start as #% %# (you don't necessarily have to change the end, but I prefer the symmetry).

然后,在更新后,通过一个小的脚本运行正确的YAML,该脚本处理该文件,并将定界符替换为jinja2理解的分隔符或对环境进行调整,以更改jinja2使用的实际定义.

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

更正的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 processed by:

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]

请注意,您必须使用`ruamel.yaml(免责声明:我是该软件包的作者),使用PyYAML不能如此轻松地完成此操作,因为它会丢弃读取YAML文件的注释.由于注释中的所有jinja2都发生在文件的开头,因此您可以使用此特定示例解决此问题,但通常情况并非如此.

Please note that you have to use `ruamel.yaml (disclaimer: I am the author of that package), you cannot do this as easily with PyYAML as it throws away the comments on reading the YAML file. Since all of the jinja2 within comments occurs at the beginning of the file you can work around this with this particular example, but in general that will not be the case.

这篇关于yaml和jinja2阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆