[Tutor] implementing config files

Carlos Daniel Ruvalcaba Valenzuela clsdaniel at gmail.com
Fri Jun 2 08:36:27 CEST 2006


Your could try to use XML files to store configuration files, I
already coded something like that, using expat parser and loading the
XML contents to objects and attributes, this is a sample code of how
works my module:

Lets supouse we have this file config.xml with the following contents:
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
        <connection>
                <server>MySQL</server>
                <host>localhost</host>
                <port>21</port>
                <user>username</user>
                <passwd></passwd>
        </connection>
</config>

and in our code:

from objxml import *

fd = file('config.xml', 'r')
p = XMLParser(fd)

root = p.Root
port = str(root.connection.port)
user = str(root.connection.username)

All nodes are objects, converting them to strings gets you the
content, you can also access the atributes as normal object
attributes.

This code can be useful for what you want, take a look.

Regards
Carlos Daniel Ruvalcaba Valenzuela


On 6/1/06, Tracy R Reed <treed at ultraviolet.org> wrote:
> Hello all!
>
> I am writing some code to implement a bunch of passive checks for the
> nagios network monitoring system. It runs some checks on the local
> machine and reports the results back to the nagios server using rpc
> calls via Perspective Broker from the Twisted module. This code and
> associated config file is distributed to all of the machines in my
> infrastructure by cfengine. This part all works quite well. Now I need
> to refactor my code into a more general and flexible infrastructure and
> provide a way to configure the services being checked instead of hard
> coding them (as they currently are).
>
> I need to implement a config file which will provide hostnames, the
> names of checks to run on those hosts, and the options to those checks.
> These things are sets which are nested inside each other which we can
> think of like nested objects. I could make a dictionary containing the
> host names each of which is a dictionary containing the services to be
> checked etc.
>
> But rather than just make dictionaries of dictionaries (which could get
> confusing) I was wondering if I could make it more robust by somehow
> defining objects nested inside of other objects in the config file with
> certain attributes. For example I would like to be able to have a
> check_tcp object which would have two attributes: hostname and port. If
> you try to assign it anything else you get an error. If port isn't a
> number between 0 and 2^16 you get an error. Etc. Basically I don't want
> errors in the config file to propagate all the way to the client machine
> on which the code is going to be executed and have wrong arguments
> passed to the program which we os.popen() and read the results from.
>
> Anyone have suggestions on how to proceed? I'm sure the problem of
> parsing config files into useful objects must be a solved one...
>
> I will eventually be posting a link to my code for others to use and
> critique. It has been a neat project so far.
>
> --
> Tracy R Reed                  http://ultraviolet.org
> A: Because we read from top to bottom, left to right
> Q: Why should I start my reply below the quoted text
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: objxml.py
Type: text/x-python
Size: 1989 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20060601/6a4b77db/attachment.py 


More information about the Tutor mailing list