X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=config.py;h=0cde6f39885aec6ce6c26c555d08291dbf697856;hb=62928a9776311c5e1a64981851b855027b1f988e;hp=19c590dc6e85b1552850c2bdf8693ac754dda586;hpb=44fabe102d9bb5bd485ecdea2d912242ee98c0fa;p=monitor.git diff --git a/config.py b/config.py index 19c590d..0cde6f3 100644 --- a/config.py +++ b/config.py @@ -1,16 +1,59 @@ +#!/usr/bin/python + +# load defaults from /etc/monitor.conf +# home/.monitor.conf +# $PWD/.monitor.conf +import os +import ConfigParser + +class Options(object): + def __init__(self): + cp = ConfigParser.ConfigParser() + cp.optionxform = str + # load defaults from global, home dir, then $PWD + cp.read(['/etc/monitor.conf', os.path.expanduser('~/.monitor.conf'), + '.monitor.conf', 'monitor.conf']) + self.cp = cp + self.section = "default" + def __getattr__(self, name): + if name in self.cp.sections(): + self.section = name + return self + else: + return self.cp.get(self.section, name) + + +import config +imported = False + +def updatemodule(module, cf): + module.__dict__.update(cf.__dict__) + +def update_section(options, section, bool=False): + # Place all default commandline values at the top level of this module + for key in options.cp.options(section): + if bool: + config.__dict__.update({key : options.cp.getboolean(section, key)}) + else: + config.__dict__.update({key : options.cp.get(section, key)}) + +def update(parseoptions): + update_commandline() + # now update the top-level module with all other args passed in here. + for key in parseoptions.__dict__.keys(): + config.__dict__.update({key: parseoptions.__dict__[key]}) + +if not config.imported: + imported = True + + #from config import options as config + options = Options() + update_section(options, 'commandline', True) + update_section(options, 'monitorconfig') + update_section(options, 'monitordatabase') + +#for i in dir(config): +# if "__" not in i: +# print i, "==", config.__dict__[i] +#print "======================================" -debug = False - -#from xml.sax import saxutils -# -#class config(saxutils.DefaultHandler): -# def __init__(self, file, start): -# self.file = file -# self.start = start -# self.config = {} -# -# def startElement(self,name, attrs): -# if name != self.start: return -# -# -#incomplete