2017-02-25 86 views
7

我有一堆YAML文件在一個配置文件夾和模板文件夾中的一堆模板。有一個簡單的一個襯墊或功能,我可以用它來解決模板文件YAML文件和替代生成可執行的腳本解析YAML文件並替換爲模板

輸入:
config文件夾:config/yaml1config/yaml2config/yaml3.
模板:template/template1,template/template2,template3

輸出
scripts/script1script2script3

腳本的數=模板數

有2種類型的模板

一說是直接置換 例

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

其他模板是嵌套的。基本上,模板需要基於YAML 實例進行循環:

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} 

回答

1

YAML不知道模板任何東西,你沒有指定什麼樣的模板,需要更新。但是,如果您使用的模板語言假設其替代值來自Python字典中的鍵值對,那麼您可以在YAML文件中指定頂級映射,加載該映射(將構建到Python中字典),然後將其饋入模板引擎。

您仍然需要遍歷文件,因此只需幾行代碼就可以實現上述功能。

2

我不完全確定你要完成什麼,我不知道你的可執行腳本是什麼意思?如果我正確解釋它,您可能會從PyYAML文檔中獲益。具體來說,關於YAML Tags and Python Types的部分。

有幾個YAML標籤允許調用python模塊或包,並且包參數由它下面的嵌套YAML標籤填充。所以,你會寫一個類或函數,消耗的YAML kwargs但你只是像這樣一條線把它在YAML配置,

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

雖然你也可以採取一看受益 部分Constructors, Representers and Resolvers以及string.Template來自python文檔。

理論上,您應該能夠從YAML文件中調用substitution()函數。