2 Configuration Utility Module
4 This module contains functions to load and verify very simple configuration
5 files. Python supports ".ini" files, which suck, so this module is used
8 Example Configuration File:
10 include /path/to/other.cf
12 # these values are the same:
13 name_protected = "Michael Spang"
14 name_unprotected = Michael Spang
16 # these values are not the same:
20 # this value is an integer
23 # this value is not an integer
26 # this is a key with no value
29 # this key contains whitespace
30 white space = sure, why not
32 # these two lines are treated as one
33 long line = first line \\
39 'name_protected': 'Michael Spang',
40 'name_unprotected:' 'Michael Spang',
44 'arbitrary_string': '2',
46 'white space': 'sure, why not'
47 'long line': 'first line \\n second line'
49 ... (data from other.cf) ...
53 from curses.ascii import isspace
56 class ConfigurationException(Exception):
57 """Exception class for incomplete and incorrect configurations."""
60 def read(filename, included=None):
62 Function to read a configuration file into a dictionary.
65 filename - the file to read
66 included - files previously read (internal)
69 IOError - when the configuration file cannot be read
74 if filename in included:
76 included.append(filename)
78 conffile = open(filename)
84 line = conffile.readline()
90 line = line[:line.find('#')]
92 # combine lines when the newline is escaped with \
93 while len(line) > 1 and line[-2] == '\\':
94 line = line[:-2] + line[-1]
95 next = conffile.readline()
102 # process include statements
103 if line.find("include") == 0 and isspace(line[7]):
105 filename = line[8:].strip()
106 options.update(read(filename, included))
109 # split 'key = value' into key and value and strip results
110 pair = map(str.strip, line.split('=', 1))
112 # found key and value
116 # found quoted string?
117 if val and val[0] == val[-1] == '"':
120 # unquoted, found num?
135 # found only key, value = None
136 elif len(pair[0]) > 1:
143 def check_string_fields(filename, field_list, cfg):
144 """Function to verify thatfields are strings."""
146 for field in field_list:
147 if field not in cfg or type(cfg[field]) is not str:
148 raise ConfigurationException('expected string value for option "%s" in "%s"' % (field, filename))
150 def check_integer_fields(filename, field_list, cfg):
151 """Function to verify that fields are integers."""
153 for field in field_list:
154 if field not in cfg or type(cfg[field]) not in (int, long):
155 raise ConfigurationException('expected numeric value for option "%s" in "%s"' % (field, filename))
157 def check_float_fields(filename, field_list, cfg):
158 """Function to verify that fields are integers or floats."""
160 for field in field_list:
161 if field not in cfg or type(cfg[field]) not in (float, long, int):
162 raise ConfigurationException('expected float value for option "%s" in "%s"' % (field, filename))