#!/usr/bin/python
-import pickle
-import os
-import getopt
-import sys
-import __main__
-class config:
- debug = True
- mail = False
- bcc = True
- email = "soltesz@cs.utk.edu"
- userlist = None
- cachert = True
- cachenodes = True
- cachesites = True
- squeeze = False
- policysavedb = True
- __file = ".config"
+# load defaults from /etc/monitor.conf
+# home/.monitor.conf
+# $PWD/.monitor.conf
+import os
+import ConfigParser
+class Options(object):
def __init__(self):
- 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()
+ 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)
- def getListFromFile(self, file):
- f = open(file, 'r')
- list = []
- for line in f:
- line = line.strip()
- list += [line]
- return list
-
- def save(self):
- f = open(self.__file, 'w')
- o = {'debug': self.debug,
- 'mail': self.mail,
- 'bcc': self.bcc,
- 'email':self.email,
- 'userlist': self.userlist,
- 'cachert': self.cachert,
- 'cachenodes' : self.cachenodes,
- 'cachesites': self.cachesites,
- 'squeeze':self.squeeze,
- 'policysavedb':self.policysavedb}
- pickle.dump(o, f)
- f.close()
+import config
+imported = False
-def usage():
- config = __main__.config()
- # --cachesites=[0|1] Cache Sites from PLC (current: %s)
- # --status Print memory usage statistics and exit
- print """
-Settings:
- --debug=[0|1] Set debugging (current: %s)
- --mail=[0|1] Send mail or not (current: %s)
- --bcc=[0|1] Include bcc of user (current: %s)
- --email=[email] Email to use above (current: %s)
- --userlist=[filename] Use a list of nodes (current: %s)
- --cachert=[0|1] Cache the RT db (current: %s)
- --cachenodes=[0|1] Cache Nodes from PLC (current: %s)
- --squeeze=[0|1] Squeeze sites or not (current: %s)
- --policysavedb=[0|1] Save policy DBs (current: %s)
- -h, --help This message
-""".lstrip() % (config.debug,
- config.mail,
- config.bcc,
- config.email,
- config.userlist,
- config.cachert,
- config.cachenodes,
- config.squeeze,
- config.policysavedb)
+def updatemodule(module, cf):
+ module.__dict__.update(cf.__dict__)
-def main():
- """ Start threads, do some housekeeping, then daemonize. """
- # Defaults
- config = __main__.config()
+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)})
- try:
- longopts = [ "debug=",
- "mail=",
- "email=",
- "bcc=",
- "userlist=",
- "cachert=",
- "cachesites=",
- "cachenodes=",
- "squeeze=",
- "policysavedb=",
- "status",
- "help"]
- (opts, argv) = getopt.getopt(sys.argv[1:], "h", longopts)
- except getopt.GetoptError, err:
- print "Error: " + err.msg
- usage()
- sys.exit(1)
+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]})
- for (opt, optval) in opts:
- if opt in ["--debug"]:
- config.debug = bool(int(optval))
- print "Running in DEBUG mode. Copying DB & "
- print "caching correspondences. NO SQUEEZING."
- elif opt in ["--mail"]:
- config.mail = bool(int(optval))
- print "NO EMAILS SENT."
- elif opt in ["--email"]:
- config.email = optval
- elif opt in ["--bcc"]:
- config.bcc = bool(int(optval))
- elif opt in ["--userlist"]:
- if len(optval) == 0:
- config.userlist = None
- else:
- config.userlist = optval
- elif opt in ["--cachert"]:
- config.cachert = bool(int(optval))
- elif opt in ["--cachesites"]:
- config.cachesites = bool(int(optval))
- elif opt in ["--cachenodes"]:
- config.cachenodes = bool(int(optval))
- elif opt in ["--policysavedb"]:
- config.policysavedb = bool(int(optval))
- elif opt in ["--squeeze"]:
- config.squeeze = bool(int(optval))
- elif opt in ["--status"]:
- #print summary(names)
- sys.exit(0)
- else:
- usage()
- sys.exit(0)
+if not config.imported:
+ imported = True
- config.save()
- usage()
+ #from config import options as config
+ options = Options()
+ update_section(options, 'commandline', True)
+ update_section(options, 'monitorconfig')
+#for i in dir(config):
+# if "__" not in i:
+# print i, "==", config.__dict__[i]
+#print "======================================"
-if __name__ == '__main__':
- main()