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 <[EMAIL PROTECTED]> 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@python.org http://mail.python.org/mailman/listinfo/tutor
import xml.parsers.expat class ObjXML: Attribs = {} Children = {} Data = None Parent = None Name = "" def __init__(self, parent, name, attrs = {}): self.Attribs = attrs self.Parent = parent self.Name = name if type(parent) != type(None): parent.Append(self) def Append(self, obj): self.Children[obj.Name] = obj def FileXML(self, fd): fd.write("<" + self.Name) for a in self.Attribs: fd.write(" " + self.Name + """="""" + self.Attribs[a] + """" """) fd.write(">") #We cannot have nodes with data and children at the same time if len(self.Children) > 0: for c in self.Children: self.Children[c].FileXML(fd) else: fd.write(self.Data) fd.write("</" + self.Name + ">") def __str__(self): if type(self.Data) == type(None): return "" return self.Data.encode('ascii') def __repr__(self): if type(self.Data) == type(None): return "" return self.Data def SetAttrib(self, name, value): self.Attribs[name] = value def __getattr__(self, name): if (self.Children.has_key(name)): return self.Children[name] if (self.Attribs.has_key(name)): return self.Attribs[name] #self.__dict__[name] class XMLParser: Root = None def start_element(self, name, attrs): if type(self.Root) == type(None): self.Root = ObjXML(None, name, attrs) else: c = ObjXML(self.Root, name, attrs) self.Root.Append(c) self.Root = c def end_element(self, name): if type(self.Root.Parent) != type(None): self.Root = self.Root.Parent def char_data(self, data): if str(data)[0] != "\n": self.Root.Data = str(data) def __init__(self, fd): p = xml.parsers.expat.ParserCreate() p.StartElementHandler = self.start_element p.EndElementHandler = self.end_element p.CharacterDataHandler = self.char_data if type(fd) == type(str()): p.Parse(fd) else: p.ParseFile(fd) while type(self.Root.Parent) != type(None): self.Root = self.Root.Parent
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor