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
.
Outputscripts/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}
Templates combine the content of multiple YAML files into a single pipeline. You can pass parameters into a template from your parent pipeline.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With