Python配置文件生成器

发布于 2024-09-05 14:55:39 字数 1000 浏览 2 评论 0原文

我想用Python制作一个配置文件生成器。我的大致想法是使用模板文件和一些具有真实设置的 XML 文件来提供输入。然后使用程序生成真正的配置文件。

示例:

[template file]
server_IP = %serverip%
server_name = %servername%


[XML file]
<serverinfo>
<server ip="x.x.x.x" name="host1" />
<server ip="x.x.x.x" name="host2" />
</serverinfo>

然后像这样获取输出配置文件

[server.ini]

[server1]
server_IP = x.x.x.x
server_name = host1

[server2]
server_IP = x.x.x.x
server_name = host2

我有几个问题:

  • 有没有开源的配置生成器程序? (关键字可能是什么),我想知道设计中是否可以添加/修改任何内容。

  • Python 有好的 XML 解析器模块吗?

  • 使用 XML 文件保存原始设置是个好主意吗?我一直在考虑使用 Excel,因为它维护起来更直观,但程序解析起来更困难。不知道人们如何处理这个问题。

希望社会各界能给我一些建议。多谢!

编辑: 在有数十个这样的输出 ini 文件的情况下。我关心两件事。

  1. 有几十个ip/hostname及相关信息,可能需要人工管理,所以XML格式会有点不方便。管理这些信息最方便的结构是什么? (Excel是一个方便的批量修改和查找信息的工具)

  2. 如果需要在ini文件中添加一些额外的行,我需要一种有效的方法,只需修改模板文件并将额外的信息添加到源文件中(可能是Excel文件或其他文件),然后可以快速生成一大堆ini文件。

I want to use Python to make a configuration file generator. My roughly idea is feeding input with template files and some XML files with the real settings. Then use the program to generate the real configuration files.

Example:

[template file]
server_IP = %serverip%
server_name = %servername%


[XML file]
<serverinfo>
<server ip="x.x.x.x" name="host1" />
<server ip="x.x.x.x" name="host2" />
</serverinfo>

and then get output configuration file like this

[server.ini]

[server1]
server_IP = x.x.x.x
server_name = host1

[server2]
server_IP = x.x.x.x
server_name = host2

I got several questions:

  • Is there any open source configuration generator program? (what could be the keyword), I wonder if there's anything can be added/modified in the design.

  • Does Python have good XML parser module?

  • Is it good idea to use XML file to save the original settings? I've been thinking to use Excel since it's more intuitive to maintain, but harder for program to parse. Not sure how people deal with this.

Hope the community can give me some suggestions. Thanks a lot!

EDIT:
In scenario that there are dozens of these output ini files. I am concerning 2 things.

  1. there are dozens of ip/hostname and related informations, that may requires to be managed by human, so XML format would be a bit inconvenient. What could be the most convenient structure to manage those information? (Excel would be a handy tool to batch modify and look up info)

  2. In case of need to add some extra line into ini files, I need a efficient way by just modify the template file and add extra info into the source file (may be the Excel file or whatever), then whole bunches of ini files can be generated quickly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心碎无痕… 2024-09-12 14:55:39

我建议使用 Michael Foord 提供的优秀的 ConfigObj 库。它可以读取/写入配置文件,即使是嵌套的部分。

I recommend using excellent ConfigObj library by Michael Foord. It can read/write configuration files, even with nested sections.

韶华倾负 2024-09-12 14:55:39

不知道有没有开源的配置生成器。

Python 有 几个 xml 解析器模块,其中最新的(也许也是最 Pythonic 的)是ElementTree。您可以在开发人员站点找到更多相关文档。

我建议尽可能避免在配置文件中使用 xml。充满名称/值对的简单平面文件对于人类来说更容易使用。如果您需要一两级结构,ini 样式文件应该可以很好地完成,并且 python 附带

I don't know if there are any open source configuration generators.

Python has several xml parser modules, the newest (and perhaps most pythonic) of which is ElementTree. You can find additional documentation for it at the developer's site.

I recommend avoiding xml in your configuration files if possible. A simple flat file full of name/value pairs is much easier for humans to work with. If you need a level or two of structure, ini-style files ought to do nicely, and python comes with a built-in parser for them.

寄风 2024-09-12 14:55:39

对于任何配置文件使用 xml 都是非常棒的。任何解释语言中的最佳选择是使用代码文件并简单地执行或导入它。 Pickle 也不错,但人类不可读。

It's terrific to use xml for any configuration file. Best choice in any interpreted language is to use code file and simply exec or import it. Pickle is also good, but not human readable.

尬尬 2024-09-12 14:55:39

我使用了 minidom 模块。这可能对你有用。

I've used the minidom module. That would probably work for you.

故笙诉离歌 2024-09-12 14:55:39

你需要一些模板引擎,看看string.Template

You need some template engine, look at string.Template

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文