任何用于解析 apache 配置文件的 python 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/237209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any python libs for parsing apache config files?
提问by daniels
Any python libs for parsing apache config files or if not python anyone aware of such thing in other languages (perl, php, java, c#)? As i'll be able to rewrite them in python.
任何用于解析 apache 配置文件的 python 库,或者如果不是 python 任何人都知道其他语言(perl、php、java、c#)中的这种东西?因为我将能够在 python 中重写它们。
采纳答案by Nick Wilkens
I did find an interesting Apache Config parser for python here: http://www.poldylicious.de/node/25
我确实在这里找到了一个有趣的 Python Apache 配置解析器:http: //www.poldylicious.de/node/25
The Apache Config Parser mentioned is not documented, but it does work.
提到的 Apache Config Parser 没有记录,但它确实有效。
回答by Charles Duffy
Red Hat's Emerging Technologies group has Augeas(written in C, but with Python bindings available), a generic system configuration tool with "lenses" for reading and writing several different configuration file formats. I would consider investigating the availability of a lens for Apache.
Red Hat 的 Emerging Technologies 小组有Augeas(用 C 编写,但可以使用 Python 绑定),这是一个通用的系统配置工具,带有用于读取和写入几种不同配置文件格式的“镜头”。我会考虑调查 Apache 镜头的可用性。
回答by geographika
There is a new library as of 2016 written using pyparsing:
截至 2016 年,有一个使用 pyparsing 编写的新库:
https://pypi.python.org/pypi/parse_apache_configs/
https://pypi.python.org/pypi/parse_apache_configs/
Has a few rough edges, but allowed me to add in directives and save to a new file.
有一些粗糙的边缘,但允许我添加指令并保存到新文件。
Source at https://github.com/alextricity25/parse_apache_configs
回答by VisioN
There is also one new parser released.
还发布了一个新的解析器。
It still lacks documentation, however is quite straightforward for understanding.
它仍然缺乏文档,但是很容易理解。
Example
例子
import apache_conf_parser
import pprint
DEFAULT_VHOST = '/etc/apache2/sites-available/000-default.conf'
vhost_default = apache_conf_parser.ApacheConfParser(DEFAULT_VHOST)
print vhost_default.nodes
print vhost_default.nodes[0].body.nodes
pprint.pprint(
{
i.name: [i.arguments for i in vhost_default.nodes[0].body.nodes]
}
)
回答by Michael Gundlach
No Python libraries exist that I know of, but here's a perl one: http://packages.debian.org/sid/libapache-configfile-perl
我知道不存在 Python 库,但这里有一个 perl 库:http: //packages.debian.org/sid/libapache-configfile-perl
Package: libapache-configfile-perl
Priority: optional
Section: interpreters
Installed-Size: 124
Maintainer: Michael Alan Dorman
Version: 1.18-1
Depends: perl (>= 5.6.0-16)
Description: Parse an Apache style httpd.conf configuration file
This module parses the Apache httpd.conf, or any
compatible config file, and provides methods for
you to access the values from the config file.
If you do rewrite it in Python, please update your post to mention the name of your package on PyPI! :)
如果您确实用 Python 重写了它,请更新您的帖子以在 PyPI 上提及您的包的名称!:)
回答by James Bennett
ZConfig, I think, used to ship with a schema for parsing Apache configuration files; it doesn't seem to anymore, but it's oriented around parsing those types of files and turning the config into a Python object. A quick glance at the documentation suggests it wouldn't be too hard to set up a ZConfig schema corresponding to whatever Apache options you'd like to parse and validate.
我认为,ZConfig 曾经附带一个用于解析 Apache 配置文件的模式;它似乎不再如此,但它面向解析这些类型的文件并将配置转换为 Python 对象。快速浏览文档表明,设置与您想要解析和验证的任何 Apache 选项相对应的 ZConfig 模式并不太难。