12

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.

daniels
  • 18,416
  • 31
  • 103
  • 173
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. – GhostCat May 10 '17 at 12:17
  • 1
    Sometimes even opinionated answers are good when you got nothing to go. Now I no longer need that, as the question was asked way in the past but it's helpful sometimes to get recommendations even if they are opinionated. – daniels May 10 '17 at 12:54
  • Lets put it that way: I do not know whether this question was valid 9 years back. But I sure it would be closed if it would be asked today; as it is clearly violating the "no recommendations" rule. And you see; what happens here is - all these answers are (and actually can only be) **link only** ones. So, sooner or later ... those links might break; and then the answer turns useless. – GhostCat May 10 '17 at 12:59
  • Where would you recommend one would go/ask when wanting to know what options he has for doing thing x and/or what are people experiences with that thing (assuming those who respond also used what they recommend)? Honest question. Just looking to know where else to go for this kind of feedback if SO is not the right place. – daniels May 10 '17 at 19:04
  • There is a "new" software recommendations site on the stackexchange.com network since I don't know. Probably that close reason should could be enhanced to point out that possibility. – GhostCat May 10 '17 at 19:09

5 Answers5

3

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • No lens exists for Apache configuration files. – Teddy Mar 03 '11 at 10:42
  • 1
    On Debian/Ubuntu, using the pypi library https://pypi.python.org/pypi/python-augeas appears to rely on https://launchpad.net/ubuntu/+source/augeas being installed on the target system first. – ThorSummoner Dec 30 '15 at 20:50
3

There is a new library as of 2016 written using pyparsing:

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

geographika
  • 6,458
  • 4
  • 38
  • 56
1

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]
    }
)
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • 1
    I was able to get this to work. Had to use a lot of interactive `help()` and `dir()` and `.__dict__`, but thats also kind of what makes python awesome. – ThorSummoner Dec 30 '15 at 21:45
  • Last updated in 2011. I wonder how it works with more recent versions of Apache. – nealmcb Jun 05 '19 at 00:34
1

No Python libraries exist that I know of, but here's a perl one: 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! :)

Michael Gundlach
  • 106,555
  • 11
  • 37
  • 41
0

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.

http://pypi.python.org/pypi/ZConfig/2.6.0

James Bennett
  • 10,903
  • 4
  • 35
  • 24