resolving YAML files and substituting into templates

I have bunch of YAML files in a config folder and bunch of templates in a template folder. Is there a simple one liner or function that I can use to resolve YAML files and substitute in the template files to generate executable scripts

Inputs:
config folder: config/yaml1 , config/yaml2 , config/yaml3. .
template: template/template1 , template/template2 , template3 .

Output
scripts/script1 , script2 , script3

The number of scripts = number of templates

There are 2 types of templates

One that is straightforward substitution Example

YAML1:
    Titles:4
    SubTitles:10
Template1:
Number of Titles {Titles} where as Number of Subtitles is {SubTitles}

Other Template is a nested one. Basically the template needs to be looped based on YAML Example:

YAML2:
    Book: "The Choice of using Choice"
        Author: "Unknown1"
    Book: "Chasing Choices"
        Author:"Known2"
Template2
Here are all the Books with Author Info
The author of the {Book} is {Author}

I am not entirely sure what you are trying to accomplish, I don't know what you mean by executable scripts? If I am interpreting it correctly you might benefit from taking a look at the PyYAML Docs. Specifically, the section on YAML Tags and Python Types.

There are several YAML Tags which allow for python modules or packages to be called and the package arguments are filled in by the nested YAML Tags below it. So, you would have to write a class or function that consumes the YAML kwargs but you would just call it in the YAML config with one line like so,

!!python/object/apply:module.function [argument, ...] 

Although you might also benefit from taking a look at the section on Constructors, Representers and Resolvers as well as string.Template from the python docs.

In theory, you should be able to call the substitution() function from inside the YAML file.


YAML doesn't know anything about templates, and you do not specify what kind of templates need updating. But if you are using a templating language that assumes its values for substitution to come from key-value pairs in a Python dict, then you can specify a top-level mapping in your YAML file, load that (which will be constructed into a Python dict) and then feed that into the template engine.

You would still have to walk over your files, so a few lines of code is more likely than realising the above in a one-liner.

链接地址: http://www.djcxy.com/p/38626.html

上一篇: Change()不会与类标识符反应

下一篇: 解析YAML文件并代入模板