[Tutor] implementing config files
Kent Johnson
kent37 at tds.net
Fri Jun 2 12:08:50 CEST 2006
Tracy R Reed wrote:
> 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.
One possibility is to make the config file be an actual Python module.
Then it can instantiate objects directly. The class constructors can do
as much checking as you like. Your config file might look like this:
import testdefs
host1 = testdefs.Host('192.168.0.53', '80')
host2 = testdefs.Host('192.168.0.55', '27')
tests = [
testdefs.check_tcp(host1),
testdefs.check_http(host1),
testdefs.check_tcp(host2),
testdefs.check_ftp(host2),
]
testdefs.Host is a class which validates and stores the hostname and
port, raising an exception if there is a problem.
testdefs.check_tcp() etc are classes that validate and remember their
init parameters and have an execute() method to actually do the test. So
just by importing the above module you validate all the parameters. To
run the tests, use something like
for test in tests:
test.execute()
> 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.
You obviously have to import the file to get the error checking, but you
could do that on a test machine before you distribute it.
Kent
More information about the Tutor
mailing list