confgen是一个小的命令实用程序,它可以帮助您生成一些配置,这要感谢jinja2模板
confgen的Python项目详细描述
confgen
confgen是一个小的命令实用程序,可以帮助您生成一些配置
- 免费软件:BSD许可证
- 在Python2.7和3.2上测试
简介
在我的公司里,我们没有一个正确的工具来为我们的网络设备生成配置,只是一部分 糟糕的vba代码,不能真正满足我们的需求。所以我开始考虑一个可以提供 我们的简单。所以,主要的想法是我们经常在一个excel文件中有一些输入数据,我们使用这个文件 用一个模板构建我们的配置。
使用confgen的快速步骤:
- 收集数据并将其放入csv文件中,文件的第一行应该是带有变量名的标题行
- 构建模板,confgen基于jinja,因此如果要构建模板,只需在http://jinja.pocoo.org/docs/上jinja的rtfd
- 生成文件
安装
Linux
安装安装工具:https://pypi.python.org/pypi/setuptools
在命令行中
$ easy_install confgen
或
$ pip install confgen
窗口
- 下载python并从here 安装python
- 安装安装工具:https://pypi.python.org/pypi/setuptools(另存为,然后双击)
- 添加;c:pythonxx;c:pythonxscripts其中XX是您的python版本到您的环境路径
- 打开命令并
easy_install pip
pip install confgen
使用量
您必须从命令行调用confgen。
命令行选项
-a | (–append) Appending the ouput to the end of the file if it exists |
-d | (–delimiter) Delimiter for your CSV formatted file, default is ; |
-h | (–help) Display the help and exit |
-i | (–input) Input filename of your CSV |
-mo | (–multipleoutput) Generate on file per line, you must specify the name of the column where are the names of files to generate |
-so | (–simpleoutput) Output file name, stdout if not specified |
-t | (–template) Your template file in text and jinja2 format |
-v | (–version) Display the version and exit |
示例
每行一个文件
下面是一个小例子,为了了解它是如何工作的,您的excel/calc选项卡如下:
name gender description beer_test child homer man D'oh! yes yeah marge women Now it's Marge's time to shine! bart boy Ay caramba! yes lisa girl Trust in yourself and you can achieve anything. yes maggie baby It's your fault I can't talk! yes
以csv格式呈现(使用;作为分隔符):
name;gender;description;beer_test;child homer;man;D'oh!;yes;yeah marge;women;Now it's Marge's time to shine!;; bart;boy;Ay caramba!;;yes lisa;girl;Trust in yourself and you can achieve anything.;;yes maggie;baby;It's your fault I can't talk!;;yes
注意:第一行必须是带有变量名的标题行(变量名中没有空格)
现在,这里是第一个模板示例:
Welcome {{ name }}, You're a {{ gender }} Your favorite expression is : "{{ description }}" {%- if beer_test %} You're allowed to drink beer {%- else %} /!\ You're not allowed to drink beer {%- endif %} {%- if child %} Children playground access : ok {%- endif %}
我们希望每行生成一个文件,文件名将是name列
natjohan~# confgen -i example.csv -t template.txt -mo name
-----------------------------------------
Input file : example.csv
Template file : template.txt
Delimiter : ;
-----------------------------------------
File homer was generated
File marge was generated
File bart was generated
File lisa was generated
File maggie was generated
*** Good job my buddy ! 5 Files were generated ***
现在,您应该有5个文件,分别是荷马、玛吉、巴特、丽莎、玛姬
荷马:
Welcome homer, You're a man Your favorite expression is : "D'oh!" You're allowed to drink beer Children playground access : ok
玛吉:
Welcome marge, You're a women Your favorite expression is : "Now it's Marge's time to shine!" /!\ You're not allowed to drink beer
巴特:
Welcome bart, You're a boy Your favorite expression is : "Ay caramba!" /!\ You're not allowed to drink beer Children playground access : ok
丽莎:
Welcome lisa, You're a girl Your favorite expression is : "Trust in yourself and you can achieve anything." /!\ You're not allowed to drink beer Children playground access : ok
麦琪:
Welcome maggie, You're a baby Your favorite expression is : "It's your fault I can't talk!" /!\ You're not allowed to drink beer Children playground access : ok
一个文件
现在是第二个例子,我们只想生成一个完整的文件
template.txt:
=> {{ name }} => {{ description }}
natjohan~# confgen -i example.csv -t template.txt -so OneFile
-----------------------------------------
Input file : example.csv
Template file : template.txt
Delimiter : ;
-----------------------------------------
*** File OneFile was generated ***
一个文件:
=> homer => D'oh! => marge => Now it's Marge's time to shine! => bart => Ay caramba! => lisa => Trust in yourself and you can achieve anything. => maggie => It's your fault I can't talk!
功能
待办事项
- 强制选项打开(文件,'x')
- 允许stdin作为模板
- 允许选择写入文件的目录
历史记录
0.0.1-15/05/2013-一些脏代码使其正常工作
0.0.2-02/06/2013-使用argparse改进命令行,整理并清理代码,使用多个简单输出进行测试:确定
0.0.3-06/10/2013-清除代码,
0.1.1-2014年3月13日-PYPI首次发布。
0.1.3-2014年3月14日-添加python3兼容性