- add install_date
[monitor.git] / monitor / config.py
1 #!/usr/bin/python
2
3 # load defaults from /etc/monitor.conf
4 # home/.monitor.conf
5 # $PWD/.monitor.conf
6 import os
7 import ConfigParser
8
9 class Options(object):
10         def __init__(self):
11                 cp = ConfigParser.ConfigParser()
12                 cp.optionxform = str
13                 # load defaults from global, home dir, then $PWD
14                 cp.read(['/etc/monitor.conf', os.path.expanduser('~/.monitor.conf'), 
15                                            '.monitor.conf', 'monitor.conf'])
16                 self.cp = cp
17                 self.section = "default"
18         def __getattr__(self, name):
19                 if name in self.cp.sections():
20                         self.section = name
21                         return self
22                 else:
23                         return self.cp.get(self.section, name)
24
25
26 import config
27 imported = False
28
29 def updatemodule(module, cf):
30         module.__dict__.update(cf.__dict__)
31
32 def update_section(options, section, bool=False):
33         # Place all default commandline values at the top level of this module
34         for key in options.cp.options(section):
35                 if bool:
36                         config.__dict__.update({key : options.cp.getboolean(section, key)})
37                 else:
38                         config.__dict__.update({key : options.cp.get(section, key)})
39
40 def update(parseoptions):
41         update_commandline()
42         # now update the top-level module with all other args passed in here.
43         for key in parseoptions.__dict__.keys():
44                 config.__dict__.update({key: parseoptions.__dict__[key]})
45
46 if not config.imported:
47         imported = True
48
49         #from config import options as config
50         options = Options()
51         try:
52                 update_section(options, 'commandline', True)
53         except:
54                 pass
55         try:
56                 update_section(options, 'monitorconfig')
57         except:
58                 pass
59         try:
60                 update_section(options, 'monitordatabase')
61         except:
62                 pass
63
64 #for i in dir(config):
65 #       if "__" not  in i:
66 #               print i, "==", config.__dict__[i]
67 #print "======================================"
68