2902f1b5ed73ffe71fc9e42e86d349b80ad55c68
[public/pyceo-broken.git] / pylib / csc / common / conf.py
1 """Library Routines"""
2
3 def read_config(config_file):
4
5     try:
6         conffile = open(config_file)
7     except IOError:
8         return None
9     
10     options = {}
11
12     while True:
13
14         line = conffile.readline()
15         if line == '':
16             break
17
18         if '#' in line:
19             line = line[:line.find('#')]
20
21         while len(line) > 1 and line[-2] == '\\':
22             line = line[:-2] + line[-1]
23             next = conffile.readline()
24             line += next
25             if next == '':
26                 break
27
28         pair = map(str.strip, line.split('=', 1))
29         
30         if len(pair) == 2:
31             key, val = pair
32
33             if val[0] == val[-1] == '"':
34                 val = val[1:-1]
35             else:
36                 try:
37                     val = int(val)
38                 except:
39                     pass
40             
41             options[key] = val
42         elif len(pair[0]) > 1:
43             key, = pair
44             options[key] = None
45
46     return options