X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=config.py;h=feef515cda76566a162ead0cd3f73c0ade86466e;hb=e178fd6ee0caeff1e42243fe86bae10680d69462;hp=112173fba238e36ca008cde62f307692e054377c;hpb=55ec8f3eb860029ecf552d8c0a3cc6dbd2f66b68;p=monitor.git diff --git a/config.py b/config.py index 112173f..feef515 100644 --- a/config.py +++ b/config.py @@ -1,13 +1,64 @@ -from xml.sax import saxutils +#!/usr/bin/python -class config(saxutils.DefaultHandler): - def __init__(self, file, start): - self.file = file - self.start = start - self.config = {} +# load defaults from /etc/monitor.conf +# home/.monitor.conf +# $PWD/.monitor.conf +import os +import ConfigParser - def startElement(self,name, attrs): - if name != self.start: return +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) -#incomplete +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() + try: + update_section(options, 'commandline', True) + except: + pass + try: + update_section(options, 'monitorconfig') + except: + pass + +#for i in dir(config): +# if "__" not in i: +# print i, "==", config.__dict__[i] +#print "======================================" +