1
2 """Nicer config class."""
3
4 import sys, os, ConfigParser, socket
5
6 __all__ = ['Config']
7
9 """Bit improved ConfigParser.
10
11 Additional features:
12 - Remembers section.
13 - Acceps defaults in get() functions.
14 - List value support.
15 """
16 - def __init__(self, main_section, filename, sane_config = 1):
17 """Initialize Config and read from file.
18
19 @param sane_config: chooses between ConfigParser/SafeConfigParser.
20 """
21 defs = {
22 'job_name': main_section,
23 'service_name': main_section,
24 'host_name': socket.gethostname(),
25 }
26 if not os.path.isfile(filename):
27 raise Exception('Config file not found: '+filename)
28
29 self.filename = filename
30 self.sane_config = sane_config
31 if sane_config:
32 self.cf = ConfigParser.SafeConfigParser(defs)
33 else:
34 self.cf = ConfigParser.ConfigParser(defs)
35 self.cf.read(filename)
36 self.main_section = main_section
37 if not self.cf.has_section(main_section):
38 raise Exception("Wrong config file, no section '%s'"%main_section)
39
41 """Re-reads config file."""
42 self.cf.read(self.filename)
43
44 - def get(self, key, default=None):
45 """Reads string value, if not set then default."""
46 try:
47 return self.cf.get(self.main_section, key)
48 except ConfigParser.NoOptionError, det:
49 if default == None:
50 raise Exception("Config value not set: " + key)
51 return default
52
53 - def getint(self, key, default=None):
54 """Reads int value, if not set then default."""
55 try:
56 return self.cf.getint(self.main_section, key)
57 except ConfigParser.NoOptionError, det:
58 if default == None:
59 raise Exception("Config value not set: " + key)
60 return default
61
63 """Reads boolean value, if not set then default."""
64 try:
65 return self.cf.getboolean(self.main_section, key)
66 except ConfigParser.NoOptionError, det:
67 if default == None:
68 raise Exception("Config value not set: " + key)
69 return default
70
72 """Reads float value, if not set then default."""
73 try:
74 return self.cf.getfloat(self.main_section, key)
75 except ConfigParser.NoOptionError, det:
76 if default == None:
77 raise Exception("Config value not set: " + key)
78 return default
79
80 - def getlist(self, key, default=None):
81 """Reads comma-separated list from key."""
82 try:
83 s = self.cf.get(self.main_section, key).strip()
84 res = []
85 if not s:
86 return res
87 for v in s.split(","):
88 res.append(v.strip())
89 return res
90 except ConfigParser.NoOptionError, det:
91 if default == None:
92 raise Exception("Config value not set: " + key)
93 return default
94
95 - def getfile(self, key, default=None):
96 """Reads filename from config.
97
98 In addition to reading string value, expands ~ to user directory.
99 """
100 fn = self.get(key, default)
101 if fn == "" or fn == "-":
102 return fn
103
104
105
106
107 fn = os.path.expanduser(fn)
108
109 return fn
110
112 """Reads a wildcard property from conf and returns its string value, if not set then default."""
113
114 orig_key = key
115 keys = [key]
116
117 for wild in values:
118 key = key.replace('*', wild, 1)
119 keys.append(key)
120 keys.reverse()
121
122 for key in keys:
123 try:
124 return self.cf.get(self.main_section, key)
125 except ConfigParser.NoOptionError, det:
126 pass
127
128 if default == None:
129 raise Exception("Config value not set: " + orig_key)
130 return default
131
133 """Returns list of sections in config file, excluding DEFAULT."""
134 return self.cf.sections()
135
136 - def clone(self, main_section):
137 """Return new Config() instance with new main section on same config file."""
138 return Config(main_section, self.filename, self.sane_config)
139