X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=config.py;h=feef515cda76566a162ead0cd3f73c0ade86466e;hb=refs%2Fheads%2F1.0;hp=906c447e473b56c5f8c5c67f6f6a1fe34f1064c9;hpb=cc22cc5b2b7e066f5324890ad80a7cba3654a5e5;p=monitor.git diff --git a/config.py b/config.py index 906c447..feef515 100644 --- a/config.py +++ b/config.py @@ -1,183 +1,64 @@ #!/usr/bin/python -import pickle -import os -import getopt -import sys -import __main__ -from optparse import OptionParser - -config_command = False - -XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/" -def parse_bool(option, opt_str, value, parser): - if opt_str in ["--debug"]: - parser.values.debug = int(int(value)) - elif opt_str in ["--mail"]: - parser.values.mail = int(int(value)) - elif opt_str in ["--bcc"]: - parser.values.bcc = int(int(value)) - elif opt_str in ["--policysavedb"]: - parser.values.policysavedb = int(int(value)) - elif opt_str in ["--squeeze"]: - parser.values.squeeze = int(int(value)) - else: - print "blue" - -def getListFromFile(file): - f = open(file, 'r') - list = [] - for line in f: - line = line.strip() - list += [line] - return list - -class config: - debug=0 - mail=0 - bcc=0 - email="soltesz@cs.utk.edu" - run=False - checkopt=False - squeeze=0 - policysavedb=0 - __file = ".config" - - def __init__(self, parser=None): - if os.path.exists(self.__file): # file exists, read that. - f = open(self.__file, 'r') - o = pickle.load(f) - self.__dict__.update(o) - f.close() - if parser == None: - self.parser = OptionParser() +# 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: - self.parser = parser - - self.parser.set_defaults(debug = self.debug, - mail = self.mail, - bcc = self.bcc, - email = self.email, - run = self.run, - checkopt = False, - squeeze = self.squeeze, - policysavedb = self.policysavedb) + return self.cp.get(self.section, name) - self.parser.add_option("", "--debug", dest="debug", - help="Enable debugging", - type="int", - metavar="[0|1]", - action="callback", - callback=parse_bool) - self.parser.add_option("", "--mail", dest="mail", - help="Enable sending email", - type="int", - metavar="[0|1]", - action="callback", - callback=parse_bool) - self.parser.add_option("", "--bcc", dest="bcc", - help="Include BCC to user", - type="int", - metavar="[0|1]", - action="callback", - callback=parse_bool) - self.parser.add_option("", "--squeeze", dest="squeeze", - help="Squeeze sites or not", - type="int", - metavar="[0|1]", - action="callback", - callback=parse_bool) - self.parser.add_option("", "--policysavedb", dest="policysavedb", - help="Save the policy event database after a run", - type="int", - metavar="[0|1]", - action="callback", - callback=parse_bool) - self.parser.add_option("", "--checkopt", dest="checkopt", - action="store_true", - help="print current options") - self.parser.add_option("", "--run", dest="run", - action="store_true", - help="Perform monitor or print configs") - self.parser.add_option("", "--email", dest="email", - help="Specify an email address to use for mail when "+\ - "debug is enabled or for bcc when it is not") - - # config_command is needed to keep subsequent loads of config() from - # trying to parse the arguments that have already been parsed by - # the new main(). - if parser == None and config_command: - print "calling parse_args" - self.parse_args() - def parse_args(self): - #print "self: %s" % self - #import traceback - #print traceback.print_stack() - #print "Ccalling parse_args" - (options, args) = self.parser.parse_args() - #for o in options.__dict__: - # print "optin: %s == %s" % (o, options.__dict__[o]) - self.__dict__.update(options.__dict__) - self.__dict__['args'] = args - self.save(options) - if options.checkopt: - self.usage() - # print "\nAdd --run to actually perform the command" - sys.exit(1) +import config +imported = False - def getListFromFile(self, file): - f = open(file, 'r') - list = [] - for line in f: - line = line.strip() - list += [line] - return list +def updatemodule(module, cf): + module.__dict__.update(cf.__dict__) - def print_values(self): - exclude = ['parser'] - for key in self.__dict__.keys(): - if key not in exclude: - print "%20s == %s" % (key, self.__dict__[key]) - - def save(self, options=None): - f = open(self.__file, 'w') - if options == None: - o = {'debug': self.debug, - 'mail': self.mail, - 'bcc': self.bcc, - 'email':self.email, - 'squeeze':self.squeeze, - 'policysavedb':self.policysavedb} +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: - o = options.__dict__ - - pickle.dump(o, f) - f.close() + config.__dict__.update({key : options.cp.get(section, key)}) - def usage(self): - self.print_values() - self.parser.print_help() +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 -def main(): - """ Start threads, do some housekeeping, then daemonize. """ - # Defaults - global config_command - config_command = True - config = __main__.config() - + #from config import options as config + options = Options() try: - print "acalling parse_args" - config.parse_args() - - except Exception, err: - print "Error: %s " % err - config.usage() - sys.exit(1) - - config.usage() + 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 "======================================" -if __name__ == '__main__': - main()